add isl_union_access_info_read_from_file
[isl.git] / isl_flow.c
blob2056a3f6ad3e002fe4f5d2f3f72255054bb1aeb7
1 /*
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
19 #include <isl/set.h>
20 #include <isl/map.h>
21 #include <isl/union_set.h>
22 #include <isl/union_map.h>
23 #include <isl/flow.h>
24 #include <isl/schedule_node.h>
25 #include <isl_sort.h>
26 #include <isl/stream.h>
28 enum isl_restriction_type {
29 isl_restriction_type_empty,
30 isl_restriction_type_none,
31 isl_restriction_type_input,
32 isl_restriction_type_output
35 struct isl_restriction {
36 enum isl_restriction_type type;
38 isl_set *source;
39 isl_set *sink;
42 /* Create a restriction of the given type.
44 static __isl_give isl_restriction *isl_restriction_alloc(
45 __isl_take isl_map *source_map, enum isl_restriction_type type)
47 isl_ctx *ctx;
48 isl_restriction *restr;
50 if (!source_map)
51 return NULL;
53 ctx = isl_map_get_ctx(source_map);
54 restr = isl_calloc_type(ctx, struct isl_restriction);
55 if (!restr)
56 goto error;
58 restr->type = type;
60 isl_map_free(source_map);
61 return restr;
62 error:
63 isl_map_free(source_map);
64 return NULL;
67 /* Create a restriction that doesn't restrict anything.
69 __isl_give isl_restriction *isl_restriction_none(__isl_take isl_map *source_map)
71 return isl_restriction_alloc(source_map, isl_restriction_type_none);
74 /* Create a restriction that removes everything.
76 __isl_give isl_restriction *isl_restriction_empty(
77 __isl_take isl_map *source_map)
79 return isl_restriction_alloc(source_map, isl_restriction_type_empty);
82 /* Create a restriction on the input of the maximization problem
83 * based on the given source and sink restrictions.
85 __isl_give isl_restriction *isl_restriction_input(
86 __isl_take isl_set *source_restr, __isl_take isl_set *sink_restr)
88 isl_ctx *ctx;
89 isl_restriction *restr;
91 if (!source_restr || !sink_restr)
92 goto error;
94 ctx = isl_set_get_ctx(source_restr);
95 restr = isl_calloc_type(ctx, struct isl_restriction);
96 if (!restr)
97 goto error;
99 restr->type = isl_restriction_type_input;
100 restr->source = source_restr;
101 restr->sink = sink_restr;
103 return restr;
104 error:
105 isl_set_free(source_restr);
106 isl_set_free(sink_restr);
107 return NULL;
110 /* Create a restriction on the output of the maximization problem
111 * based on the given source restriction.
113 __isl_give isl_restriction *isl_restriction_output(
114 __isl_take isl_set *source_restr)
116 isl_ctx *ctx;
117 isl_restriction *restr;
119 if (!source_restr)
120 return NULL;
122 ctx = isl_set_get_ctx(source_restr);
123 restr = isl_calloc_type(ctx, struct isl_restriction);
124 if (!restr)
125 goto error;
127 restr->type = isl_restriction_type_output;
128 restr->source = source_restr;
130 return restr;
131 error:
132 isl_set_free(source_restr);
133 return NULL;
136 __isl_null isl_restriction *isl_restriction_free(
137 __isl_take isl_restriction *restr)
139 if (!restr)
140 return NULL;
142 isl_set_free(restr->source);
143 isl_set_free(restr->sink);
144 free(restr);
145 return NULL;
148 isl_ctx *isl_restriction_get_ctx(__isl_keep isl_restriction *restr)
150 return restr ? isl_set_get_ctx(restr->source) : NULL;
153 /* A private structure to keep track of a mapping together with
154 * a user-specified identifier and a boolean indicating whether
155 * the map represents a must or may access/dependence.
157 struct isl_labeled_map {
158 struct isl_map *map;
159 void *data;
160 int must;
163 /* A structure containing the input for dependence analysis:
164 * - a sink
165 * - n_must + n_may (<= max_source) sources
166 * - a function for determining the relative order of sources and sink
167 * The must sources are placed before the may sources.
169 * domain_map is an auxiliary map that maps the sink access relation
170 * to the domain of this access relation.
171 * This field is only needed when restrict_fn is set and
172 * the field itself is set by isl_access_info_compute_flow.
174 * restrict_fn is a callback that (if not NULL) will be called
175 * right before any lexicographical maximization.
177 struct isl_access_info {
178 isl_map *domain_map;
179 struct isl_labeled_map sink;
180 isl_access_level_before level_before;
182 isl_access_restrict restrict_fn;
183 void *restrict_user;
185 int max_source;
186 int n_must;
187 int n_may;
188 struct isl_labeled_map source[1];
191 /* A structure containing the output of dependence analysis:
192 * - n_source dependences
193 * - a wrapped subset of the sink for which definitely no source could be found
194 * - a wrapped subset of the sink for which possibly no source could be found
196 struct isl_flow {
197 isl_set *must_no_source;
198 isl_set *may_no_source;
199 int n_source;
200 struct isl_labeled_map *dep;
203 /* Construct an isl_access_info structure and fill it up with
204 * the given data. The number of sources is set to 0.
206 __isl_give isl_access_info *isl_access_info_alloc(__isl_take isl_map *sink,
207 void *sink_user, isl_access_level_before fn, int max_source)
209 isl_ctx *ctx;
210 struct isl_access_info *acc;
212 if (!sink)
213 return NULL;
215 ctx = isl_map_get_ctx(sink);
216 isl_assert(ctx, max_source >= 0, goto error);
218 acc = isl_calloc(ctx, struct isl_access_info,
219 sizeof(struct isl_access_info) +
220 (max_source - 1) * sizeof(struct isl_labeled_map));
221 if (!acc)
222 goto error;
224 acc->sink.map = sink;
225 acc->sink.data = sink_user;
226 acc->level_before = fn;
227 acc->max_source = max_source;
228 acc->n_must = 0;
229 acc->n_may = 0;
231 return acc;
232 error:
233 isl_map_free(sink);
234 return NULL;
237 /* Free the given isl_access_info structure.
239 __isl_null isl_access_info *isl_access_info_free(
240 __isl_take isl_access_info *acc)
242 int i;
244 if (!acc)
245 return NULL;
246 isl_map_free(acc->domain_map);
247 isl_map_free(acc->sink.map);
248 for (i = 0; i < acc->n_must + acc->n_may; ++i)
249 isl_map_free(acc->source[i].map);
250 free(acc);
251 return NULL;
254 isl_ctx *isl_access_info_get_ctx(__isl_keep isl_access_info *acc)
256 return acc ? isl_map_get_ctx(acc->sink.map) : NULL;
259 __isl_give isl_access_info *isl_access_info_set_restrict(
260 __isl_take isl_access_info *acc, isl_access_restrict fn, void *user)
262 if (!acc)
263 return NULL;
264 acc->restrict_fn = fn;
265 acc->restrict_user = user;
266 return acc;
269 /* Add another source to an isl_access_info structure, making
270 * sure the "must" sources are placed before the "may" sources.
271 * This function may be called at most max_source times on a
272 * given isl_access_info structure, with max_source as specified
273 * in the call to isl_access_info_alloc that constructed the structure.
275 __isl_give isl_access_info *isl_access_info_add_source(
276 __isl_take isl_access_info *acc, __isl_take isl_map *source,
277 int must, void *source_user)
279 isl_ctx *ctx;
281 if (!acc)
282 goto error;
283 ctx = isl_map_get_ctx(acc->sink.map);
284 isl_assert(ctx, acc->n_must + acc->n_may < acc->max_source, goto error);
286 if (must) {
287 if (acc->n_may)
288 acc->source[acc->n_must + acc->n_may] =
289 acc->source[acc->n_must];
290 acc->source[acc->n_must].map = source;
291 acc->source[acc->n_must].data = source_user;
292 acc->source[acc->n_must].must = 1;
293 acc->n_must++;
294 } else {
295 acc->source[acc->n_must + acc->n_may].map = source;
296 acc->source[acc->n_must + acc->n_may].data = source_user;
297 acc->source[acc->n_must + acc->n_may].must = 0;
298 acc->n_may++;
301 return acc;
302 error:
303 isl_map_free(source);
304 isl_access_info_free(acc);
305 return NULL;
308 /* Return -n, 0 or n (with n a positive value), depending on whether
309 * the source access identified by p1 should be sorted before, together
310 * or after that identified by p2.
312 * If p1 appears before p2, then it should be sorted first.
313 * For more generic initial schedules, it is possible that neither
314 * p1 nor p2 appears before the other, or at least not in any obvious way.
315 * We therefore also check if p2 appears before p1, in which case p2
316 * should be sorted first.
317 * If not, we try to order the two statements based on the description
318 * of the iteration domains. This results in an arbitrary, but fairly
319 * stable ordering.
321 static int access_sort_cmp(const void *p1, const void *p2, void *user)
323 isl_access_info *acc = user;
324 const struct isl_labeled_map *i1, *i2;
325 int level1, level2;
326 uint32_t h1, h2;
327 i1 = (const struct isl_labeled_map *) p1;
328 i2 = (const struct isl_labeled_map *) p2;
330 level1 = acc->level_before(i1->data, i2->data);
331 if (level1 % 2)
332 return -1;
334 level2 = acc->level_before(i2->data, i1->data);
335 if (level2 % 2)
336 return 1;
338 h1 = isl_map_get_hash(i1->map);
339 h2 = isl_map_get_hash(i2->map);
340 return h1 > h2 ? 1 : h1 < h2 ? -1 : 0;
343 /* Sort the must source accesses in their textual order.
345 static __isl_give isl_access_info *isl_access_info_sort_sources(
346 __isl_take isl_access_info *acc)
348 if (!acc)
349 return NULL;
350 if (acc->n_must <= 1)
351 return acc;
353 if (isl_sort(acc->source, acc->n_must, sizeof(struct isl_labeled_map),
354 access_sort_cmp, acc) < 0)
355 return isl_access_info_free(acc);
357 return acc;
360 /* Align the parameters of the two spaces if needed and then call
361 * isl_space_join.
363 static __isl_give isl_space *space_align_and_join(__isl_take isl_space *left,
364 __isl_take isl_space *right)
366 isl_bool equal_params;
368 equal_params = isl_space_has_equal_params(left, right);
369 if (equal_params < 0)
370 goto error;
371 if (equal_params)
372 return isl_space_join(left, right);
374 left = isl_space_align_params(left, isl_space_copy(right));
375 right = isl_space_align_params(right, isl_space_copy(left));
376 return isl_space_join(left, right);
377 error:
378 isl_space_free(left);
379 isl_space_free(right);
380 return NULL;
383 /* Initialize an empty isl_flow structure corresponding to a given
384 * isl_access_info structure.
385 * For each must access, two dependences are created (initialized
386 * to the empty relation), one for the resulting must dependences
387 * and one for the resulting may dependences. May accesses can
388 * only lead to may dependences, so only one dependence is created
389 * for each of them.
390 * This function is private as isl_flow structures are only supposed
391 * to be created by isl_access_info_compute_flow.
393 static __isl_give isl_flow *isl_flow_alloc(__isl_keep isl_access_info *acc)
395 int i, n;
396 struct isl_ctx *ctx;
397 struct isl_flow *dep;
399 if (!acc)
400 return NULL;
402 ctx = isl_map_get_ctx(acc->sink.map);
403 dep = isl_calloc_type(ctx, struct isl_flow);
404 if (!dep)
405 return NULL;
407 n = 2 * acc->n_must + acc->n_may;
408 dep->dep = isl_calloc_array(ctx, struct isl_labeled_map, n);
409 if (n && !dep->dep)
410 goto error;
412 dep->n_source = n;
413 for (i = 0; i < acc->n_must; ++i) {
414 isl_space *dim;
415 dim = space_align_and_join(
416 isl_map_get_space(acc->source[i].map),
417 isl_space_reverse(isl_map_get_space(acc->sink.map)));
418 dep->dep[2 * i].map = isl_map_empty(dim);
419 dep->dep[2 * i + 1].map = isl_map_copy(dep->dep[2 * i].map);
420 dep->dep[2 * i].data = acc->source[i].data;
421 dep->dep[2 * i + 1].data = acc->source[i].data;
422 dep->dep[2 * i].must = 1;
423 dep->dep[2 * i + 1].must = 0;
424 if (!dep->dep[2 * i].map || !dep->dep[2 * i + 1].map)
425 goto error;
427 for (i = acc->n_must; i < acc->n_must + acc->n_may; ++i) {
428 isl_space *dim;
429 dim = space_align_and_join(
430 isl_map_get_space(acc->source[i].map),
431 isl_space_reverse(isl_map_get_space(acc->sink.map)));
432 dep->dep[acc->n_must + i].map = isl_map_empty(dim);
433 dep->dep[acc->n_must + i].data = acc->source[i].data;
434 dep->dep[acc->n_must + i].must = 0;
435 if (!dep->dep[acc->n_must + i].map)
436 goto error;
439 return dep;
440 error:
441 isl_flow_free(dep);
442 return NULL;
445 /* Iterate over all sources and for each resulting flow dependence
446 * that is not empty, call the user specfied function.
447 * The second argument in this function call identifies the source,
448 * while the third argument correspond to the final argument of
449 * the isl_flow_foreach call.
451 isl_stat isl_flow_foreach(__isl_keep isl_flow *deps,
452 isl_stat (*fn)(__isl_take isl_map *dep, int must, void *dep_user,
453 void *user),
454 void *user)
456 int i;
458 if (!deps)
459 return isl_stat_error;
461 for (i = 0; i < deps->n_source; ++i) {
462 if (isl_map_plain_is_empty(deps->dep[i].map))
463 continue;
464 if (fn(isl_map_copy(deps->dep[i].map), deps->dep[i].must,
465 deps->dep[i].data, user) < 0)
466 return isl_stat_error;
469 return isl_stat_ok;
472 /* Return a copy of the subset of the sink for which no source could be found.
474 __isl_give isl_map *isl_flow_get_no_source(__isl_keep isl_flow *deps, int must)
476 if (!deps)
477 return NULL;
479 if (must)
480 return isl_set_unwrap(isl_set_copy(deps->must_no_source));
481 else
482 return isl_set_unwrap(isl_set_copy(deps->may_no_source));
485 void isl_flow_free(__isl_take isl_flow *deps)
487 int i;
489 if (!deps)
490 return;
491 isl_set_free(deps->must_no_source);
492 isl_set_free(deps->may_no_source);
493 if (deps->dep) {
494 for (i = 0; i < deps->n_source; ++i)
495 isl_map_free(deps->dep[i].map);
496 free(deps->dep);
498 free(deps);
501 isl_ctx *isl_flow_get_ctx(__isl_keep isl_flow *deps)
503 return deps ? isl_set_get_ctx(deps->must_no_source) : NULL;
506 /* Return a map that enforces that the domain iteration occurs after
507 * the range iteration at the given level.
508 * If level is odd, then the domain iteration should occur after
509 * the target iteration in their shared level/2 outermost loops.
510 * In this case we simply need to enforce that these outermost
511 * loop iterations are the same.
512 * If level is even, then the loop iterator of the domain should
513 * be greater than the loop iterator of the range at the last
514 * of the level/2 shared loops, i.e., loop level/2 - 1.
516 static __isl_give isl_map *after_at_level(__isl_take isl_space *dim, int level)
518 struct isl_basic_map *bmap;
520 if (level % 2)
521 bmap = isl_basic_map_equal(dim, level/2);
522 else
523 bmap = isl_basic_map_more_at(dim, level/2 - 1);
525 return isl_map_from_basic_map(bmap);
528 /* Compute the partial lexicographic maximum of "dep" on domain "sink",
529 * but first check if the user has set acc->restrict_fn and if so
530 * update either the input or the output of the maximization problem
531 * with respect to the resulting restriction.
533 * Since the user expects a mapping from sink iterations to source iterations,
534 * whereas the domain of "dep" is a wrapped map, mapping sink iterations
535 * to accessed array elements, we first need to project out the accessed
536 * sink array elements by applying acc->domain_map.
537 * Similarly, the sink restriction specified by the user needs to be
538 * converted back to the wrapped map.
540 static __isl_give isl_map *restricted_partial_lexmax(
541 __isl_keep isl_access_info *acc, __isl_take isl_map *dep,
542 int source, __isl_take isl_set *sink, __isl_give isl_set **empty)
544 isl_map *source_map;
545 isl_restriction *restr;
546 isl_set *sink_domain;
547 isl_set *sink_restr;
548 isl_map *res;
550 if (!acc->restrict_fn)
551 return isl_map_partial_lexmax(dep, sink, empty);
553 source_map = isl_map_copy(dep);
554 source_map = isl_map_apply_domain(source_map,
555 isl_map_copy(acc->domain_map));
556 sink_domain = isl_set_copy(sink);
557 sink_domain = isl_set_apply(sink_domain, isl_map_copy(acc->domain_map));
558 restr = acc->restrict_fn(source_map, sink_domain,
559 acc->source[source].data, acc->restrict_user);
560 isl_set_free(sink_domain);
561 isl_map_free(source_map);
563 if (!restr)
564 goto error;
565 if (restr->type == isl_restriction_type_input) {
566 dep = isl_map_intersect_range(dep, isl_set_copy(restr->source));
567 sink_restr = isl_set_copy(restr->sink);
568 sink_restr = isl_set_apply(sink_restr,
569 isl_map_reverse(isl_map_copy(acc->domain_map)));
570 sink = isl_set_intersect(sink, sink_restr);
571 } else if (restr->type == isl_restriction_type_empty) {
572 isl_space *space = isl_map_get_space(dep);
573 isl_map_free(dep);
574 dep = isl_map_empty(space);
577 res = isl_map_partial_lexmax(dep, sink, empty);
579 if (restr->type == isl_restriction_type_output)
580 res = isl_map_intersect_range(res, isl_set_copy(restr->source));
582 isl_restriction_free(restr);
583 return res;
584 error:
585 isl_map_free(dep);
586 isl_set_free(sink);
587 *empty = NULL;
588 return NULL;
591 /* Compute the last iteration of must source j that precedes the sink
592 * at the given level for sink iterations in set_C.
593 * The subset of set_C for which no such iteration can be found is returned
594 * in *empty.
596 static struct isl_map *last_source(struct isl_access_info *acc,
597 struct isl_set *set_C,
598 int j, int level, struct isl_set **empty)
600 struct isl_map *read_map;
601 struct isl_map *write_map;
602 struct isl_map *dep_map;
603 struct isl_map *after;
604 struct isl_map *result;
606 read_map = isl_map_copy(acc->sink.map);
607 write_map = isl_map_copy(acc->source[j].map);
608 write_map = isl_map_reverse(write_map);
609 dep_map = isl_map_apply_range(read_map, write_map);
610 after = after_at_level(isl_map_get_space(dep_map), level);
611 dep_map = isl_map_intersect(dep_map, after);
612 result = restricted_partial_lexmax(acc, dep_map, j, set_C, empty);
613 result = isl_map_reverse(result);
615 return result;
618 /* For a given mapping between iterations of must source j and iterations
619 * of the sink, compute the last iteration of must source k preceding
620 * the sink at level before_level for any of the sink iterations,
621 * but following the corresponding iteration of must source j at level
622 * after_level.
624 static struct isl_map *last_later_source(struct isl_access_info *acc,
625 struct isl_map *old_map,
626 int j, int before_level,
627 int k, int after_level,
628 struct isl_set **empty)
630 isl_space *dim;
631 struct isl_set *set_C;
632 struct isl_map *read_map;
633 struct isl_map *write_map;
634 struct isl_map *dep_map;
635 struct isl_map *after_write;
636 struct isl_map *before_read;
637 struct isl_map *result;
639 set_C = isl_map_range(isl_map_copy(old_map));
640 read_map = isl_map_copy(acc->sink.map);
641 write_map = isl_map_copy(acc->source[k].map);
643 write_map = isl_map_reverse(write_map);
644 dep_map = isl_map_apply_range(read_map, write_map);
645 dim = space_align_and_join(isl_map_get_space(acc->source[k].map),
646 isl_space_reverse(isl_map_get_space(acc->source[j].map)));
647 after_write = after_at_level(dim, after_level);
648 after_write = isl_map_apply_range(after_write, old_map);
649 after_write = isl_map_reverse(after_write);
650 dep_map = isl_map_intersect(dep_map, after_write);
651 before_read = after_at_level(isl_map_get_space(dep_map), before_level);
652 dep_map = isl_map_intersect(dep_map, before_read);
653 result = restricted_partial_lexmax(acc, dep_map, k, set_C, empty);
654 result = isl_map_reverse(result);
656 return result;
659 /* Given a shared_level between two accesses, return 1 if the
660 * the first can precede the second at the requested target_level.
661 * If the target level is odd, i.e., refers to a statement level
662 * dimension, then first needs to precede second at the requested
663 * level, i.e., shared_level must be equal to target_level.
664 * If the target level is odd, then the two loops should share
665 * at least the requested number of outer loops.
667 static int can_precede_at_level(int shared_level, int target_level)
669 if (shared_level < target_level)
670 return 0;
671 if ((target_level % 2) && shared_level > target_level)
672 return 0;
673 return 1;
676 /* Given a possible flow dependence temp_rel[j] between source j and the sink
677 * at level sink_level, remove those elements for which
678 * there is an iteration of another source k < j that is closer to the sink.
679 * The flow dependences temp_rel[k] are updated with the improved sources.
680 * Any improved source needs to precede the sink at the same level
681 * and needs to follow source j at the same or a deeper level.
682 * The lower this level, the later the execution date of source k.
683 * We therefore consider lower levels first.
685 * If temp_rel[j] is empty, then there can be no improvement and
686 * we return immediately.
688 static int intermediate_sources(__isl_keep isl_access_info *acc,
689 struct isl_map **temp_rel, int j, int sink_level)
691 int k, level;
692 int depth = 2 * isl_map_dim(acc->source[j].map, isl_dim_in) + 1;
694 if (isl_map_plain_is_empty(temp_rel[j]))
695 return 0;
697 for (k = j - 1; k >= 0; --k) {
698 int plevel, plevel2;
699 plevel = acc->level_before(acc->source[k].data, acc->sink.data);
700 if (!can_precede_at_level(plevel, sink_level))
701 continue;
703 plevel2 = acc->level_before(acc->source[j].data,
704 acc->source[k].data);
706 for (level = sink_level; level <= depth; ++level) {
707 struct isl_map *T;
708 struct isl_set *trest;
709 struct isl_map *copy;
711 if (!can_precede_at_level(plevel2, level))
712 continue;
714 copy = isl_map_copy(temp_rel[j]);
715 T = last_later_source(acc, copy, j, sink_level, k,
716 level, &trest);
717 if (isl_map_plain_is_empty(T)) {
718 isl_set_free(trest);
719 isl_map_free(T);
720 continue;
722 temp_rel[j] = isl_map_intersect_range(temp_rel[j], trest);
723 temp_rel[k] = isl_map_union_disjoint(temp_rel[k], T);
727 return 0;
730 /* Compute all iterations of may source j that precedes the sink at the given
731 * level for sink iterations in set_C.
733 static __isl_give isl_map *all_sources(__isl_keep isl_access_info *acc,
734 __isl_take isl_set *set_C, int j, int level)
736 isl_map *read_map;
737 isl_map *write_map;
738 isl_map *dep_map;
739 isl_map *after;
741 read_map = isl_map_copy(acc->sink.map);
742 read_map = isl_map_intersect_domain(read_map, set_C);
743 write_map = isl_map_copy(acc->source[acc->n_must + j].map);
744 write_map = isl_map_reverse(write_map);
745 dep_map = isl_map_apply_range(read_map, write_map);
746 after = after_at_level(isl_map_get_space(dep_map), level);
747 dep_map = isl_map_intersect(dep_map, after);
749 return isl_map_reverse(dep_map);
752 /* For a given mapping between iterations of must source k and iterations
753 * of the sink, compute all iterations of may source j preceding
754 * the sink at level before_level for any of the sink iterations,
755 * but following the corresponding iteration of must source k at level
756 * after_level.
758 static __isl_give isl_map *all_later_sources(__isl_keep isl_access_info *acc,
759 __isl_take isl_map *old_map,
760 int j, int before_level, int k, int after_level)
762 isl_space *dim;
763 isl_set *set_C;
764 isl_map *read_map;
765 isl_map *write_map;
766 isl_map *dep_map;
767 isl_map *after_write;
768 isl_map *before_read;
770 set_C = isl_map_range(isl_map_copy(old_map));
771 read_map = isl_map_copy(acc->sink.map);
772 read_map = isl_map_intersect_domain(read_map, set_C);
773 write_map = isl_map_copy(acc->source[acc->n_must + j].map);
775 write_map = isl_map_reverse(write_map);
776 dep_map = isl_map_apply_range(read_map, write_map);
777 dim = isl_space_join(isl_map_get_space(acc->source[acc->n_must + j].map),
778 isl_space_reverse(isl_map_get_space(acc->source[k].map)));
779 after_write = after_at_level(dim, after_level);
780 after_write = isl_map_apply_range(after_write, old_map);
781 after_write = isl_map_reverse(after_write);
782 dep_map = isl_map_intersect(dep_map, after_write);
783 before_read = after_at_level(isl_map_get_space(dep_map), before_level);
784 dep_map = isl_map_intersect(dep_map, before_read);
785 return isl_map_reverse(dep_map);
788 /* Given the must and may dependence relations for the must accesses
789 * for level sink_level, check if there are any accesses of may access j
790 * that occur in between and return their union.
791 * If some of these accesses are intermediate with respect to
792 * (previously thought to be) must dependences, then these
793 * must dependences are turned into may dependences.
795 static __isl_give isl_map *all_intermediate_sources(
796 __isl_keep isl_access_info *acc, __isl_take isl_map *map,
797 struct isl_map **must_rel, struct isl_map **may_rel,
798 int j, int sink_level)
800 int k, level;
801 int depth = 2 * isl_map_dim(acc->source[acc->n_must + j].map,
802 isl_dim_in) + 1;
804 for (k = 0; k < acc->n_must; ++k) {
805 int plevel;
807 if (isl_map_plain_is_empty(may_rel[k]) &&
808 isl_map_plain_is_empty(must_rel[k]))
809 continue;
811 plevel = acc->level_before(acc->source[k].data,
812 acc->source[acc->n_must + j].data);
814 for (level = sink_level; level <= depth; ++level) {
815 isl_map *T;
816 isl_map *copy;
817 isl_set *ran;
819 if (!can_precede_at_level(plevel, level))
820 continue;
822 copy = isl_map_copy(may_rel[k]);
823 T = all_later_sources(acc, copy, j, sink_level, k, level);
824 map = isl_map_union(map, T);
826 copy = isl_map_copy(must_rel[k]);
827 T = all_later_sources(acc, copy, j, sink_level, k, level);
828 ran = isl_map_range(isl_map_copy(T));
829 map = isl_map_union(map, T);
830 may_rel[k] = isl_map_union_disjoint(may_rel[k],
831 isl_map_intersect_range(isl_map_copy(must_rel[k]),
832 isl_set_copy(ran)));
833 T = isl_map_from_domain_and_range(
834 isl_set_universe(
835 isl_space_domain(isl_map_get_space(must_rel[k]))),
836 ran);
837 must_rel[k] = isl_map_subtract(must_rel[k], T);
841 return map;
844 /* Compute dependences for the case where all accesses are "may"
845 * accesses, which boils down to computing memory based dependences.
846 * The generic algorithm would also work in this case, but it would
847 * be overkill to use it.
849 static __isl_give isl_flow *compute_mem_based_dependences(
850 __isl_keep isl_access_info *acc)
852 int i;
853 isl_set *mustdo;
854 isl_set *maydo;
855 isl_flow *res;
857 res = isl_flow_alloc(acc);
858 if (!res)
859 return NULL;
861 mustdo = isl_map_domain(isl_map_copy(acc->sink.map));
862 maydo = isl_set_copy(mustdo);
864 for (i = 0; i < acc->n_may; ++i) {
865 int plevel;
866 int is_before;
867 isl_space *dim;
868 isl_map *before;
869 isl_map *dep;
871 plevel = acc->level_before(acc->source[i].data, acc->sink.data);
872 is_before = plevel & 1;
873 plevel >>= 1;
875 dim = isl_map_get_space(res->dep[i].map);
876 if (is_before)
877 before = isl_map_lex_le_first(dim, plevel);
878 else
879 before = isl_map_lex_lt_first(dim, plevel);
880 dep = isl_map_apply_range(isl_map_copy(acc->source[i].map),
881 isl_map_reverse(isl_map_copy(acc->sink.map)));
882 dep = isl_map_intersect(dep, before);
883 mustdo = isl_set_subtract(mustdo,
884 isl_map_range(isl_map_copy(dep)));
885 res->dep[i].map = isl_map_union(res->dep[i].map, dep);
888 res->may_no_source = isl_set_subtract(maydo, isl_set_copy(mustdo));
889 res->must_no_source = mustdo;
891 return res;
894 /* Compute dependences for the case where there is at least one
895 * "must" access.
897 * The core algorithm considers all levels in which a source may precede
898 * the sink, where a level may either be a statement level or a loop level.
899 * The outermost statement level is 1, the first loop level is 2, etc...
900 * The algorithm basically does the following:
901 * for all levels l of the read access from innermost to outermost
902 * for all sources w that may precede the sink access at that level
903 * compute the last iteration of the source that precedes the sink access
904 * at that level
905 * add result to possible last accesses at level l of source w
906 * for all sources w2 that we haven't considered yet at this level that may
907 * also precede the sink access
908 * for all levels l2 of w from l to innermost
909 * for all possible last accesses dep of w at l
910 * compute last iteration of w2 between the source and sink
911 * of dep
912 * add result to possible last accesses at level l of write w2
913 * and replace possible last accesses dep by the remainder
916 * The above algorithm is applied to the must access. During the course
917 * of the algorithm, we keep track of sink iterations that still
918 * need to be considered. These iterations are split into those that
919 * haven't been matched to any source access (mustdo) and those that have only
920 * been matched to may accesses (maydo).
921 * At the end of each level, we also consider the may accesses.
922 * In particular, we consider may accesses that precede the remaining
923 * sink iterations, moving elements from mustdo to maydo when appropriate,
924 * and may accesses that occur between a must source and a sink of any
925 * dependences found at the current level, turning must dependences into
926 * may dependences when appropriate.
929 static __isl_give isl_flow *compute_val_based_dependences(
930 __isl_keep isl_access_info *acc)
932 isl_ctx *ctx;
933 isl_flow *res;
934 isl_set *mustdo = NULL;
935 isl_set *maydo = NULL;
936 int level, j;
937 int depth;
938 isl_map **must_rel = NULL;
939 isl_map **may_rel = NULL;
941 if (!acc)
942 return NULL;
944 res = isl_flow_alloc(acc);
945 if (!res)
946 goto error;
947 ctx = isl_map_get_ctx(acc->sink.map);
949 depth = 2 * isl_map_dim(acc->sink.map, isl_dim_in) + 1;
950 mustdo = isl_map_domain(isl_map_copy(acc->sink.map));
951 maydo = isl_set_empty(isl_set_get_space(mustdo));
952 if (!mustdo || !maydo)
953 goto error;
954 if (isl_set_plain_is_empty(mustdo))
955 goto done;
957 must_rel = isl_alloc_array(ctx, struct isl_map *, acc->n_must);
958 may_rel = isl_alloc_array(ctx, struct isl_map *, acc->n_must);
959 if (!must_rel || !may_rel)
960 goto error;
962 for (level = depth; level >= 1; --level) {
963 for (j = acc->n_must-1; j >=0; --j) {
964 isl_space *space;
965 space = isl_map_get_space(res->dep[2 * j].map);
966 must_rel[j] = isl_map_empty(space);
967 may_rel[j] = isl_map_copy(must_rel[j]);
970 for (j = acc->n_must - 1; j >= 0; --j) {
971 struct isl_map *T;
972 struct isl_set *rest;
973 int plevel;
975 plevel = acc->level_before(acc->source[j].data,
976 acc->sink.data);
977 if (!can_precede_at_level(plevel, level))
978 continue;
980 T = last_source(acc, mustdo, j, level, &rest);
981 must_rel[j] = isl_map_union_disjoint(must_rel[j], T);
982 mustdo = rest;
984 intermediate_sources(acc, must_rel, j, level);
986 T = last_source(acc, maydo, j, level, &rest);
987 may_rel[j] = isl_map_union_disjoint(may_rel[j], T);
988 maydo = rest;
990 intermediate_sources(acc, may_rel, j, level);
992 if (isl_set_plain_is_empty(mustdo) &&
993 isl_set_plain_is_empty(maydo))
994 break;
996 for (j = j - 1; j >= 0; --j) {
997 int plevel;
999 plevel = acc->level_before(acc->source[j].data,
1000 acc->sink.data);
1001 if (!can_precede_at_level(plevel, level))
1002 continue;
1004 intermediate_sources(acc, must_rel, j, level);
1005 intermediate_sources(acc, may_rel, j, level);
1008 for (j = 0; j < acc->n_may; ++j) {
1009 int plevel;
1010 isl_map *T;
1011 isl_set *ran;
1013 plevel = acc->level_before(acc->source[acc->n_must + j].data,
1014 acc->sink.data);
1015 if (!can_precede_at_level(plevel, level))
1016 continue;
1018 T = all_sources(acc, isl_set_copy(maydo), j, level);
1019 res->dep[2 * acc->n_must + j].map =
1020 isl_map_union(res->dep[2 * acc->n_must + j].map, T);
1021 T = all_sources(acc, isl_set_copy(mustdo), j, level);
1022 ran = isl_map_range(isl_map_copy(T));
1023 res->dep[2 * acc->n_must + j].map =
1024 isl_map_union(res->dep[2 * acc->n_must + j].map, T);
1025 mustdo = isl_set_subtract(mustdo, isl_set_copy(ran));
1026 maydo = isl_set_union_disjoint(maydo, ran);
1028 T = res->dep[2 * acc->n_must + j].map;
1029 T = all_intermediate_sources(acc, T, must_rel, may_rel,
1030 j, level);
1031 res->dep[2 * acc->n_must + j].map = T;
1034 for (j = acc->n_must - 1; j >= 0; --j) {
1035 res->dep[2 * j].map =
1036 isl_map_union_disjoint(res->dep[2 * j].map,
1037 must_rel[j]);
1038 res->dep[2 * j + 1].map =
1039 isl_map_union_disjoint(res->dep[2 * j + 1].map,
1040 may_rel[j]);
1043 if (isl_set_plain_is_empty(mustdo) &&
1044 isl_set_plain_is_empty(maydo))
1045 break;
1048 free(must_rel);
1049 free(may_rel);
1050 done:
1051 res->must_no_source = mustdo;
1052 res->may_no_source = maydo;
1053 return res;
1054 error:
1055 isl_flow_free(res);
1056 isl_set_free(mustdo);
1057 isl_set_free(maydo);
1058 free(must_rel);
1059 free(may_rel);
1060 return NULL;
1063 /* Given a "sink" access, a list of n "source" accesses,
1064 * compute for each iteration of the sink access
1065 * and for each element accessed by that iteration,
1066 * the source access in the list that last accessed the
1067 * element accessed by the sink access before this sink access.
1068 * Each access is given as a map from the loop iterators
1069 * to the array indices.
1070 * The result is a list of n relations between source and sink
1071 * iterations and a subset of the domain of the sink access,
1072 * corresponding to those iterations that access an element
1073 * not previously accessed.
1075 * To deal with multi-valued sink access relations, the sink iteration
1076 * domain is first extended with dimensions that correspond to the data
1077 * space. However, these extra dimensions are not projected out again.
1078 * It is up to the caller to decide whether these dimensions should be kept.
1080 static __isl_give isl_flow *access_info_compute_flow_core(
1081 __isl_take isl_access_info *acc)
1083 struct isl_flow *res = NULL;
1085 if (!acc)
1086 return NULL;
1088 acc->sink.map = isl_map_range_map(acc->sink.map);
1089 if (!acc->sink.map)
1090 goto error;
1092 if (acc->n_must == 0)
1093 res = compute_mem_based_dependences(acc);
1094 else {
1095 acc = isl_access_info_sort_sources(acc);
1096 res = compute_val_based_dependences(acc);
1098 acc = isl_access_info_free(acc);
1099 if (!res)
1100 return NULL;
1101 if (!res->must_no_source || !res->may_no_source)
1102 goto error;
1103 return res;
1104 error:
1105 isl_access_info_free(acc);
1106 isl_flow_free(res);
1107 return NULL;
1110 /* Given a "sink" access, a list of n "source" accesses,
1111 * compute for each iteration of the sink access
1112 * and for each element accessed by that iteration,
1113 * the source access in the list that last accessed the
1114 * element accessed by the sink access before this sink access.
1115 * Each access is given as a map from the loop iterators
1116 * to the array indices.
1117 * The result is a list of n relations between source and sink
1118 * iterations and a subset of the domain of the sink access,
1119 * corresponding to those iterations that access an element
1120 * not previously accessed.
1122 * To deal with multi-valued sink access relations,
1123 * access_info_compute_flow_core extends the sink iteration domain
1124 * with dimensions that correspond to the data space. These extra dimensions
1125 * are projected out from the result of access_info_compute_flow_core.
1127 __isl_give isl_flow *isl_access_info_compute_flow(__isl_take isl_access_info *acc)
1129 int j;
1130 struct isl_flow *res;
1132 if (!acc)
1133 return NULL;
1135 acc->domain_map = isl_map_domain_map(isl_map_copy(acc->sink.map));
1136 res = access_info_compute_flow_core(acc);
1137 if (!res)
1138 return NULL;
1140 for (j = 0; j < res->n_source; ++j) {
1141 res->dep[j].map = isl_map_range_factor_domain(res->dep[j].map);
1142 if (!res->dep[j].map)
1143 goto error;
1146 return res;
1147 error:
1148 isl_flow_free(res);
1149 return NULL;
1153 /* Keep track of some information about a schedule for a given
1154 * access. In particular, keep track of which dimensions
1155 * have a constant value and of the actual constant values.
1157 struct isl_sched_info {
1158 int *is_cst;
1159 isl_vec *cst;
1162 static void sched_info_free(__isl_take struct isl_sched_info *info)
1164 if (!info)
1165 return;
1166 isl_vec_free(info->cst);
1167 free(info->is_cst);
1168 free(info);
1171 /* Extract information on the constant dimensions of the schedule
1172 * for a given access. The "map" is of the form
1174 * [S -> D] -> A
1176 * with S the schedule domain, D the iteration domain and A the data domain.
1178 static __isl_give struct isl_sched_info *sched_info_alloc(
1179 __isl_keep isl_map *map)
1181 isl_ctx *ctx;
1182 isl_space *dim;
1183 struct isl_sched_info *info;
1184 int i, n;
1186 if (!map)
1187 return NULL;
1189 dim = isl_space_unwrap(isl_space_domain(isl_map_get_space(map)));
1190 if (!dim)
1191 return NULL;
1192 n = isl_space_dim(dim, isl_dim_in);
1193 isl_space_free(dim);
1195 ctx = isl_map_get_ctx(map);
1196 info = isl_alloc_type(ctx, struct isl_sched_info);
1197 if (!info)
1198 return NULL;
1199 info->is_cst = isl_alloc_array(ctx, int, n);
1200 info->cst = isl_vec_alloc(ctx, n);
1201 if (n && (!info->is_cst || !info->cst))
1202 goto error;
1204 for (i = 0; i < n; ++i) {
1205 isl_val *v;
1207 v = isl_map_plain_get_val_if_fixed(map, isl_dim_in, i);
1208 if (!v)
1209 goto error;
1210 info->is_cst[i] = !isl_val_is_nan(v);
1211 if (info->is_cst[i])
1212 info->cst = isl_vec_set_element_val(info->cst, i, v);
1213 else
1214 isl_val_free(v);
1217 return info;
1218 error:
1219 sched_info_free(info);
1220 return NULL;
1223 /* The different types of access relations that isl_union_access_info
1224 * keeps track of.
1226 * "isl_access_sink" represents the sink accesses.
1227 * "isl_access_must_source" represents the definite source accesses.
1228 * "isl_access_may_source" represents the possible source accesses.
1230 * isl_access_sink is sometimes treated differently and
1231 * should therefore appear first.
1233 enum isl_access_type {
1234 isl_access_sink,
1235 isl_access_must_source,
1236 isl_access_may_source,
1237 isl_access_end
1240 /* This structure represents the input for a dependence analysis computation.
1242 * "access" contains the access relations.
1244 * "schedule" or "schedule_map" represents the execution order.
1245 * Exactly one of these fields should be NULL. The other field
1246 * determines the execution order.
1248 * The domains of these four maps refer to the same iteration spaces(s).
1249 * The ranges of the first three maps also refer to the same data space(s).
1251 * After a call to isl_union_access_info_introduce_schedule,
1252 * the "schedule_map" field no longer contains useful information.
1254 struct isl_union_access_info {
1255 isl_union_map *access[isl_access_end];
1257 isl_schedule *schedule;
1258 isl_union_map *schedule_map;
1261 /* Free "access" and return NULL.
1263 __isl_null isl_union_access_info *isl_union_access_info_free(
1264 __isl_take isl_union_access_info *access)
1266 enum isl_access_type i;
1268 if (!access)
1269 return NULL;
1271 for (i = isl_access_sink; i < isl_access_end; ++i)
1272 isl_union_map_free(access->access[i]);
1273 isl_schedule_free(access->schedule);
1274 isl_union_map_free(access->schedule_map);
1275 free(access);
1277 return NULL;
1280 /* Return the isl_ctx to which "access" belongs.
1282 isl_ctx *isl_union_access_info_get_ctx(__isl_keep isl_union_access_info *access)
1284 if (!access)
1285 return NULL;
1286 return isl_union_map_get_ctx(access->access[isl_access_sink]);
1289 /* Construct an empty (invalid) isl_union_access_info object.
1290 * The caller is responsible for setting the sink access relation and
1291 * initializing all the other fields, e.g., by calling
1292 * isl_union_access_info_init.
1294 static __isl_give isl_union_access_info *isl_union_access_info_alloc(
1295 isl_ctx *ctx)
1297 return isl_calloc_type(ctx, isl_union_access_info);
1300 /* Initialize all the fields of "info", except the sink access relation,
1301 * which is assumed to have been set by the caller.
1303 * By default, we use the schedule field of the isl_union_access_info,
1304 * but this may be overridden by a call
1305 * to isl_union_access_info_set_schedule_map.
1307 static __isl_give isl_union_access_info *isl_union_access_info_init(
1308 __isl_take isl_union_access_info *info)
1310 isl_space *space;
1311 isl_union_map *empty;
1312 enum isl_access_type i;
1314 if (!info)
1315 return NULL;
1316 if (!info->access[isl_access_sink])
1317 return isl_union_access_info_free(info);
1319 space = isl_union_map_get_space(info->access[isl_access_sink]);
1320 empty = isl_union_map_empty(isl_space_copy(space));
1321 for (i = isl_access_sink + 1; i < isl_access_end; ++i)
1322 if (!info->access[i])
1323 info->access[i] = isl_union_map_copy(empty);
1324 isl_union_map_free(empty);
1325 if (!info->schedule && !info->schedule_map)
1326 info->schedule = isl_schedule_empty(isl_space_copy(space));
1327 isl_space_free(space);
1329 for (i = isl_access_sink + 1; i < isl_access_end; ++i)
1330 if (!info->access[i])
1331 return isl_union_access_info_free(info);
1332 if (!info->schedule && !info->schedule_map)
1333 return isl_union_access_info_free(info);
1335 return info;
1338 /* Create a new isl_union_access_info with the given sink accesses and
1339 * and no other accesses or schedule information.
1341 __isl_give isl_union_access_info *isl_union_access_info_from_sink(
1342 __isl_take isl_union_map *sink)
1344 isl_ctx *ctx;
1345 isl_union_access_info *access;
1347 if (!sink)
1348 return NULL;
1349 ctx = isl_union_map_get_ctx(sink);
1350 access = isl_union_access_info_alloc(ctx);
1351 if (!access)
1352 goto error;
1353 access->access[isl_access_sink] = sink;
1354 return isl_union_access_info_init(access);
1355 error:
1356 isl_union_map_free(sink);
1357 return NULL;
1360 /* Replace the access relation of type "type" of "info" by "access".
1362 static __isl_give isl_union_access_info *isl_union_access_info_set(
1363 __isl_take isl_union_access_info *info,
1364 enum isl_access_type type, __isl_take isl_union_map *access)
1366 if (!info || !access)
1367 goto error;
1369 isl_union_map_free(info->access[type]);
1370 info->access[type] = access;
1372 return info;
1373 error:
1374 isl_union_access_info_free(info);
1375 isl_union_map_free(access);
1376 return NULL;
1379 /* Replace the definite source accesses of "access" by "must_source".
1381 __isl_give isl_union_access_info *isl_union_access_info_set_must_source(
1382 __isl_take isl_union_access_info *access,
1383 __isl_take isl_union_map *must_source)
1385 return isl_union_access_info_set(access, isl_access_must_source,
1386 must_source);
1389 /* Replace the possible source accesses of "access" by "may_source".
1391 __isl_give isl_union_access_info *isl_union_access_info_set_may_source(
1392 __isl_take isl_union_access_info *access,
1393 __isl_take isl_union_map *may_source)
1395 return isl_union_access_info_set(access, isl_access_may_source,
1396 may_source);
1399 /* Replace the schedule of "access" by "schedule".
1400 * Also free the schedule_map in case it was set last.
1402 __isl_give isl_union_access_info *isl_union_access_info_set_schedule(
1403 __isl_take isl_union_access_info *access,
1404 __isl_take isl_schedule *schedule)
1406 if (!access || !schedule)
1407 goto error;
1409 access->schedule_map = isl_union_map_free(access->schedule_map);
1410 isl_schedule_free(access->schedule);
1411 access->schedule = schedule;
1413 return access;
1414 error:
1415 isl_union_access_info_free(access);
1416 isl_schedule_free(schedule);
1417 return NULL;
1420 /* Replace the schedule map of "access" by "schedule_map".
1421 * Also free the schedule in case it was set last.
1423 __isl_give isl_union_access_info *isl_union_access_info_set_schedule_map(
1424 __isl_take isl_union_access_info *access,
1425 __isl_take isl_union_map *schedule_map)
1427 if (!access || !schedule_map)
1428 goto error;
1430 isl_union_map_free(access->schedule_map);
1431 access->schedule = isl_schedule_free(access->schedule);
1432 access->schedule_map = schedule_map;
1434 return access;
1435 error:
1436 isl_union_access_info_free(access);
1437 isl_union_map_free(schedule_map);
1438 return NULL;
1441 __isl_give isl_union_access_info *isl_union_access_info_copy(
1442 __isl_keep isl_union_access_info *access)
1444 isl_union_access_info *copy;
1445 enum isl_access_type i;
1447 if (!access)
1448 return NULL;
1449 copy = isl_union_access_info_from_sink(
1450 isl_union_map_copy(access->access[isl_access_sink]));
1451 for (i = isl_access_sink + 1; i < isl_access_end; ++i)
1452 copy = isl_union_access_info_set(copy, i,
1453 isl_union_map_copy(access->access[i]));
1454 if (access->schedule)
1455 copy = isl_union_access_info_set_schedule(copy,
1456 isl_schedule_copy(access->schedule));
1457 else
1458 copy = isl_union_access_info_set_schedule_map(copy,
1459 isl_union_map_copy(access->schedule_map));
1461 return copy;
1464 /* Print a key-value pair of a YAML mapping to "p",
1465 * with key "name" and value "umap".
1467 static __isl_give isl_printer *print_union_map_field(__isl_take isl_printer *p,
1468 const char *name, __isl_keep isl_union_map *umap)
1470 p = isl_printer_print_str(p, name);
1471 p = isl_printer_yaml_next(p);
1472 p = isl_printer_print_str(p, "\"");
1473 p = isl_printer_print_union_map(p, umap);
1474 p = isl_printer_print_str(p, "\"");
1475 p = isl_printer_yaml_next(p);
1477 return p;
1480 /* An enumeration of the various keys that may appear in a YAML mapping
1481 * of an isl_union_access_info object.
1482 * The keys for the access relation types are assumed to have the same values
1483 * as the access relation types in isl_access_type.
1485 enum isl_ai_key {
1486 isl_ai_key_error = -1,
1487 isl_ai_key_sink = isl_access_sink,
1488 isl_ai_key_must_source = isl_access_must_source,
1489 isl_ai_key_may_source = isl_access_may_source,
1490 isl_ai_key_schedule_map,
1491 isl_ai_key_schedule,
1492 isl_ai_key_end
1495 /* Textual representations of the YAML keys for an isl_union_access_info
1496 * object.
1498 static char *key_str[] = {
1499 [isl_ai_key_sink] = "sink",
1500 [isl_ai_key_must_source] = "must_source",
1501 [isl_ai_key_may_source] = "may_source",
1502 [isl_ai_key_schedule_map] = "schedule_map",
1503 [isl_ai_key_schedule] = "schedule",
1506 /* Print a key-value pair corresponding to the access relation of type "type"
1507 * of a YAML mapping of "info" to "p".
1509 * The sink access relation is always printed, but any other access relation
1510 * is only printed if it is non-empty.
1512 static __isl_give isl_printer *print_access_field(__isl_take isl_printer *p,
1513 __isl_keep isl_union_access_info *info, enum isl_access_type type)
1515 if (type != isl_access_sink) {
1516 isl_bool empty;
1518 empty = isl_union_map_is_empty(info->access[type]);
1519 if (empty < 0)
1520 return isl_printer_free(p);
1521 if (empty)
1522 return p;
1524 return print_union_map_field(p, key_str[type], info->access[type]);
1527 /* Print the information contained in "access" to "p".
1528 * The information is printed as a YAML document.
1530 __isl_give isl_printer *isl_printer_print_union_access_info(
1531 __isl_take isl_printer *p, __isl_keep isl_union_access_info *access)
1533 enum isl_access_type i;
1535 if (!access)
1536 return isl_printer_free(p);
1538 p = isl_printer_yaml_start_mapping(p);
1539 for (i = isl_access_sink; i < isl_access_end; ++i)
1540 p = print_access_field(p, access, i);
1541 if (access->schedule) {
1542 p = isl_printer_print_str(p, key_str[isl_ai_key_schedule]);
1543 p = isl_printer_yaml_next(p);
1544 p = isl_printer_print_schedule(p, access->schedule);
1545 p = isl_printer_yaml_next(p);
1546 } else {
1547 p = print_union_map_field(p, key_str[isl_ai_key_schedule_map],
1548 access->schedule_map);
1550 p = isl_printer_yaml_end_mapping(p);
1552 return p;
1555 /* Return a string representation of the information in "access".
1556 * The information is printed in flow format.
1558 __isl_give char *isl_union_access_info_to_str(
1559 __isl_keep isl_union_access_info *access)
1561 isl_printer *p;
1562 char *s;
1564 if (!access)
1565 return NULL;
1567 p = isl_printer_to_str(isl_union_access_info_get_ctx(access));
1568 p = isl_printer_set_yaml_style(p, ISL_YAML_STYLE_FLOW);
1569 p = isl_printer_print_union_access_info(p, access);
1570 s = isl_printer_get_str(p);
1571 isl_printer_free(p);
1573 return s;
1576 #undef KEY
1577 #define KEY enum isl_ai_key
1578 #undef KEY_ERROR
1579 #define KEY_ERROR isl_ai_key_error
1580 #undef KEY_END
1581 #define KEY_END isl_ai_key_end
1582 #include "extract_key.c"
1584 #undef BASE
1585 #define BASE union_map
1586 #include "read_in_string_templ.c"
1588 /* Read an isl_union_access_info object from "s".
1590 * Start off with an empty (invalid) isl_union_access_info object and
1591 * then fill up the fields based on the input.
1592 * The input needs to contain at least a description of the sink
1593 * access relation as well as some form of schedule.
1594 * The other access relations are set to empty relations
1595 * by isl_union_access_info_init if they are not specified in the input.
1597 __isl_give isl_union_access_info *isl_stream_read_union_access_info(
1598 isl_stream *s)
1600 isl_ctx *ctx;
1601 isl_union_access_info *info;
1602 int more;
1603 int sink_set = 0;
1604 int schedule_set = 0;
1606 if (isl_stream_yaml_read_start_mapping(s))
1607 return NULL;
1609 ctx = isl_stream_get_ctx(s);
1610 info = isl_union_access_info_alloc(ctx);
1611 while ((more = isl_stream_yaml_next(s)) > 0) {
1612 enum isl_ai_key key;
1613 isl_union_map *access, *schedule_map;
1614 isl_schedule *schedule;
1616 key = get_key(s);
1617 if (isl_stream_yaml_next(s) < 0)
1618 return isl_union_access_info_free(info);
1619 switch (key) {
1620 case isl_ai_key_end:
1621 case isl_ai_key_error:
1622 return isl_union_access_info_free(info);
1623 case isl_ai_key_sink:
1624 sink_set = 1;
1625 case isl_ai_key_must_source:
1626 case isl_ai_key_may_source:
1627 access = read_union_map(s);
1628 info = isl_union_access_info_set(info, key, access);
1629 if (!info)
1630 return NULL;
1631 break;
1632 case isl_ai_key_schedule_map:
1633 schedule_set = 1;
1634 schedule_map = read_union_map(s);
1635 info = isl_union_access_info_set_schedule_map(info,
1636 schedule_map);
1637 if (!info)
1638 return NULL;
1639 break;
1640 case isl_ai_key_schedule:
1641 schedule_set = 1;
1642 schedule = isl_stream_read_schedule(s);
1643 info = isl_union_access_info_set_schedule(info,
1644 schedule);
1645 if (!info)
1646 return NULL;
1647 break;
1650 if (more < 0)
1651 return isl_union_access_info_free(info);
1653 if (isl_stream_yaml_read_end_mapping(s) < 0) {
1654 isl_stream_error(s, NULL, "unexpected extra elements");
1655 return isl_union_access_info_free(info);
1658 if (!sink_set) {
1659 isl_stream_error(s, NULL, "no sink specified");
1660 return isl_union_access_info_free(info);
1663 if (!schedule_set) {
1664 isl_stream_error(s, NULL, "no schedule specified");
1665 return isl_union_access_info_free(info);
1668 return isl_union_access_info_init(info);
1671 /* Read an isl_union_access_info object from the file "input".
1673 __isl_give isl_union_access_info *isl_union_access_info_read_from_file(
1674 isl_ctx *ctx, FILE *input)
1676 isl_stream *s;
1677 isl_union_access_info *access;
1679 s = isl_stream_new_file(ctx, input);
1680 if (!s)
1681 return NULL;
1682 access = isl_stream_read_union_access_info(s);
1683 isl_stream_free(s);
1685 return access;
1688 /* Update the fields of "access" such that they all have the same parameters,
1689 * keeping in mind that the schedule_map field may be NULL and ignoring
1690 * the schedule field.
1692 static __isl_give isl_union_access_info *isl_union_access_info_align_params(
1693 __isl_take isl_union_access_info *access)
1695 isl_space *space;
1696 enum isl_access_type i;
1698 if (!access)
1699 return NULL;
1701 space = isl_union_map_get_space(access->access[isl_access_sink]);
1702 for (i = isl_access_sink + 1; i < isl_access_end; ++i)
1703 space = isl_space_align_params(space,
1704 isl_union_map_get_space(access->access[i]));
1705 if (access->schedule_map)
1706 space = isl_space_align_params(space,
1707 isl_union_map_get_space(access->schedule_map));
1708 for (i = isl_access_sink; i < isl_access_end; ++i)
1709 access->access[i] =
1710 isl_union_map_align_params(access->access[i],
1711 isl_space_copy(space));
1712 if (!access->schedule_map) {
1713 isl_space_free(space);
1714 } else {
1715 access->schedule_map =
1716 isl_union_map_align_params(access->schedule_map, space);
1717 if (!access->schedule_map)
1718 return isl_union_access_info_free(access);
1721 for (i = isl_access_sink; i < isl_access_end; ++i)
1722 if (!access->access[i])
1723 return isl_union_access_info_free(access);
1725 return access;
1728 /* Prepend the schedule dimensions to the iteration domains.
1730 * That is, if the schedule is of the form
1732 * D -> S
1734 * while the access relations are of the form
1736 * D -> A
1738 * then the updated access relations are of the form
1740 * [S -> D] -> A
1742 * The schedule map is also replaced by the map
1744 * [S -> D] -> D
1746 * that is used during the internal computation.
1747 * Neither the original schedule map nor this updated schedule map
1748 * are used after the call to this function.
1750 static __isl_give isl_union_access_info *
1751 isl_union_access_info_introduce_schedule(
1752 __isl_take isl_union_access_info *access)
1754 isl_union_map *sm;
1755 enum isl_access_type i;
1757 if (!access)
1758 return NULL;
1760 sm = isl_union_map_reverse(access->schedule_map);
1761 sm = isl_union_map_range_map(sm);
1762 for (i = isl_access_sink; i < isl_access_end; ++i)
1763 access->access[i] =
1764 isl_union_map_apply_range(isl_union_map_copy(sm),
1765 access->access[i]);
1766 access->schedule_map = sm;
1768 for (i = isl_access_sink; i < isl_access_end; ++i)
1769 if (!access->access[i])
1770 return isl_union_access_info_free(access);
1771 if (!access->schedule_map)
1772 return isl_union_access_info_free(access);
1774 return access;
1777 /* This structure represents the result of a dependence analysis computation.
1779 * "must_dep" represents the full definite dependences
1780 * "may_dep" represents the full non-definite dependences.
1781 * Both are of the form
1783 * [Source] -> [[Sink -> Data]]
1785 * (after the schedule dimensions have been projected out).
1786 * "must_no_source" represents the subset of the sink accesses for which
1787 * definitely no source was found.
1788 * "may_no_source" represents the subset of the sink accesses for which
1789 * possibly, but not definitely, no source was found.
1791 struct isl_union_flow {
1792 isl_union_map *must_dep;
1793 isl_union_map *may_dep;
1794 isl_union_map *must_no_source;
1795 isl_union_map *may_no_source;
1798 /* Return the isl_ctx to which "flow" belongs.
1800 isl_ctx *isl_union_flow_get_ctx(__isl_keep isl_union_flow *flow)
1802 return flow ? isl_union_map_get_ctx(flow->must_dep) : NULL;
1805 /* Free "flow" and return NULL.
1807 __isl_null isl_union_flow *isl_union_flow_free(__isl_take isl_union_flow *flow)
1809 if (!flow)
1810 return NULL;
1811 isl_union_map_free(flow->must_dep);
1812 isl_union_map_free(flow->may_dep);
1813 isl_union_map_free(flow->must_no_source);
1814 isl_union_map_free(flow->may_no_source);
1815 free(flow);
1816 return NULL;
1819 void isl_union_flow_dump(__isl_keep isl_union_flow *flow)
1821 if (!flow)
1822 return;
1824 fprintf(stderr, "must dependences: ");
1825 isl_union_map_dump(flow->must_dep);
1826 fprintf(stderr, "may dependences: ");
1827 isl_union_map_dump(flow->may_dep);
1828 fprintf(stderr, "must no source: ");
1829 isl_union_map_dump(flow->must_no_source);
1830 fprintf(stderr, "may no source: ");
1831 isl_union_map_dump(flow->may_no_source);
1834 /* Return the full definite dependences in "flow", with accessed elements.
1836 __isl_give isl_union_map *isl_union_flow_get_full_must_dependence(
1837 __isl_keep isl_union_flow *flow)
1839 if (!flow)
1840 return NULL;
1841 return isl_union_map_copy(flow->must_dep);
1844 /* Return the full possible dependences in "flow", including the definite
1845 * dependences, with accessed elements.
1847 __isl_give isl_union_map *isl_union_flow_get_full_may_dependence(
1848 __isl_keep isl_union_flow *flow)
1850 if (!flow)
1851 return NULL;
1852 return isl_union_map_union(isl_union_map_copy(flow->must_dep),
1853 isl_union_map_copy(flow->may_dep));
1856 /* Return the definite dependences in "flow", without the accessed elements.
1858 __isl_give isl_union_map *isl_union_flow_get_must_dependence(
1859 __isl_keep isl_union_flow *flow)
1861 isl_union_map *dep;
1863 if (!flow)
1864 return NULL;
1865 dep = isl_union_map_copy(flow->must_dep);
1866 return isl_union_map_range_factor_domain(dep);
1869 /* Return the possible dependences in "flow", including the definite
1870 * dependences, without the accessed elements.
1872 __isl_give isl_union_map *isl_union_flow_get_may_dependence(
1873 __isl_keep isl_union_flow *flow)
1875 isl_union_map *dep;
1877 if (!flow)
1878 return NULL;
1879 dep = isl_union_map_union(isl_union_map_copy(flow->must_dep),
1880 isl_union_map_copy(flow->may_dep));
1881 return isl_union_map_range_factor_domain(dep);
1884 /* Return the non-definite dependences in "flow".
1886 static __isl_give isl_union_map *isl_union_flow_get_non_must_dependence(
1887 __isl_keep isl_union_flow *flow)
1889 if (!flow)
1890 return NULL;
1891 return isl_union_map_copy(flow->may_dep);
1894 /* Return the subset of the sink accesses for which definitely
1895 * no source was found.
1897 __isl_give isl_union_map *isl_union_flow_get_must_no_source(
1898 __isl_keep isl_union_flow *flow)
1900 if (!flow)
1901 return NULL;
1902 return isl_union_map_copy(flow->must_no_source);
1905 /* Return the subset of the sink accesses for which possibly
1906 * no source was found, including those for which definitely
1907 * no source was found.
1909 __isl_give isl_union_map *isl_union_flow_get_may_no_source(
1910 __isl_keep isl_union_flow *flow)
1912 if (!flow)
1913 return NULL;
1914 return isl_union_map_union(isl_union_map_copy(flow->must_no_source),
1915 isl_union_map_copy(flow->may_no_source));
1918 /* Return the subset of the sink accesses for which possibly, but not
1919 * definitely, no source was found.
1921 static __isl_give isl_union_map *isl_union_flow_get_non_must_no_source(
1922 __isl_keep isl_union_flow *flow)
1924 if (!flow)
1925 return NULL;
1926 return isl_union_map_copy(flow->may_no_source);
1929 /* Create a new isl_union_flow object, initialized with empty
1930 * dependence relations and sink subsets.
1932 static __isl_give isl_union_flow *isl_union_flow_alloc(
1933 __isl_take isl_space *space)
1935 isl_ctx *ctx;
1936 isl_union_map *empty;
1937 isl_union_flow *flow;
1939 if (!space)
1940 return NULL;
1941 ctx = isl_space_get_ctx(space);
1942 flow = isl_alloc_type(ctx, isl_union_flow);
1943 if (!flow)
1944 goto error;
1946 empty = isl_union_map_empty(space);
1947 flow->must_dep = isl_union_map_copy(empty);
1948 flow->may_dep = isl_union_map_copy(empty);
1949 flow->must_no_source = isl_union_map_copy(empty);
1950 flow->may_no_source = empty;
1952 if (!flow->must_dep || !flow->may_dep ||
1953 !flow->must_no_source || !flow->may_no_source)
1954 return isl_union_flow_free(flow);
1956 return flow;
1957 error:
1958 isl_space_free(space);
1959 return NULL;
1962 /* Copy this isl_union_flow object.
1964 __isl_give isl_union_flow *isl_union_flow_copy(__isl_keep isl_union_flow *flow)
1966 isl_union_flow *copy;
1968 if (!flow)
1969 return NULL;
1971 copy = isl_union_flow_alloc(isl_union_map_get_space(flow->must_dep));
1973 if (!copy)
1974 return NULL;
1976 copy->must_dep = isl_union_map_union(copy->must_dep,
1977 isl_union_map_copy(flow->must_dep));
1978 copy->may_dep = isl_union_map_union(copy->may_dep,
1979 isl_union_map_copy(flow->may_dep));
1980 copy->must_no_source = isl_union_map_union(copy->must_no_source,
1981 isl_union_map_copy(flow->must_no_source));
1982 copy->may_no_source = isl_union_map_union(copy->may_no_source,
1983 isl_union_map_copy(flow->may_no_source));
1985 if (!copy->must_dep || !copy->may_dep ||
1986 !copy->must_no_source || !copy->may_no_source)
1987 return isl_union_flow_free(copy);
1989 return copy;
1992 /* Drop the schedule dimensions from the iteration domains in "flow".
1993 * In particular, the schedule dimensions have been prepended
1994 * to the iteration domains prior to the dependence analysis by
1995 * replacing the iteration domain D, by the wrapped map [S -> D].
1996 * Replace these wrapped maps by the original D.
1998 * In particular, the dependences computed by access_info_compute_flow_core
1999 * are of the form
2001 * [S -> D] -> [[S' -> D'] -> A]
2003 * The schedule dimensions are projected out by first currying the range,
2004 * resulting in
2006 * [S -> D] -> [S' -> [D' -> A]]
2008 * and then computing the factor range
2010 * D -> [D' -> A]
2012 static __isl_give isl_union_flow *isl_union_flow_drop_schedule(
2013 __isl_take isl_union_flow *flow)
2015 if (!flow)
2016 return NULL;
2018 flow->must_dep = isl_union_map_range_curry(flow->must_dep);
2019 flow->must_dep = isl_union_map_factor_range(flow->must_dep);
2020 flow->may_dep = isl_union_map_range_curry(flow->may_dep);
2021 flow->may_dep = isl_union_map_factor_range(flow->may_dep);
2022 flow->must_no_source =
2023 isl_union_map_domain_factor_range(flow->must_no_source);
2024 flow->may_no_source =
2025 isl_union_map_domain_factor_range(flow->may_no_source);
2027 if (!flow->must_dep || !flow->may_dep ||
2028 !flow->must_no_source || !flow->may_no_source)
2029 return isl_union_flow_free(flow);
2031 return flow;
2034 struct isl_compute_flow_data {
2035 isl_union_map *must_source;
2036 isl_union_map *may_source;
2037 isl_union_flow *flow;
2039 int count;
2040 int must;
2041 isl_space *dim;
2042 struct isl_sched_info *sink_info;
2043 struct isl_sched_info **source_info;
2044 isl_access_info *accesses;
2047 static isl_stat count_matching_array(__isl_take isl_map *map, void *user)
2049 int eq;
2050 isl_space *dim;
2051 struct isl_compute_flow_data *data;
2053 data = (struct isl_compute_flow_data *)user;
2055 dim = isl_space_range(isl_map_get_space(map));
2057 eq = isl_space_is_equal(dim, data->dim);
2059 isl_space_free(dim);
2060 isl_map_free(map);
2062 if (eq < 0)
2063 return isl_stat_error;
2064 if (eq)
2065 data->count++;
2067 return isl_stat_ok;
2070 static isl_stat collect_matching_array(__isl_take isl_map *map, void *user)
2072 int eq;
2073 isl_space *dim;
2074 struct isl_sched_info *info;
2075 struct isl_compute_flow_data *data;
2077 data = (struct isl_compute_flow_data *)user;
2079 dim = isl_space_range(isl_map_get_space(map));
2081 eq = isl_space_is_equal(dim, data->dim);
2083 isl_space_free(dim);
2085 if (eq < 0)
2086 goto error;
2087 if (!eq) {
2088 isl_map_free(map);
2089 return isl_stat_ok;
2092 info = sched_info_alloc(map);
2093 data->source_info[data->count] = info;
2095 data->accesses = isl_access_info_add_source(data->accesses,
2096 map, data->must, info);
2098 data->count++;
2100 return isl_stat_ok;
2101 error:
2102 isl_map_free(map);
2103 return isl_stat_error;
2106 /* Determine the shared nesting level and the "textual order" of
2107 * the given accesses.
2109 * We first determine the minimal schedule dimension for both accesses.
2111 * If among those dimensions, we can find one where both have a fixed
2112 * value and if moreover those values are different, then the previous
2113 * dimension is the last shared nesting level and the textual order
2114 * is determined based on the order of the fixed values.
2115 * If no such fixed values can be found, then we set the shared
2116 * nesting level to the minimal schedule dimension, with no textual ordering.
2118 static int before(void *first, void *second)
2120 struct isl_sched_info *info1 = first;
2121 struct isl_sched_info *info2 = second;
2122 int n1, n2;
2123 int i;
2125 n1 = isl_vec_size(info1->cst);
2126 n2 = isl_vec_size(info2->cst);
2128 if (n2 < n1)
2129 n1 = n2;
2131 for (i = 0; i < n1; ++i) {
2132 int r;
2133 int cmp;
2135 if (!info1->is_cst[i])
2136 continue;
2137 if (!info2->is_cst[i])
2138 continue;
2139 cmp = isl_vec_cmp_element(info1->cst, info2->cst, i);
2140 if (cmp == 0)
2141 continue;
2143 r = 2 * i + (cmp < 0);
2145 return r;
2148 return 2 * n1;
2151 /* Given a sink access, look for all the source accesses that access
2152 * the same array and perform dataflow analysis on them using
2153 * isl_access_info_compute_flow_core.
2155 static isl_stat compute_flow(__isl_take isl_map *map, void *user)
2157 int i;
2158 isl_ctx *ctx;
2159 struct isl_compute_flow_data *data;
2160 isl_flow *flow;
2161 isl_union_flow *df;
2163 data = (struct isl_compute_flow_data *)user;
2164 df = data->flow;
2166 ctx = isl_map_get_ctx(map);
2168 data->accesses = NULL;
2169 data->sink_info = NULL;
2170 data->source_info = NULL;
2171 data->count = 0;
2172 data->dim = isl_space_range(isl_map_get_space(map));
2174 if (isl_union_map_foreach_map(data->must_source,
2175 &count_matching_array, data) < 0)
2176 goto error;
2177 if (isl_union_map_foreach_map(data->may_source,
2178 &count_matching_array, data) < 0)
2179 goto error;
2181 data->sink_info = sched_info_alloc(map);
2182 data->source_info = isl_calloc_array(ctx, struct isl_sched_info *,
2183 data->count);
2185 data->accesses = isl_access_info_alloc(isl_map_copy(map),
2186 data->sink_info, &before, data->count);
2187 if (!data->sink_info || (data->count && !data->source_info) ||
2188 !data->accesses)
2189 goto error;
2190 data->count = 0;
2191 data->must = 1;
2192 if (isl_union_map_foreach_map(data->must_source,
2193 &collect_matching_array, data) < 0)
2194 goto error;
2195 data->must = 0;
2196 if (isl_union_map_foreach_map(data->may_source,
2197 &collect_matching_array, data) < 0)
2198 goto error;
2200 flow = access_info_compute_flow_core(data->accesses);
2201 data->accesses = NULL;
2203 if (!flow)
2204 goto error;
2206 df->must_no_source = isl_union_map_union(df->must_no_source,
2207 isl_union_map_from_map(isl_flow_get_no_source(flow, 1)));
2208 df->may_no_source = isl_union_map_union(df->may_no_source,
2209 isl_union_map_from_map(isl_flow_get_no_source(flow, 0)));
2211 for (i = 0; i < flow->n_source; ++i) {
2212 isl_union_map *dep;
2213 dep = isl_union_map_from_map(isl_map_copy(flow->dep[i].map));
2214 if (flow->dep[i].must)
2215 df->must_dep = isl_union_map_union(df->must_dep, dep);
2216 else
2217 df->may_dep = isl_union_map_union(df->may_dep, dep);
2220 isl_flow_free(flow);
2222 sched_info_free(data->sink_info);
2223 if (data->source_info) {
2224 for (i = 0; i < data->count; ++i)
2225 sched_info_free(data->source_info[i]);
2226 free(data->source_info);
2228 isl_space_free(data->dim);
2229 isl_map_free(map);
2231 return isl_stat_ok;
2232 error:
2233 isl_access_info_free(data->accesses);
2234 sched_info_free(data->sink_info);
2235 if (data->source_info) {
2236 for (i = 0; i < data->count; ++i)
2237 sched_info_free(data->source_info[i]);
2238 free(data->source_info);
2240 isl_space_free(data->dim);
2241 isl_map_free(map);
2243 return isl_stat_error;
2246 /* Remove the must accesses from the may accesses.
2248 * A must access always trumps a may access, so there is no need
2249 * for a must access to also be considered as a may access. Doing so
2250 * would only cost extra computations only to find out that
2251 * the duplicated may access does not make any difference.
2253 static __isl_give isl_union_access_info *isl_union_access_info_normalize(
2254 __isl_take isl_union_access_info *access)
2256 if (!access)
2257 return NULL;
2258 access->access[isl_access_may_source] =
2259 isl_union_map_subtract(access->access[isl_access_may_source],
2260 isl_union_map_copy(access->access[isl_access_must_source]));
2261 if (!access->access[isl_access_may_source])
2262 return isl_union_access_info_free(access);
2264 return access;
2267 /* Given a description of the "sink" accesses, the "source" accesses and
2268 * a schedule, compute for each instance of a sink access
2269 * and for each element accessed by that instance,
2270 * the possible or definite source accesses that last accessed the
2271 * element accessed by the sink access before this sink access
2272 * in the sense that there is no intermediate definite source access.
2274 * The must_no_source and may_no_source elements of the result
2275 * are subsets of access->sink. The elements must_dep and may_dep
2276 * map domain elements of access->{may,must)_source to
2277 * domain elements of access->sink.
2279 * This function is used when only the schedule map representation
2280 * is available.
2282 * We first prepend the schedule dimensions to the domain
2283 * of the accesses so that we can easily compare their relative order.
2284 * Then we consider each sink access individually in compute_flow.
2286 static __isl_give isl_union_flow *compute_flow_union_map(
2287 __isl_take isl_union_access_info *access)
2289 struct isl_compute_flow_data data;
2290 isl_union_map *sink;
2292 access = isl_union_access_info_align_params(access);
2293 access = isl_union_access_info_introduce_schedule(access);
2294 if (!access)
2295 return NULL;
2297 data.must_source = access->access[isl_access_must_source];
2298 data.may_source = access->access[isl_access_may_source];
2300 sink = access->access[isl_access_sink];
2301 data.flow = isl_union_flow_alloc(isl_union_map_get_space(sink));
2303 if (isl_union_map_foreach_map(sink, &compute_flow, &data) < 0)
2304 goto error;
2306 data.flow = isl_union_flow_drop_schedule(data.flow);
2308 isl_union_access_info_free(access);
2309 return data.flow;
2310 error:
2311 isl_union_access_info_free(access);
2312 isl_union_flow_free(data.flow);
2313 return NULL;
2316 /* A schedule access relation.
2318 * The access relation "access" is of the form [S -> D] -> A,
2319 * where S corresponds to the prefix schedule at "node".
2320 * "must" is only relevant for source accesses and indicates
2321 * whether the access is a must source or a may source.
2323 struct isl_scheduled_access {
2324 isl_map *access;
2325 int must;
2326 isl_schedule_node *node;
2329 /* Data structure for keeping track of individual scheduled sink and source
2330 * accesses when computing dependence analysis based on a schedule tree.
2332 * "n_sink" is the number of used entries in "sink"
2333 * "n_source" is the number of used entries in "source"
2335 * "set_sink", "must" and "node" are only used inside collect_sink_source,
2336 * to keep track of the current node and
2337 * of what extract_sink_source needs to do.
2339 struct isl_compute_flow_schedule_data {
2340 isl_union_access_info *access;
2342 int n_sink;
2343 int n_source;
2345 struct isl_scheduled_access *sink;
2346 struct isl_scheduled_access *source;
2348 int set_sink;
2349 int must;
2350 isl_schedule_node *node;
2353 /* Align the parameters of all sinks with all sources.
2355 * If there are no sinks or no sources, then no alignment is needed.
2357 static void isl_compute_flow_schedule_data_align_params(
2358 struct isl_compute_flow_schedule_data *data)
2360 int i;
2361 isl_space *space;
2363 if (data->n_sink == 0 || data->n_source == 0)
2364 return;
2366 space = isl_map_get_space(data->sink[0].access);
2368 for (i = 1; i < data->n_sink; ++i)
2369 space = isl_space_align_params(space,
2370 isl_map_get_space(data->sink[i].access));
2371 for (i = 0; i < data->n_source; ++i)
2372 space = isl_space_align_params(space,
2373 isl_map_get_space(data->source[i].access));
2375 for (i = 0; i < data->n_sink; ++i)
2376 data->sink[i].access =
2377 isl_map_align_params(data->sink[i].access,
2378 isl_space_copy(space));
2379 for (i = 0; i < data->n_source; ++i)
2380 data->source[i].access =
2381 isl_map_align_params(data->source[i].access,
2382 isl_space_copy(space));
2384 isl_space_free(space);
2387 /* Free all the memory referenced from "data".
2388 * Do not free "data" itself as it may be allocated on the stack.
2390 static void isl_compute_flow_schedule_data_clear(
2391 struct isl_compute_flow_schedule_data *data)
2393 int i;
2395 if (!data->sink)
2396 return;
2398 for (i = 0; i < data->n_sink; ++i) {
2399 isl_map_free(data->sink[i].access);
2400 isl_schedule_node_free(data->sink[i].node);
2403 for (i = 0; i < data->n_source; ++i) {
2404 isl_map_free(data->source[i].access);
2405 isl_schedule_node_free(data->source[i].node);
2408 free(data->sink);
2411 /* isl_schedule_foreach_schedule_node_top_down callback for counting
2412 * (an upper bound on) the number of sinks and sources.
2414 * Sinks and sources are only extracted at leaves of the tree,
2415 * so we skip the node if it is not a leaf.
2416 * Otherwise we increment data->n_sink and data->n_source with
2417 * the number of spaces in the sink and source access domains
2418 * that reach this node.
2420 static isl_bool count_sink_source(__isl_keep isl_schedule_node *node,
2421 void *user)
2423 struct isl_compute_flow_schedule_data *data = user;
2424 isl_union_set *domain;
2425 isl_union_map *umap;
2426 isl_bool r = isl_bool_false;
2428 if (isl_schedule_node_get_type(node) != isl_schedule_node_leaf)
2429 return isl_bool_true;
2431 domain = isl_schedule_node_get_universe_domain(node);
2433 umap = isl_union_map_copy(data->access->access[isl_access_sink]);
2434 umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(domain));
2435 data->n_sink += isl_union_map_n_map(umap);
2436 isl_union_map_free(umap);
2437 if (!umap)
2438 r = isl_bool_error;
2440 umap = isl_union_map_copy(data->access->access[isl_access_must_source]);
2441 umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(domain));
2442 data->n_source += isl_union_map_n_map(umap);
2443 isl_union_map_free(umap);
2444 if (!umap)
2445 r = isl_bool_error;
2447 umap = isl_union_map_copy(data->access->access[isl_access_may_source]);
2448 umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(domain));
2449 data->n_source += isl_union_map_n_map(umap);
2450 isl_union_map_free(umap);
2451 if (!umap)
2452 r = isl_bool_error;
2454 isl_union_set_free(domain);
2456 return r;
2459 /* Add a single scheduled sink or source (depending on data->set_sink)
2460 * with scheduled access relation "map", must property data->must and
2461 * schedule node data->node to the list of sinks or sources.
2463 static isl_stat extract_sink_source(__isl_take isl_map *map, void *user)
2465 struct isl_compute_flow_schedule_data *data = user;
2466 struct isl_scheduled_access *access;
2468 if (data->set_sink)
2469 access = data->sink + data->n_sink++;
2470 else
2471 access = data->source + data->n_source++;
2473 access->access = map;
2474 access->must = data->must;
2475 access->node = isl_schedule_node_copy(data->node);
2477 return isl_stat_ok;
2480 /* isl_schedule_foreach_schedule_node_top_down callback for collecting
2481 * individual scheduled source and sink accesses (taking into account
2482 * the domain of the schedule).
2484 * We only collect accesses at the leaves of the schedule tree.
2485 * We prepend the schedule dimensions at the leaf to the iteration
2486 * domains of the source and sink accesses and then extract
2487 * the individual accesses (per space).
2489 * In particular, if the prefix schedule at the node is of the form
2491 * D -> S
2493 * while the access relations are of the form
2495 * D -> A
2497 * then the updated access relations are of the form
2499 * [S -> D] -> A
2501 * Note that S consists of a single space such that introducing S
2502 * in the access relations does not increase the number of spaces.
2504 static isl_bool collect_sink_source(__isl_keep isl_schedule_node *node,
2505 void *user)
2507 struct isl_compute_flow_schedule_data *data = user;
2508 isl_union_map *prefix;
2509 isl_union_map *umap;
2510 isl_bool r = isl_bool_false;
2512 if (isl_schedule_node_get_type(node) != isl_schedule_node_leaf)
2513 return isl_bool_true;
2515 data->node = node;
2517 prefix = isl_schedule_node_get_prefix_schedule_relation(node);
2518 prefix = isl_union_map_reverse(prefix);
2519 prefix = isl_union_map_range_map(prefix);
2521 data->set_sink = 1;
2522 umap = isl_union_map_copy(data->access->access[isl_access_sink]);
2523 umap = isl_union_map_apply_range(isl_union_map_copy(prefix), umap);
2524 if (isl_union_map_foreach_map(umap, &extract_sink_source, data) < 0)
2525 r = isl_bool_error;
2526 isl_union_map_free(umap);
2528 data->set_sink = 0;
2529 data->must = 1;
2530 umap = isl_union_map_copy(data->access->access[isl_access_must_source]);
2531 umap = isl_union_map_apply_range(isl_union_map_copy(prefix), umap);
2532 if (isl_union_map_foreach_map(umap, &extract_sink_source, data) < 0)
2533 r = isl_bool_error;
2534 isl_union_map_free(umap);
2536 data->set_sink = 0;
2537 data->must = 0;
2538 umap = isl_union_map_copy(data->access->access[isl_access_may_source]);
2539 umap = isl_union_map_apply_range(isl_union_map_copy(prefix), umap);
2540 if (isl_union_map_foreach_map(umap, &extract_sink_source, data) < 0)
2541 r = isl_bool_error;
2542 isl_union_map_free(umap);
2544 isl_union_map_free(prefix);
2546 return r;
2549 /* isl_access_info_compute_flow callback for determining whether
2550 * the shared nesting level and the ordering within that level
2551 * for two scheduled accesses for use in compute_single_flow.
2553 * The tokens passed to this function refer to the leaves
2554 * in the schedule tree where the accesses take place.
2556 * If n is the shared number of loops, then we need to return
2557 * "2 * n + 1" if "first" precedes "second" inside the innermost
2558 * shared loop and "2 * n" otherwise.
2560 * The innermost shared ancestor may be the leaves themselves
2561 * if the accesses take place in the same leaf. Otherwise,
2562 * it is either a set node or a sequence node. Only in the case
2563 * of a sequence node do we consider one access to precede the other.
2565 static int before_node(void *first, void *second)
2567 isl_schedule_node *node1 = first;
2568 isl_schedule_node *node2 = second;
2569 isl_schedule_node *shared;
2570 int depth;
2571 int before = 0;
2573 shared = isl_schedule_node_get_shared_ancestor(node1, node2);
2574 if (!shared)
2575 return -1;
2577 depth = isl_schedule_node_get_schedule_depth(shared);
2578 if (isl_schedule_node_get_type(shared) == isl_schedule_node_sequence) {
2579 int pos1, pos2;
2581 pos1 = isl_schedule_node_get_ancestor_child_position(node1,
2582 shared);
2583 pos2 = isl_schedule_node_get_ancestor_child_position(node2,
2584 shared);
2585 before = pos1 < pos2;
2588 isl_schedule_node_free(shared);
2590 return 2 * depth + before;
2593 /* Add the scheduled sources from "data" that access
2594 * the same data space as "sink" to "access".
2596 static __isl_give isl_access_info *add_matching_sources(
2597 __isl_take isl_access_info *access, struct isl_scheduled_access *sink,
2598 struct isl_compute_flow_schedule_data *data)
2600 int i;
2601 isl_space *space;
2603 space = isl_space_range(isl_map_get_space(sink->access));
2604 for (i = 0; i < data->n_source; ++i) {
2605 struct isl_scheduled_access *source;
2606 isl_space *source_space;
2607 int eq;
2609 source = &data->source[i];
2610 source_space = isl_map_get_space(source->access);
2611 source_space = isl_space_range(source_space);
2612 eq = isl_space_is_equal(space, source_space);
2613 isl_space_free(source_space);
2615 if (!eq)
2616 continue;
2617 if (eq < 0)
2618 goto error;
2620 access = isl_access_info_add_source(access,
2621 isl_map_copy(source->access), source->must, source->node);
2624 isl_space_free(space);
2625 return access;
2626 error:
2627 isl_space_free(space);
2628 isl_access_info_free(access);
2629 return NULL;
2632 /* Given a scheduled sink access relation "sink", compute the corresponding
2633 * dependences on the sources in "data" and add the computed dependences
2634 * to "uf".
2636 * The dependences computed by access_info_compute_flow_core are of the form
2638 * [S -> I] -> [[S' -> I'] -> A]
2640 * The schedule dimensions are projected out by first currying the range,
2641 * resulting in
2643 * [S -> I] -> [S' -> [I' -> A]]
2645 * and then computing the factor range
2647 * I -> [I' -> A]
2649 static __isl_give isl_union_flow *compute_single_flow(
2650 __isl_take isl_union_flow *uf, struct isl_scheduled_access *sink,
2651 struct isl_compute_flow_schedule_data *data)
2653 int i;
2654 isl_access_info *access;
2655 isl_flow *flow;
2656 isl_map *map;
2658 if (!uf)
2659 return NULL;
2661 access = isl_access_info_alloc(isl_map_copy(sink->access), sink->node,
2662 &before_node, data->n_source);
2663 access = add_matching_sources(access, sink, data);
2665 flow = access_info_compute_flow_core(access);
2666 if (!flow)
2667 return isl_union_flow_free(uf);
2669 map = isl_map_domain_factor_range(isl_flow_get_no_source(flow, 1));
2670 uf->must_no_source = isl_union_map_union(uf->must_no_source,
2671 isl_union_map_from_map(map));
2672 map = isl_map_domain_factor_range(isl_flow_get_no_source(flow, 0));
2673 uf->may_no_source = isl_union_map_union(uf->may_no_source,
2674 isl_union_map_from_map(map));
2676 for (i = 0; i < flow->n_source; ++i) {
2677 isl_union_map *dep;
2679 map = isl_map_range_curry(isl_map_copy(flow->dep[i].map));
2680 map = isl_map_factor_range(map);
2681 dep = isl_union_map_from_map(map);
2682 if (flow->dep[i].must)
2683 uf->must_dep = isl_union_map_union(uf->must_dep, dep);
2684 else
2685 uf->may_dep = isl_union_map_union(uf->may_dep, dep);
2688 isl_flow_free(flow);
2690 return uf;
2693 /* Given a description of the "sink" accesses, the "source" accesses and
2694 * a schedule, compute for each instance of a sink access
2695 * and for each element accessed by that instance,
2696 * the possible or definite source accesses that last accessed the
2697 * element accessed by the sink access before this sink access
2698 * in the sense that there is no intermediate definite source access.
2699 * Only consider dependences between statement instances that belong
2700 * to the domain of the schedule.
2702 * The must_no_source and may_no_source elements of the result
2703 * are subsets of access->sink. The elements must_dep and may_dep
2704 * map domain elements of access->{may,must)_source to
2705 * domain elements of access->sink.
2707 * This function is used when a schedule tree representation
2708 * is available.
2710 * We extract the individual scheduled source and sink access relations
2711 * (taking into account the domain of the schedule) and
2712 * then compute dependences for each scheduled sink individually.
2714 static __isl_give isl_union_flow *compute_flow_schedule(
2715 __isl_take isl_union_access_info *access)
2717 struct isl_compute_flow_schedule_data data = { access };
2718 int i, n;
2719 isl_ctx *ctx;
2720 isl_space *space;
2721 isl_union_flow *flow;
2723 ctx = isl_union_access_info_get_ctx(access);
2725 data.n_sink = 0;
2726 data.n_source = 0;
2727 if (isl_schedule_foreach_schedule_node_top_down(access->schedule,
2728 &count_sink_source, &data) < 0)
2729 goto error;
2731 n = data.n_sink + data.n_source;
2732 data.sink = isl_calloc_array(ctx, struct isl_scheduled_access, n);
2733 if (n && !data.sink)
2734 goto error;
2735 data.source = data.sink + data.n_sink;
2737 data.n_sink = 0;
2738 data.n_source = 0;
2739 if (isl_schedule_foreach_schedule_node_top_down(access->schedule,
2740 &collect_sink_source, &data) < 0)
2741 goto error;
2743 space = isl_union_map_get_space(access->access[isl_access_sink]);
2744 flow = isl_union_flow_alloc(space);
2746 isl_compute_flow_schedule_data_align_params(&data);
2748 for (i = 0; i < data.n_sink; ++i)
2749 flow = compute_single_flow(flow, &data.sink[i], &data);
2751 isl_compute_flow_schedule_data_clear(&data);
2753 isl_union_access_info_free(access);
2754 return flow;
2755 error:
2756 isl_union_access_info_free(access);
2757 isl_compute_flow_schedule_data_clear(&data);
2758 return NULL;
2761 /* Given a description of the "sink" accesses, the "source" accesses and
2762 * a schedule, compute for each instance of a sink access
2763 * and for each element accessed by that instance,
2764 * the possible or definite source accesses that last accessed the
2765 * element accessed by the sink access before this sink access
2766 * in the sense that there is no intermediate definite source access.
2768 * The must_no_source and may_no_source elements of the result
2769 * are subsets of access->sink. The elements must_dep and may_dep
2770 * map domain elements of access->{may,must)_source to
2771 * domain elements of access->sink.
2773 * We check whether the schedule is available as a schedule tree
2774 * or a schedule map and call the corresponding function to perform
2775 * the analysis.
2777 __isl_give isl_union_flow *isl_union_access_info_compute_flow(
2778 __isl_take isl_union_access_info *access)
2780 access = isl_union_access_info_normalize(access);
2781 if (!access)
2782 return NULL;
2783 if (access->schedule)
2784 return compute_flow_schedule(access);
2785 else
2786 return compute_flow_union_map(access);
2789 /* Print the information contained in "flow" to "p".
2790 * The information is printed as a YAML document.
2792 __isl_give isl_printer *isl_printer_print_union_flow(
2793 __isl_take isl_printer *p, __isl_keep isl_union_flow *flow)
2795 isl_union_map *umap;
2797 if (!flow)
2798 return isl_printer_free(p);
2800 p = isl_printer_yaml_start_mapping(p);
2801 umap = isl_union_flow_get_full_must_dependence(flow);
2802 p = print_union_map_field(p, "must_dependence", umap);
2803 isl_union_map_free(umap);
2804 umap = isl_union_flow_get_full_may_dependence(flow);
2805 p = print_union_map_field(p, "may_dependence", umap);
2806 isl_union_map_free(umap);
2807 p = print_union_map_field(p, "must_no_source", flow->must_no_source);
2808 umap = isl_union_flow_get_may_no_source(flow);
2809 p = print_union_map_field(p, "may_no_source", umap);
2810 isl_union_map_free(umap);
2811 p = isl_printer_yaml_end_mapping(p);
2813 return p;
2816 /* Return a string representation of the information in "flow".
2817 * The information is printed in flow format.
2819 __isl_give char *isl_union_flow_to_str(__isl_keep isl_union_flow *flow)
2821 isl_printer *p;
2822 char *s;
2824 if (!flow)
2825 return NULL;
2827 p = isl_printer_to_str(isl_union_flow_get_ctx(flow));
2828 p = isl_printer_set_yaml_style(p, ISL_YAML_STYLE_FLOW);
2829 p = isl_printer_print_union_flow(p, flow);
2830 s = isl_printer_get_str(p);
2831 isl_printer_free(p);
2833 return s;
2836 /* Given a collection of "sink" and "source" accesses,
2837 * compute for each iteration of a sink access
2838 * and for each element accessed by that iteration,
2839 * the source access in the list that last accessed the
2840 * element accessed by the sink access before this sink access.
2841 * Each access is given as a map from the loop iterators
2842 * to the array indices.
2843 * The result is a relations between source and sink
2844 * iterations and a subset of the domain of the sink accesses,
2845 * corresponding to those iterations that access an element
2846 * not previously accessed.
2848 * We collect the inputs in an isl_union_access_info object,
2849 * call isl_union_access_info_compute_flow and extract
2850 * the outputs from the result.
2852 int isl_union_map_compute_flow(__isl_take isl_union_map *sink,
2853 __isl_take isl_union_map *must_source,
2854 __isl_take isl_union_map *may_source,
2855 __isl_take isl_union_map *schedule,
2856 __isl_give isl_union_map **must_dep, __isl_give isl_union_map **may_dep,
2857 __isl_give isl_union_map **must_no_source,
2858 __isl_give isl_union_map **may_no_source)
2860 isl_union_access_info *access;
2861 isl_union_flow *flow;
2863 access = isl_union_access_info_from_sink(sink);
2864 access = isl_union_access_info_set_must_source(access, must_source);
2865 access = isl_union_access_info_set_may_source(access, may_source);
2866 access = isl_union_access_info_set_schedule_map(access, schedule);
2867 flow = isl_union_access_info_compute_flow(access);
2869 if (must_dep)
2870 *must_dep = isl_union_flow_get_must_dependence(flow);
2871 if (may_dep)
2872 *may_dep = isl_union_flow_get_non_must_dependence(flow);
2873 if (must_no_source)
2874 *must_no_source = isl_union_flow_get_must_no_source(flow);
2875 if (may_no_source)
2876 *may_no_source = isl_union_flow_get_non_must_no_source(flow);
2878 isl_union_flow_free(flow);
2880 if ((must_dep && !*must_dep) || (may_dep && !*may_dep) ||
2881 (must_no_source && !*must_no_source) ||
2882 (may_no_source && !*may_no_source))
2883 goto error;
2885 return 0;
2886 error:
2887 if (must_dep)
2888 *must_dep = isl_union_map_free(*must_dep);
2889 if (may_dep)
2890 *may_dep = isl_union_map_free(*may_dep);
2891 if (must_no_source)
2892 *must_no_source = isl_union_map_free(*must_no_source);
2893 if (may_no_source)
2894 *may_no_source = isl_union_map_free(*may_no_source);
2895 return -1;