make schedule_max_coefficient option work as advertised
[isl.git] / isl_scheduler.c
blob457422522498860d15286ec42515deefa03f1f34
1 /*
2 * Copyright 2011 INRIA Saclay
3 * Copyright 2012-2014 Ecole Normale Superieure
4 * Copyright 2015-2016 Sven Verdoolaege
6 * Use of this software is governed by the MIT license
8 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
9 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
10 * 91893 Orsay, France
11 * and Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
14 #include <isl_ctx_private.h>
15 #include <isl_map_private.h>
16 #include <isl_space_private.h>
17 #include <isl_aff_private.h>
18 #include <isl/hash.h>
19 #include <isl/constraint.h>
20 #include <isl/schedule.h>
21 #include <isl/schedule_node.h>
22 #include <isl_mat_private.h>
23 #include <isl_vec_private.h>
24 #include <isl/set.h>
25 #include <isl/union_set.h>
26 #include <isl_seq.h>
27 #include <isl_tab.h>
28 #include <isl_dim_map.h>
29 #include <isl/map_to_basic_set.h>
30 #include <isl_sort.h>
31 #include <isl_options_private.h>
32 #include <isl_tarjan.h>
33 #include <isl_morph.h>
36 * The scheduling algorithm implemented in this file was inspired by
37 * Bondhugula et al., "Automatic Transformations for Communication-Minimized
38 * Parallelization and Locality Optimization in the Polyhedral Model".
41 enum isl_edge_type {
42 isl_edge_validity = 0,
43 isl_edge_first = isl_edge_validity,
44 isl_edge_coincidence,
45 isl_edge_condition,
46 isl_edge_conditional_validity,
47 isl_edge_proximity,
48 isl_edge_last = isl_edge_proximity,
49 isl_edge_local
52 /* The constraints that need to be satisfied by a schedule on "domain".
54 * "context" specifies extra constraints on the parameters.
56 * "validity" constraints map domain elements i to domain elements
57 * that should be scheduled after i. (Hard constraint)
58 * "proximity" constraints map domain elements i to domains elements
59 * that should be scheduled as early as possible after i (or before i).
60 * (Soft constraint)
62 * "condition" and "conditional_validity" constraints map possibly "tagged"
63 * domain elements i -> s to "tagged" domain elements j -> t.
64 * The elements of the "conditional_validity" constraints, but without the
65 * tags (i.e., the elements i -> j) are treated as validity constraints,
66 * except that during the construction of a tilable band,
67 * the elements of the "conditional_validity" constraints may be violated
68 * provided that all adjacent elements of the "condition" constraints
69 * are local within the band.
70 * A dependence is local within a band if domain and range are mapped
71 * to the same schedule point by the band.
73 struct isl_schedule_constraints {
74 isl_union_set *domain;
75 isl_set *context;
77 isl_union_map *constraint[isl_edge_last + 1];
80 __isl_give isl_schedule_constraints *isl_schedule_constraints_copy(
81 __isl_keep isl_schedule_constraints *sc)
83 isl_ctx *ctx;
84 isl_schedule_constraints *sc_copy;
85 enum isl_edge_type i;
87 ctx = isl_union_set_get_ctx(sc->domain);
88 sc_copy = isl_calloc_type(ctx, struct isl_schedule_constraints);
89 if (!sc_copy)
90 return NULL;
92 sc_copy->domain = isl_union_set_copy(sc->domain);
93 sc_copy->context = isl_set_copy(sc->context);
94 if (!sc_copy->domain || !sc_copy->context)
95 return isl_schedule_constraints_free(sc_copy);
97 for (i = isl_edge_first; i <= isl_edge_last; ++i) {
98 sc_copy->constraint[i] = isl_union_map_copy(sc->constraint[i]);
99 if (!sc_copy->constraint[i])
100 return isl_schedule_constraints_free(sc_copy);
103 return sc_copy;
107 /* Construct an isl_schedule_constraints object for computing a schedule
108 * on "domain". The initial object does not impose any constraints.
110 __isl_give isl_schedule_constraints *isl_schedule_constraints_on_domain(
111 __isl_take isl_union_set *domain)
113 isl_ctx *ctx;
114 isl_space *space;
115 isl_schedule_constraints *sc;
116 isl_union_map *empty;
117 enum isl_edge_type i;
119 if (!domain)
120 return NULL;
122 ctx = isl_union_set_get_ctx(domain);
123 sc = isl_calloc_type(ctx, struct isl_schedule_constraints);
124 if (!sc)
125 goto error;
127 space = isl_union_set_get_space(domain);
128 sc->domain = domain;
129 sc->context = isl_set_universe(isl_space_copy(space));
130 empty = isl_union_map_empty(space);
131 for (i = isl_edge_first; i <= isl_edge_last; ++i) {
132 sc->constraint[i] = isl_union_map_copy(empty);
133 if (!sc->constraint[i])
134 sc->domain = isl_union_set_free(sc->domain);
136 isl_union_map_free(empty);
138 if (!sc->domain || !sc->context)
139 return isl_schedule_constraints_free(sc);
141 return sc;
142 error:
143 isl_union_set_free(domain);
144 return NULL;
147 /* Replace the context of "sc" by "context".
149 __isl_give isl_schedule_constraints *isl_schedule_constraints_set_context(
150 __isl_take isl_schedule_constraints *sc, __isl_take isl_set *context)
152 if (!sc || !context)
153 goto error;
155 isl_set_free(sc->context);
156 sc->context = context;
158 return sc;
159 error:
160 isl_schedule_constraints_free(sc);
161 isl_set_free(context);
162 return NULL;
165 /* Replace the validity constraints of "sc" by "validity".
167 __isl_give isl_schedule_constraints *isl_schedule_constraints_set_validity(
168 __isl_take isl_schedule_constraints *sc,
169 __isl_take isl_union_map *validity)
171 if (!sc || !validity)
172 goto error;
174 isl_union_map_free(sc->constraint[isl_edge_validity]);
175 sc->constraint[isl_edge_validity] = validity;
177 return sc;
178 error:
179 isl_schedule_constraints_free(sc);
180 isl_union_map_free(validity);
181 return NULL;
184 /* Replace the coincidence constraints of "sc" by "coincidence".
186 __isl_give isl_schedule_constraints *isl_schedule_constraints_set_coincidence(
187 __isl_take isl_schedule_constraints *sc,
188 __isl_take isl_union_map *coincidence)
190 if (!sc || !coincidence)
191 goto error;
193 isl_union_map_free(sc->constraint[isl_edge_coincidence]);
194 sc->constraint[isl_edge_coincidence] = coincidence;
196 return sc;
197 error:
198 isl_schedule_constraints_free(sc);
199 isl_union_map_free(coincidence);
200 return NULL;
203 /* Replace the proximity constraints of "sc" by "proximity".
205 __isl_give isl_schedule_constraints *isl_schedule_constraints_set_proximity(
206 __isl_take isl_schedule_constraints *sc,
207 __isl_take isl_union_map *proximity)
209 if (!sc || !proximity)
210 goto error;
212 isl_union_map_free(sc->constraint[isl_edge_proximity]);
213 sc->constraint[isl_edge_proximity] = proximity;
215 return sc;
216 error:
217 isl_schedule_constraints_free(sc);
218 isl_union_map_free(proximity);
219 return NULL;
222 /* Replace the conditional validity constraints of "sc" by "condition"
223 * and "validity".
225 __isl_give isl_schedule_constraints *
226 isl_schedule_constraints_set_conditional_validity(
227 __isl_take isl_schedule_constraints *sc,
228 __isl_take isl_union_map *condition,
229 __isl_take isl_union_map *validity)
231 if (!sc || !condition || !validity)
232 goto error;
234 isl_union_map_free(sc->constraint[isl_edge_condition]);
235 sc->constraint[isl_edge_condition] = condition;
236 isl_union_map_free(sc->constraint[isl_edge_conditional_validity]);
237 sc->constraint[isl_edge_conditional_validity] = validity;
239 return sc;
240 error:
241 isl_schedule_constraints_free(sc);
242 isl_union_map_free(condition);
243 isl_union_map_free(validity);
244 return NULL;
247 __isl_null isl_schedule_constraints *isl_schedule_constraints_free(
248 __isl_take isl_schedule_constraints *sc)
250 enum isl_edge_type i;
252 if (!sc)
253 return NULL;
255 isl_union_set_free(sc->domain);
256 isl_set_free(sc->context);
257 for (i = isl_edge_first; i <= isl_edge_last; ++i)
258 isl_union_map_free(sc->constraint[i]);
260 free(sc);
262 return NULL;
265 isl_ctx *isl_schedule_constraints_get_ctx(
266 __isl_keep isl_schedule_constraints *sc)
268 return sc ? isl_union_set_get_ctx(sc->domain) : NULL;
271 /* Return the domain of "sc".
273 __isl_give isl_union_set *isl_schedule_constraints_get_domain(
274 __isl_keep isl_schedule_constraints *sc)
276 if (!sc)
277 return NULL;
279 return isl_union_set_copy(sc->domain);
282 /* Return the validity constraints of "sc".
284 __isl_give isl_union_map *isl_schedule_constraints_get_validity(
285 __isl_keep isl_schedule_constraints *sc)
287 if (!sc)
288 return NULL;
290 return isl_union_map_copy(sc->constraint[isl_edge_validity]);
293 /* Return the coincidence constraints of "sc".
295 __isl_give isl_union_map *isl_schedule_constraints_get_coincidence(
296 __isl_keep isl_schedule_constraints *sc)
298 if (!sc)
299 return NULL;
301 return isl_union_map_copy(sc->constraint[isl_edge_coincidence]);
304 /* Return the proximity constraints of "sc".
306 __isl_give isl_union_map *isl_schedule_constraints_get_proximity(
307 __isl_keep isl_schedule_constraints *sc)
309 if (!sc)
310 return NULL;
312 return isl_union_map_copy(sc->constraint[isl_edge_proximity]);
315 /* Return the conditional validity constraints of "sc".
317 __isl_give isl_union_map *isl_schedule_constraints_get_conditional_validity(
318 __isl_keep isl_schedule_constraints *sc)
320 if (!sc)
321 return NULL;
323 return
324 isl_union_map_copy(sc->constraint[isl_edge_conditional_validity]);
327 /* Return the conditions for the conditional validity constraints of "sc".
329 __isl_give isl_union_map *
330 isl_schedule_constraints_get_conditional_validity_condition(
331 __isl_keep isl_schedule_constraints *sc)
333 if (!sc)
334 return NULL;
336 return isl_union_map_copy(sc->constraint[isl_edge_condition]);
339 /* Can a schedule constraint of type "type" be tagged?
341 static int may_be_tagged(enum isl_edge_type type)
343 if (type == isl_edge_condition || type == isl_edge_conditional_validity)
344 return 1;
345 return 0;
348 /* Apply "umap" to the domains of the wrapped relations
349 * inside the domain and range of "c".
351 * That is, for each map of the form
353 * [D -> S] -> [E -> T]
355 * in "c", apply "umap" to D and E.
357 * D is exposed by currying the relation to
359 * D -> [S -> [E -> T]]
361 * E is exposed by doing the same to the inverse of "c".
363 static __isl_give isl_union_map *apply_factor_domain(
364 __isl_take isl_union_map *c, __isl_keep isl_union_map *umap)
366 c = isl_union_map_curry(c);
367 c = isl_union_map_apply_domain(c, isl_union_map_copy(umap));
368 c = isl_union_map_uncurry(c);
370 c = isl_union_map_reverse(c);
371 c = isl_union_map_curry(c);
372 c = isl_union_map_apply_domain(c, isl_union_map_copy(umap));
373 c = isl_union_map_uncurry(c);
374 c = isl_union_map_reverse(c);
376 return c;
379 /* Apply "umap" to domain and range of "c".
380 * If "tag" is set, then "c" may contain tags and then "umap"
381 * needs to be applied to the domains of the wrapped relations
382 * inside the domain and range of "c".
384 static __isl_give isl_union_map *apply(__isl_take isl_union_map *c,
385 __isl_keep isl_union_map *umap, int tag)
387 isl_union_map *t;
389 if (tag)
390 t = isl_union_map_copy(c);
391 c = isl_union_map_apply_domain(c, isl_union_map_copy(umap));
392 c = isl_union_map_apply_range(c, isl_union_map_copy(umap));
393 if (!tag)
394 return c;
395 t = apply_factor_domain(t, umap);
396 c = isl_union_map_union(c, t);
397 return c;
400 /* Apply "umap" to the domain of the schedule constraints "sc".
402 * The two sides of the various schedule constraints are adjusted
403 * accordingly.
405 __isl_give isl_schedule_constraints *isl_schedule_constraints_apply(
406 __isl_take isl_schedule_constraints *sc,
407 __isl_take isl_union_map *umap)
409 enum isl_edge_type i;
411 if (!sc || !umap)
412 goto error;
414 for (i = isl_edge_first; i <= isl_edge_last; ++i) {
415 int tag = may_be_tagged(i);
417 sc->constraint[i] = apply(sc->constraint[i], umap, tag);
418 if (!sc->constraint[i])
419 goto error;
421 sc->domain = isl_union_set_apply(sc->domain, umap);
422 if (!sc->domain)
423 return isl_schedule_constraints_free(sc);
425 return sc;
426 error:
427 isl_schedule_constraints_free(sc);
428 isl_union_map_free(umap);
429 return NULL;
432 void isl_schedule_constraints_dump(__isl_keep isl_schedule_constraints *sc)
434 if (!sc)
435 return;
437 fprintf(stderr, "domain: ");
438 isl_union_set_dump(sc->domain);
439 fprintf(stderr, "context: ");
440 isl_set_dump(sc->context);
441 fprintf(stderr, "validity: ");
442 isl_union_map_dump(sc->constraint[isl_edge_validity]);
443 fprintf(stderr, "proximity: ");
444 isl_union_map_dump(sc->constraint[isl_edge_proximity]);
445 fprintf(stderr, "coincidence: ");
446 isl_union_map_dump(sc->constraint[isl_edge_coincidence]);
447 fprintf(stderr, "condition: ");
448 isl_union_map_dump(sc->constraint[isl_edge_condition]);
449 fprintf(stderr, "conditional_validity: ");
450 isl_union_map_dump(sc->constraint[isl_edge_conditional_validity]);
453 /* Align the parameters of the fields of "sc".
455 static __isl_give isl_schedule_constraints *
456 isl_schedule_constraints_align_params(__isl_take isl_schedule_constraints *sc)
458 isl_space *space;
459 enum isl_edge_type i;
461 if (!sc)
462 return NULL;
464 space = isl_union_set_get_space(sc->domain);
465 space = isl_space_align_params(space, isl_set_get_space(sc->context));
466 for (i = isl_edge_first; i <= isl_edge_last; ++i)
467 space = isl_space_align_params(space,
468 isl_union_map_get_space(sc->constraint[i]));
470 for (i = isl_edge_first; i <= isl_edge_last; ++i) {
471 sc->constraint[i] = isl_union_map_align_params(
472 sc->constraint[i], isl_space_copy(space));
473 if (!sc->constraint[i])
474 space = isl_space_free(space);
476 sc->context = isl_set_align_params(sc->context, isl_space_copy(space));
477 sc->domain = isl_union_set_align_params(sc->domain, space);
478 if (!sc->context || !sc->domain)
479 return isl_schedule_constraints_free(sc);
481 return sc;
484 /* Return the total number of isl_maps in the constraints of "sc".
486 static __isl_give int isl_schedule_constraints_n_map(
487 __isl_keep isl_schedule_constraints *sc)
489 enum isl_edge_type i;
490 int n = 0;
492 for (i = isl_edge_first; i <= isl_edge_last; ++i)
493 n += isl_union_map_n_map(sc->constraint[i]);
495 return n;
498 /* Internal information about a node that is used during the construction
499 * of a schedule.
500 * space represents the space in which the domain lives
501 * sched is a matrix representation of the schedule being constructed
502 * for this node; if compressed is set, then this schedule is
503 * defined over the compressed domain space
504 * sched_map is an isl_map representation of the same (partial) schedule
505 * sched_map may be NULL; if compressed is set, then this map
506 * is defined over the uncompressed domain space
507 * rank is the number of linearly independent rows in the linear part
508 * of sched
509 * the columns of cmap represent a change of basis for the schedule
510 * coefficients; the first rank columns span the linear part of
511 * the schedule rows
512 * cinv is the inverse of cmap.
513 * ctrans is the transpose of cmap.
514 * start is the first variable in the LP problem in the sequences that
515 * represents the schedule coefficients of this node
516 * nvar is the dimension of the domain
517 * nparam is the number of parameters or 0 if we are not constructing
518 * a parametric schedule
520 * If compressed is set, then hull represents the constraints
521 * that were used to derive the compression, while compress and
522 * decompress map the original space to the compressed space and
523 * vice versa.
525 * scc is the index of SCC (or WCC) this node belongs to
527 * "cluster" is only used inside extract_clusters and identifies
528 * the cluster of SCCs that the node belongs to.
530 * coincident contains a boolean for each of the rows of the schedule,
531 * indicating whether the corresponding scheduling dimension satisfies
532 * the coincidence constraints in the sense that the corresponding
533 * dependence distances are zero.
535 struct isl_sched_node {
536 isl_space *space;
537 int compressed;
538 isl_set *hull;
539 isl_multi_aff *compress;
540 isl_multi_aff *decompress;
541 isl_mat *sched;
542 isl_map *sched_map;
543 int rank;
544 isl_mat *cmap;
545 isl_mat *cinv;
546 isl_mat *ctrans;
547 int start;
548 int nvar;
549 int nparam;
551 int scc;
552 int cluster;
554 int *coincident;
557 static int node_has_space(const void *entry, const void *val)
559 struct isl_sched_node *node = (struct isl_sched_node *)entry;
560 isl_space *dim = (isl_space *)val;
562 return isl_space_is_equal(node->space, dim);
565 static int node_scc_exactly(struct isl_sched_node *node, int scc)
567 return node->scc == scc;
570 static int node_scc_at_most(struct isl_sched_node *node, int scc)
572 return node->scc <= scc;
575 static int node_scc_at_least(struct isl_sched_node *node, int scc)
577 return node->scc >= scc;
580 /* An edge in the dependence graph. An edge may be used to
581 * ensure validity of the generated schedule, to minimize the dependence
582 * distance or both
584 * map is the dependence relation, with i -> j in the map if j depends on i
585 * tagged_condition and tagged_validity contain the union of all tagged
586 * condition or conditional validity dependence relations that
587 * specialize the dependence relation "map"; that is,
588 * if (i -> a) -> (j -> b) is an element of "tagged_condition"
589 * or "tagged_validity", then i -> j is an element of "map".
590 * If these fields are NULL, then they represent the empty relation.
591 * src is the source node
592 * dst is the sink node
594 * types is a bit vector containing the types of this edge.
595 * validity is set if the edge is used to ensure correctness
596 * coincidence is used to enforce zero dependence distances
597 * proximity is set if the edge is used to minimize dependence distances
598 * condition is set if the edge represents a condition
599 * for a conditional validity schedule constraint
600 * local can only be set for condition edges and indicates that
601 * the dependence distance over the edge should be zero
602 * conditional_validity is set if the edge is used to conditionally
603 * ensure correctness
605 * For validity edges, start and end mark the sequence of inequality
606 * constraints in the LP problem that encode the validity constraint
607 * corresponding to this edge.
609 * During clustering, an edge may be marked "no_merge" if it should
610 * not be used to merge clusters.
611 * The weight is also only used during clustering and it is
612 * an indication of how many schedule dimensions on either side
613 * of the schedule constraints can be aligned.
614 * If the weight is negative, then this means that this edge was postponed
615 * by has_bounded_distances or any_no_merge. The original weight can
616 * be retrieved by adding 1 + graph->max_weight, with "graph"
617 * the graph containing this edge.
619 struct isl_sched_edge {
620 isl_map *map;
621 isl_union_map *tagged_condition;
622 isl_union_map *tagged_validity;
624 struct isl_sched_node *src;
625 struct isl_sched_node *dst;
627 unsigned types;
629 int start;
630 int end;
632 int no_merge;
633 int weight;
636 /* Is "edge" marked as being of type "type"?
638 static int is_type(struct isl_sched_edge *edge, enum isl_edge_type type)
640 return ISL_FL_ISSET(edge->types, 1 << type);
643 /* Mark "edge" as being of type "type".
645 static void set_type(struct isl_sched_edge *edge, enum isl_edge_type type)
647 ISL_FL_SET(edge->types, 1 << type);
650 /* No longer mark "edge" as being of type "type"?
652 static void clear_type(struct isl_sched_edge *edge, enum isl_edge_type type)
654 ISL_FL_CLR(edge->types, 1 << type);
657 /* Is "edge" marked as a validity edge?
659 static int is_validity(struct isl_sched_edge *edge)
661 return is_type(edge, isl_edge_validity);
664 /* Mark "edge" as a validity edge.
666 static void set_validity(struct isl_sched_edge *edge)
668 set_type(edge, isl_edge_validity);
671 /* Is "edge" marked as a proximity edge?
673 static int is_proximity(struct isl_sched_edge *edge)
675 return is_type(edge, isl_edge_proximity);
678 /* Is "edge" marked as a local edge?
680 static int is_local(struct isl_sched_edge *edge)
682 return is_type(edge, isl_edge_local);
685 /* Mark "edge" as a local edge.
687 static void set_local(struct isl_sched_edge *edge)
689 set_type(edge, isl_edge_local);
692 /* No longer mark "edge" as a local edge.
694 static void clear_local(struct isl_sched_edge *edge)
696 clear_type(edge, isl_edge_local);
699 /* Is "edge" marked as a coincidence edge?
701 static int is_coincidence(struct isl_sched_edge *edge)
703 return is_type(edge, isl_edge_coincidence);
706 /* Is "edge" marked as a condition edge?
708 static int is_condition(struct isl_sched_edge *edge)
710 return is_type(edge, isl_edge_condition);
713 /* Is "edge" marked as a conditional validity edge?
715 static int is_conditional_validity(struct isl_sched_edge *edge)
717 return is_type(edge, isl_edge_conditional_validity);
720 /* Internal information about the dependence graph used during
721 * the construction of the schedule.
723 * intra_hmap is a cache, mapping dependence relations to their dual,
724 * for dependences from a node to itself
725 * inter_hmap is a cache, mapping dependence relations to their dual,
726 * for dependences between distinct nodes
727 * if compression is involved then the key for these maps
728 * is the original, uncompressed dependence relation, while
729 * the value is the dual of the compressed dependence relation.
731 * n is the number of nodes
732 * node is the list of nodes
733 * maxvar is the maximal number of variables over all nodes
734 * max_row is the allocated number of rows in the schedule
735 * n_row is the current (maximal) number of linearly independent
736 * rows in the node schedules
737 * n_total_row is the current number of rows in the node schedules
738 * band_start is the starting row in the node schedules of the current band
739 * root is set if this graph is the original dependence graph,
740 * without any splitting
742 * sorted contains a list of node indices sorted according to the
743 * SCC to which a node belongs
745 * n_edge is the number of edges
746 * edge is the list of edges
747 * max_edge contains the maximal number of edges of each type;
748 * in particular, it contains the number of edges in the inital graph.
749 * edge_table contains pointers into the edge array, hashed on the source
750 * and sink spaces; there is one such table for each type;
751 * a given edge may be referenced from more than one table
752 * if the corresponding relation appears in more than one of the
753 * sets of dependences; however, for each type there is only
754 * a single edge between a given pair of source and sink space
755 * in the entire graph
757 * node_table contains pointers into the node array, hashed on the space
759 * region contains a list of variable sequences that should be non-trivial
761 * lp contains the (I)LP problem used to obtain new schedule rows
763 * src_scc and dst_scc are the source and sink SCCs of an edge with
764 * conflicting constraints
766 * scc represents the number of components
767 * weak is set if the components are weakly connected
769 * max_weight is used during clustering and represents the maximal
770 * weight of the relevant proximity edges.
772 struct isl_sched_graph {
773 isl_map_to_basic_set *intra_hmap;
774 isl_map_to_basic_set *inter_hmap;
776 struct isl_sched_node *node;
777 int n;
778 int maxvar;
779 int max_row;
780 int n_row;
782 int *sorted;
784 int n_total_row;
785 int band_start;
787 int root;
789 struct isl_sched_edge *edge;
790 int n_edge;
791 int max_edge[isl_edge_last + 1];
792 struct isl_hash_table *edge_table[isl_edge_last + 1];
794 struct isl_hash_table *node_table;
795 struct isl_region *region;
797 isl_basic_set *lp;
799 int src_scc;
800 int dst_scc;
802 int scc;
803 int weak;
805 int max_weight;
808 /* Initialize node_table based on the list of nodes.
810 static int graph_init_table(isl_ctx *ctx, struct isl_sched_graph *graph)
812 int i;
814 graph->node_table = isl_hash_table_alloc(ctx, graph->n);
815 if (!graph->node_table)
816 return -1;
818 for (i = 0; i < graph->n; ++i) {
819 struct isl_hash_table_entry *entry;
820 uint32_t hash;
822 hash = isl_space_get_hash(graph->node[i].space);
823 entry = isl_hash_table_find(ctx, graph->node_table, hash,
824 &node_has_space,
825 graph->node[i].space, 1);
826 if (!entry)
827 return -1;
828 entry->data = &graph->node[i];
831 return 0;
834 /* Return a pointer to the node that lives within the given space,
835 * or NULL if there is no such node.
837 static struct isl_sched_node *graph_find_node(isl_ctx *ctx,
838 struct isl_sched_graph *graph, __isl_keep isl_space *dim)
840 struct isl_hash_table_entry *entry;
841 uint32_t hash;
843 hash = isl_space_get_hash(dim);
844 entry = isl_hash_table_find(ctx, graph->node_table, hash,
845 &node_has_space, dim, 0);
847 return entry ? entry->data : NULL;
850 static int edge_has_src_and_dst(const void *entry, const void *val)
852 const struct isl_sched_edge *edge = entry;
853 const struct isl_sched_edge *temp = val;
855 return edge->src == temp->src && edge->dst == temp->dst;
858 /* Add the given edge to graph->edge_table[type].
860 static isl_stat graph_edge_table_add(isl_ctx *ctx,
861 struct isl_sched_graph *graph, enum isl_edge_type type,
862 struct isl_sched_edge *edge)
864 struct isl_hash_table_entry *entry;
865 uint32_t hash;
867 hash = isl_hash_init();
868 hash = isl_hash_builtin(hash, edge->src);
869 hash = isl_hash_builtin(hash, edge->dst);
870 entry = isl_hash_table_find(ctx, graph->edge_table[type], hash,
871 &edge_has_src_and_dst, edge, 1);
872 if (!entry)
873 return isl_stat_error;
874 entry->data = edge;
876 return isl_stat_ok;
879 /* Allocate the edge_tables based on the maximal number of edges of
880 * each type.
882 static int graph_init_edge_tables(isl_ctx *ctx, struct isl_sched_graph *graph)
884 int i;
886 for (i = 0; i <= isl_edge_last; ++i) {
887 graph->edge_table[i] = isl_hash_table_alloc(ctx,
888 graph->max_edge[i]);
889 if (!graph->edge_table[i])
890 return -1;
893 return 0;
896 /* If graph->edge_table[type] contains an edge from the given source
897 * to the given destination, then return the hash table entry of this edge.
898 * Otherwise, return NULL.
900 static struct isl_hash_table_entry *graph_find_edge_entry(
901 struct isl_sched_graph *graph,
902 enum isl_edge_type type,
903 struct isl_sched_node *src, struct isl_sched_node *dst)
905 isl_ctx *ctx = isl_space_get_ctx(src->space);
906 uint32_t hash;
907 struct isl_sched_edge temp = { .src = src, .dst = dst };
909 hash = isl_hash_init();
910 hash = isl_hash_builtin(hash, temp.src);
911 hash = isl_hash_builtin(hash, temp.dst);
912 return isl_hash_table_find(ctx, graph->edge_table[type], hash,
913 &edge_has_src_and_dst, &temp, 0);
917 /* If graph->edge_table[type] contains an edge from the given source
918 * to the given destination, then return this edge.
919 * Otherwise, return NULL.
921 static struct isl_sched_edge *graph_find_edge(struct isl_sched_graph *graph,
922 enum isl_edge_type type,
923 struct isl_sched_node *src, struct isl_sched_node *dst)
925 struct isl_hash_table_entry *entry;
927 entry = graph_find_edge_entry(graph, type, src, dst);
928 if (!entry)
929 return NULL;
931 return entry->data;
934 /* Check whether the dependence graph has an edge of the given type
935 * between the given two nodes.
937 static isl_bool graph_has_edge(struct isl_sched_graph *graph,
938 enum isl_edge_type type,
939 struct isl_sched_node *src, struct isl_sched_node *dst)
941 struct isl_sched_edge *edge;
942 isl_bool empty;
944 edge = graph_find_edge(graph, type, src, dst);
945 if (!edge)
946 return 0;
948 empty = isl_map_plain_is_empty(edge->map);
949 if (empty < 0)
950 return isl_bool_error;
952 return !empty;
955 /* Look for any edge with the same src, dst and map fields as "model".
957 * Return the matching edge if one can be found.
958 * Return "model" if no matching edge is found.
959 * Return NULL on error.
961 static struct isl_sched_edge *graph_find_matching_edge(
962 struct isl_sched_graph *graph, struct isl_sched_edge *model)
964 enum isl_edge_type i;
965 struct isl_sched_edge *edge;
967 for (i = isl_edge_first; i <= isl_edge_last; ++i) {
968 int is_equal;
970 edge = graph_find_edge(graph, i, model->src, model->dst);
971 if (!edge)
972 continue;
973 is_equal = isl_map_plain_is_equal(model->map, edge->map);
974 if (is_equal < 0)
975 return NULL;
976 if (is_equal)
977 return edge;
980 return model;
983 /* Remove the given edge from all the edge_tables that refer to it.
985 static void graph_remove_edge(struct isl_sched_graph *graph,
986 struct isl_sched_edge *edge)
988 isl_ctx *ctx = isl_map_get_ctx(edge->map);
989 enum isl_edge_type i;
991 for (i = isl_edge_first; i <= isl_edge_last; ++i) {
992 struct isl_hash_table_entry *entry;
994 entry = graph_find_edge_entry(graph, i, edge->src, edge->dst);
995 if (!entry)
996 continue;
997 if (entry->data != edge)
998 continue;
999 isl_hash_table_remove(ctx, graph->edge_table[i], entry);
1003 /* Check whether the dependence graph has any edge
1004 * between the given two nodes.
1006 static isl_bool graph_has_any_edge(struct isl_sched_graph *graph,
1007 struct isl_sched_node *src, struct isl_sched_node *dst)
1009 enum isl_edge_type i;
1010 isl_bool r;
1012 for (i = isl_edge_first; i <= isl_edge_last; ++i) {
1013 r = graph_has_edge(graph, i, src, dst);
1014 if (r < 0 || r)
1015 return r;
1018 return r;
1021 /* Check whether the dependence graph has a validity edge
1022 * between the given two nodes.
1024 * Conditional validity edges are essentially validity edges that
1025 * can be ignored if the corresponding condition edges are iteration private.
1026 * Here, we are only checking for the presence of validity
1027 * edges, so we need to consider the conditional validity edges too.
1028 * In particular, this function is used during the detection
1029 * of strongly connected components and we cannot ignore
1030 * conditional validity edges during this detection.
1032 static isl_bool graph_has_validity_edge(struct isl_sched_graph *graph,
1033 struct isl_sched_node *src, struct isl_sched_node *dst)
1035 isl_bool r;
1037 r = graph_has_edge(graph, isl_edge_validity, src, dst);
1038 if (r < 0 || r)
1039 return r;
1041 return graph_has_edge(graph, isl_edge_conditional_validity, src, dst);
1044 static int graph_alloc(isl_ctx *ctx, struct isl_sched_graph *graph,
1045 int n_node, int n_edge)
1047 int i;
1049 graph->n = n_node;
1050 graph->n_edge = n_edge;
1051 graph->node = isl_calloc_array(ctx, struct isl_sched_node, graph->n);
1052 graph->sorted = isl_calloc_array(ctx, int, graph->n);
1053 graph->region = isl_alloc_array(ctx, struct isl_region, graph->n);
1054 graph->edge = isl_calloc_array(ctx,
1055 struct isl_sched_edge, graph->n_edge);
1057 graph->intra_hmap = isl_map_to_basic_set_alloc(ctx, 2 * n_edge);
1058 graph->inter_hmap = isl_map_to_basic_set_alloc(ctx, 2 * n_edge);
1060 if (!graph->node || !graph->region || (graph->n_edge && !graph->edge) ||
1061 !graph->sorted)
1062 return -1;
1064 for(i = 0; i < graph->n; ++i)
1065 graph->sorted[i] = i;
1067 return 0;
1070 static void graph_free(isl_ctx *ctx, struct isl_sched_graph *graph)
1072 int i;
1074 isl_map_to_basic_set_free(graph->intra_hmap);
1075 isl_map_to_basic_set_free(graph->inter_hmap);
1077 if (graph->node)
1078 for (i = 0; i < graph->n; ++i) {
1079 isl_space_free(graph->node[i].space);
1080 isl_set_free(graph->node[i].hull);
1081 isl_multi_aff_free(graph->node[i].compress);
1082 isl_multi_aff_free(graph->node[i].decompress);
1083 isl_mat_free(graph->node[i].sched);
1084 isl_map_free(graph->node[i].sched_map);
1085 isl_mat_free(graph->node[i].cmap);
1086 isl_mat_free(graph->node[i].cinv);
1087 isl_mat_free(graph->node[i].ctrans);
1088 if (graph->root)
1089 free(graph->node[i].coincident);
1091 free(graph->node);
1092 free(graph->sorted);
1093 if (graph->edge)
1094 for (i = 0; i < graph->n_edge; ++i) {
1095 isl_map_free(graph->edge[i].map);
1096 isl_union_map_free(graph->edge[i].tagged_condition);
1097 isl_union_map_free(graph->edge[i].tagged_validity);
1099 free(graph->edge);
1100 free(graph->region);
1101 for (i = 0; i <= isl_edge_last; ++i)
1102 isl_hash_table_free(ctx, graph->edge_table[i]);
1103 isl_hash_table_free(ctx, graph->node_table);
1104 isl_basic_set_free(graph->lp);
1107 /* For each "set" on which this function is called, increment
1108 * graph->n by one and update graph->maxvar.
1110 static isl_stat init_n_maxvar(__isl_take isl_set *set, void *user)
1112 struct isl_sched_graph *graph = user;
1113 int nvar = isl_set_dim(set, isl_dim_set);
1115 graph->n++;
1116 if (nvar > graph->maxvar)
1117 graph->maxvar = nvar;
1119 isl_set_free(set);
1121 return isl_stat_ok;
1124 /* Add the number of basic maps in "map" to *n.
1126 static isl_stat add_n_basic_map(__isl_take isl_map *map, void *user)
1128 int *n = user;
1130 *n += isl_map_n_basic_map(map);
1131 isl_map_free(map);
1133 return isl_stat_ok;
1136 /* Compute the number of rows that should be allocated for the schedule.
1137 * In particular, we need one row for each variable or one row
1138 * for each basic map in the dependences.
1139 * Note that it is practically impossible to exhaust both
1140 * the number of dependences and the number of variables.
1142 static int compute_max_row(struct isl_sched_graph *graph,
1143 __isl_keep isl_schedule_constraints *sc)
1145 enum isl_edge_type i;
1146 int n_edge;
1148 graph->n = 0;
1149 graph->maxvar = 0;
1150 if (isl_union_set_foreach_set(sc->domain, &init_n_maxvar, graph) < 0)
1151 return -1;
1152 n_edge = 0;
1153 for (i = isl_edge_first; i <= isl_edge_last; ++i)
1154 if (isl_union_map_foreach_map(sc->constraint[i],
1155 &add_n_basic_map, &n_edge) < 0)
1156 return -1;
1157 graph->max_row = n_edge + graph->maxvar;
1159 return 0;
1162 /* Does "bset" have any defining equalities for its set variables?
1164 static int has_any_defining_equality(__isl_keep isl_basic_set *bset)
1166 int i, n;
1168 if (!bset)
1169 return -1;
1171 n = isl_basic_set_dim(bset, isl_dim_set);
1172 for (i = 0; i < n; ++i) {
1173 int has;
1175 has = isl_basic_set_has_defining_equality(bset, isl_dim_set, i,
1176 NULL);
1177 if (has < 0 || has)
1178 return has;
1181 return 0;
1184 /* Add a new node to the graph representing the given space.
1185 * "nvar" is the (possibly compressed) number of variables and
1186 * may be smaller than then number of set variables in "space"
1187 * if "compressed" is set.
1188 * If "compressed" is set, then "hull" represents the constraints
1189 * that were used to derive the compression, while "compress" and
1190 * "decompress" map the original space to the compressed space and
1191 * vice versa.
1192 * If "compressed" is not set, then "hull", "compress" and "decompress"
1193 * should be NULL.
1195 static isl_stat add_node(struct isl_sched_graph *graph,
1196 __isl_take isl_space *space, int nvar, int compressed,
1197 __isl_take isl_set *hull, __isl_take isl_multi_aff *compress,
1198 __isl_take isl_multi_aff *decompress)
1200 int nparam;
1201 isl_ctx *ctx;
1202 isl_mat *sched;
1203 int *coincident;
1205 if (!space)
1206 return isl_stat_error;
1208 ctx = isl_space_get_ctx(space);
1209 nparam = isl_space_dim(space, isl_dim_param);
1210 if (!ctx->opt->schedule_parametric)
1211 nparam = 0;
1212 sched = isl_mat_alloc(ctx, 0, 1 + nparam + nvar);
1213 graph->node[graph->n].space = space;
1214 graph->node[graph->n].nvar = nvar;
1215 graph->node[graph->n].nparam = nparam;
1216 graph->node[graph->n].sched = sched;
1217 graph->node[graph->n].sched_map = NULL;
1218 coincident = isl_calloc_array(ctx, int, graph->max_row);
1219 graph->node[graph->n].coincident = coincident;
1220 graph->node[graph->n].compressed = compressed;
1221 graph->node[graph->n].hull = hull;
1222 graph->node[graph->n].compress = compress;
1223 graph->node[graph->n].decompress = decompress;
1224 graph->n++;
1226 if (!space || !sched || (graph->max_row && !coincident))
1227 return isl_stat_error;
1228 if (compressed && (!hull || !compress || !decompress))
1229 return isl_stat_error;
1231 return isl_stat_ok;
1234 /* Add a new node to the graph representing the given set.
1236 * If any of the set variables is defined by an equality, then
1237 * we perform variable compression such that we can perform
1238 * the scheduling on the compressed domain.
1240 static isl_stat extract_node(__isl_take isl_set *set, void *user)
1242 int nvar;
1243 int has_equality;
1244 isl_space *space;
1245 isl_basic_set *hull;
1246 isl_set *hull_set;
1247 isl_morph *morph;
1248 isl_multi_aff *compress, *decompress;
1249 struct isl_sched_graph *graph = user;
1251 space = isl_set_get_space(set);
1252 hull = isl_set_affine_hull(set);
1253 hull = isl_basic_set_remove_divs(hull);
1254 nvar = isl_space_dim(space, isl_dim_set);
1255 has_equality = has_any_defining_equality(hull);
1257 if (has_equality < 0)
1258 goto error;
1259 if (!has_equality) {
1260 isl_basic_set_free(hull);
1261 return add_node(graph, space, nvar, 0, NULL, NULL, NULL);
1264 morph = isl_basic_set_variable_compression(hull, isl_dim_set);
1265 nvar = isl_morph_ran_dim(morph, isl_dim_set);
1266 compress = isl_morph_get_var_multi_aff(morph);
1267 morph = isl_morph_inverse(morph);
1268 decompress = isl_morph_get_var_multi_aff(morph);
1269 isl_morph_free(morph);
1271 hull_set = isl_set_from_basic_set(hull);
1272 return add_node(graph, space, nvar, 1, hull_set, compress, decompress);
1273 error:
1274 isl_basic_set_free(hull);
1275 isl_space_free(space);
1276 return isl_stat_error;
1279 struct isl_extract_edge_data {
1280 enum isl_edge_type type;
1281 struct isl_sched_graph *graph;
1284 /* Merge edge2 into edge1, freeing the contents of edge2.
1285 * Return 0 on success and -1 on failure.
1287 * edge1 and edge2 are assumed to have the same value for the map field.
1289 static int merge_edge(struct isl_sched_edge *edge1,
1290 struct isl_sched_edge *edge2)
1292 edge1->types |= edge2->types;
1293 isl_map_free(edge2->map);
1295 if (is_condition(edge2)) {
1296 if (!edge1->tagged_condition)
1297 edge1->tagged_condition = edge2->tagged_condition;
1298 else
1299 edge1->tagged_condition =
1300 isl_union_map_union(edge1->tagged_condition,
1301 edge2->tagged_condition);
1304 if (is_conditional_validity(edge2)) {
1305 if (!edge1->tagged_validity)
1306 edge1->tagged_validity = edge2->tagged_validity;
1307 else
1308 edge1->tagged_validity =
1309 isl_union_map_union(edge1->tagged_validity,
1310 edge2->tagged_validity);
1313 if (is_condition(edge2) && !edge1->tagged_condition)
1314 return -1;
1315 if (is_conditional_validity(edge2) && !edge1->tagged_validity)
1316 return -1;
1318 return 0;
1321 /* Insert dummy tags in domain and range of "map".
1323 * In particular, if "map" is of the form
1325 * A -> B
1327 * then return
1329 * [A -> dummy_tag] -> [B -> dummy_tag]
1331 * where the dummy_tags are identical and equal to any dummy tags
1332 * introduced by any other call to this function.
1334 static __isl_give isl_map *insert_dummy_tags(__isl_take isl_map *map)
1336 static char dummy;
1337 isl_ctx *ctx;
1338 isl_id *id;
1339 isl_space *space;
1340 isl_set *domain, *range;
1342 ctx = isl_map_get_ctx(map);
1344 id = isl_id_alloc(ctx, NULL, &dummy);
1345 space = isl_space_params(isl_map_get_space(map));
1346 space = isl_space_set_from_params(space);
1347 space = isl_space_set_tuple_id(space, isl_dim_set, id);
1348 space = isl_space_map_from_set(space);
1350 domain = isl_map_wrap(map);
1351 range = isl_map_wrap(isl_map_universe(space));
1352 map = isl_map_from_domain_and_range(domain, range);
1353 map = isl_map_zip(map);
1355 return map;
1358 /* Given that at least one of "src" or "dst" is compressed, return
1359 * a map between the spaces of these nodes restricted to the affine
1360 * hull that was used in the compression.
1362 static __isl_give isl_map *extract_hull(struct isl_sched_node *src,
1363 struct isl_sched_node *dst)
1365 isl_set *dom, *ran;
1367 if (src->compressed)
1368 dom = isl_set_copy(src->hull);
1369 else
1370 dom = isl_set_universe(isl_space_copy(src->space));
1371 if (dst->compressed)
1372 ran = isl_set_copy(dst->hull);
1373 else
1374 ran = isl_set_universe(isl_space_copy(dst->space));
1376 return isl_map_from_domain_and_range(dom, ran);
1379 /* Intersect the domains of the nested relations in domain and range
1380 * of "tagged" with "map".
1382 static __isl_give isl_map *map_intersect_domains(__isl_take isl_map *tagged,
1383 __isl_keep isl_map *map)
1385 isl_set *set;
1387 tagged = isl_map_zip(tagged);
1388 set = isl_map_wrap(isl_map_copy(map));
1389 tagged = isl_map_intersect_domain(tagged, set);
1390 tagged = isl_map_zip(tagged);
1391 return tagged;
1394 /* Return a pointer to the node that lives in the domain space of "map"
1395 * or NULL if there is no such node.
1397 static struct isl_sched_node *find_domain_node(isl_ctx *ctx,
1398 struct isl_sched_graph *graph, __isl_keep isl_map *map)
1400 struct isl_sched_node *node;
1401 isl_space *space;
1403 space = isl_space_domain(isl_map_get_space(map));
1404 node = graph_find_node(ctx, graph, space);
1405 isl_space_free(space);
1407 return node;
1410 /* Return a pointer to the node that lives in the range space of "map"
1411 * or NULL if there is no such node.
1413 static struct isl_sched_node *find_range_node(isl_ctx *ctx,
1414 struct isl_sched_graph *graph, __isl_keep isl_map *map)
1416 struct isl_sched_node *node;
1417 isl_space *space;
1419 space = isl_space_range(isl_map_get_space(map));
1420 node = graph_find_node(ctx, graph, space);
1421 isl_space_free(space);
1423 return node;
1426 /* Add a new edge to the graph based on the given map
1427 * and add it to data->graph->edge_table[data->type].
1428 * If a dependence relation of a given type happens to be identical
1429 * to one of the dependence relations of a type that was added before,
1430 * then we don't create a new edge, but instead mark the original edge
1431 * as also representing a dependence of the current type.
1433 * Edges of type isl_edge_condition or isl_edge_conditional_validity
1434 * may be specified as "tagged" dependence relations. That is, "map"
1435 * may contain elements (i -> a) -> (j -> b), where i -> j denotes
1436 * the dependence on iterations and a and b are tags.
1437 * edge->map is set to the relation containing the elements i -> j,
1438 * while edge->tagged_condition and edge->tagged_validity contain
1439 * the union of all the "map" relations
1440 * for which extract_edge is called that result in the same edge->map.
1442 * If the source or the destination node is compressed, then
1443 * intersect both "map" and "tagged" with the constraints that
1444 * were used to construct the compression.
1445 * This ensures that there are no schedule constraints defined
1446 * outside of these domains, while the scheduler no longer has
1447 * any control over those outside parts.
1449 static isl_stat extract_edge(__isl_take isl_map *map, void *user)
1451 isl_ctx *ctx = isl_map_get_ctx(map);
1452 struct isl_extract_edge_data *data = user;
1453 struct isl_sched_graph *graph = data->graph;
1454 struct isl_sched_node *src, *dst;
1455 struct isl_sched_edge *edge;
1456 isl_map *tagged = NULL;
1458 if (data->type == isl_edge_condition ||
1459 data->type == isl_edge_conditional_validity) {
1460 if (isl_map_can_zip(map)) {
1461 tagged = isl_map_copy(map);
1462 map = isl_set_unwrap(isl_map_domain(isl_map_zip(map)));
1463 } else {
1464 tagged = insert_dummy_tags(isl_map_copy(map));
1468 src = find_domain_node(ctx, graph, map);
1469 dst = find_range_node(ctx, graph, map);
1471 if (!src || !dst) {
1472 isl_map_free(map);
1473 isl_map_free(tagged);
1474 return isl_stat_ok;
1477 if (src->compressed || dst->compressed) {
1478 isl_map *hull;
1479 hull = extract_hull(src, dst);
1480 if (tagged)
1481 tagged = map_intersect_domains(tagged, hull);
1482 map = isl_map_intersect(map, hull);
1485 graph->edge[graph->n_edge].src = src;
1486 graph->edge[graph->n_edge].dst = dst;
1487 graph->edge[graph->n_edge].map = map;
1488 graph->edge[graph->n_edge].types = 0;
1489 graph->edge[graph->n_edge].tagged_condition = NULL;
1490 graph->edge[graph->n_edge].tagged_validity = NULL;
1491 set_type(&graph->edge[graph->n_edge], data->type);
1492 if (data->type == isl_edge_condition)
1493 graph->edge[graph->n_edge].tagged_condition =
1494 isl_union_map_from_map(tagged);
1495 if (data->type == isl_edge_conditional_validity)
1496 graph->edge[graph->n_edge].tagged_validity =
1497 isl_union_map_from_map(tagged);
1499 edge = graph_find_matching_edge(graph, &graph->edge[graph->n_edge]);
1500 if (!edge) {
1501 graph->n_edge++;
1502 return isl_stat_error;
1504 if (edge == &graph->edge[graph->n_edge])
1505 return graph_edge_table_add(ctx, graph, data->type,
1506 &graph->edge[graph->n_edge++]);
1508 if (merge_edge(edge, &graph->edge[graph->n_edge]) < 0)
1509 return -1;
1511 return graph_edge_table_add(ctx, graph, data->type, edge);
1514 /* Initialize the schedule graph "graph" from the schedule constraints "sc".
1516 * The context is included in the domain before the nodes of
1517 * the graphs are extracted in order to be able to exploit
1518 * any possible additional equalities.
1519 * Note that this intersection is only performed locally here.
1521 static isl_stat graph_init(struct isl_sched_graph *graph,
1522 __isl_keep isl_schedule_constraints *sc)
1524 isl_ctx *ctx;
1525 isl_union_set *domain;
1526 struct isl_extract_edge_data data;
1527 enum isl_edge_type i;
1528 isl_stat r;
1530 if (!sc)
1531 return isl_stat_error;
1533 ctx = isl_schedule_constraints_get_ctx(sc);
1535 domain = isl_schedule_constraints_get_domain(sc);
1536 graph->n = isl_union_set_n_set(domain);
1537 isl_union_set_free(domain);
1539 if (graph_alloc(ctx, graph, graph->n,
1540 isl_schedule_constraints_n_map(sc)) < 0)
1541 return isl_stat_error;
1543 if (compute_max_row(graph, sc) < 0)
1544 return isl_stat_error;
1545 graph->root = 1;
1546 graph->n = 0;
1547 domain = isl_schedule_constraints_get_domain(sc);
1548 domain = isl_union_set_intersect_params(domain,
1549 isl_set_copy(sc->context));
1550 r = isl_union_set_foreach_set(domain, &extract_node, graph);
1551 isl_union_set_free(domain);
1552 if (r < 0)
1553 return isl_stat_error;
1554 if (graph_init_table(ctx, graph) < 0)
1555 return isl_stat_error;
1556 for (i = isl_edge_first; i <= isl_edge_last; ++i)
1557 graph->max_edge[i] = isl_union_map_n_map(sc->constraint[i]);
1558 if (graph_init_edge_tables(ctx, graph) < 0)
1559 return isl_stat_error;
1560 graph->n_edge = 0;
1561 data.graph = graph;
1562 for (i = isl_edge_first; i <= isl_edge_last; ++i) {
1563 data.type = i;
1564 if (isl_union_map_foreach_map(sc->constraint[i],
1565 &extract_edge, &data) < 0)
1566 return isl_stat_error;
1569 return isl_stat_ok;
1572 /* Check whether there is any dependence from node[j] to node[i]
1573 * or from node[i] to node[j].
1575 static isl_bool node_follows_weak(int i, int j, void *user)
1577 isl_bool f;
1578 struct isl_sched_graph *graph = user;
1580 f = graph_has_any_edge(graph, &graph->node[j], &graph->node[i]);
1581 if (f < 0 || f)
1582 return f;
1583 return graph_has_any_edge(graph, &graph->node[i], &graph->node[j]);
1586 /* Check whether there is a (conditional) validity dependence from node[j]
1587 * to node[i], forcing node[i] to follow node[j].
1589 static isl_bool node_follows_strong(int i, int j, void *user)
1591 struct isl_sched_graph *graph = user;
1593 return graph_has_validity_edge(graph, &graph->node[j], &graph->node[i]);
1596 /* Use Tarjan's algorithm for computing the strongly connected components
1597 * in the dependence graph only considering those edges defined by "follows".
1599 static int detect_ccs(isl_ctx *ctx, struct isl_sched_graph *graph,
1600 isl_bool (*follows)(int i, int j, void *user))
1602 int i, n;
1603 struct isl_tarjan_graph *g = NULL;
1605 g = isl_tarjan_graph_init(ctx, graph->n, follows, graph);
1606 if (!g)
1607 return -1;
1609 graph->scc = 0;
1610 i = 0;
1611 n = graph->n;
1612 while (n) {
1613 while (g->order[i] != -1) {
1614 graph->node[g->order[i]].scc = graph->scc;
1615 --n;
1616 ++i;
1618 ++i;
1619 graph->scc++;
1622 isl_tarjan_graph_free(g);
1624 return 0;
1627 /* Apply Tarjan's algorithm to detect the strongly connected components
1628 * in the dependence graph.
1629 * Only consider the (conditional) validity dependences and clear "weak".
1631 static int detect_sccs(isl_ctx *ctx, struct isl_sched_graph *graph)
1633 graph->weak = 0;
1634 return detect_ccs(ctx, graph, &node_follows_strong);
1637 /* Apply Tarjan's algorithm to detect the (weakly) connected components
1638 * in the dependence graph.
1639 * Consider all dependences and set "weak".
1641 static int detect_wccs(isl_ctx *ctx, struct isl_sched_graph *graph)
1643 graph->weak = 1;
1644 return detect_ccs(ctx, graph, &node_follows_weak);
1647 static int cmp_scc(const void *a, const void *b, void *data)
1649 struct isl_sched_graph *graph = data;
1650 const int *i1 = a;
1651 const int *i2 = b;
1653 return graph->node[*i1].scc - graph->node[*i2].scc;
1656 /* Sort the elements of graph->sorted according to the corresponding SCCs.
1658 static int sort_sccs(struct isl_sched_graph *graph)
1660 return isl_sort(graph->sorted, graph->n, sizeof(int), &cmp_scc, graph);
1663 /* Given a dependence relation R from "node" to itself,
1664 * construct the set of coefficients of valid constraints for elements
1665 * in that dependence relation.
1666 * In particular, the result contains tuples of coefficients
1667 * c_0, c_n, c_x such that
1669 * c_0 + c_n n + c_x y - c_x x >= 0 for each (x,y) in R
1671 * or, equivalently,
1673 * c_0 + c_n n + c_x d >= 0 for each d in delta R = { y - x | (x,y) in R }
1675 * We choose here to compute the dual of delta R.
1676 * Alternatively, we could have computed the dual of R, resulting
1677 * in a set of tuples c_0, c_n, c_x, c_y, and then
1678 * plugged in (c_0, c_n, c_x, -c_x).
1680 * If "node" has been compressed, then the dependence relation
1681 * is also compressed before the set of coefficients is computed.
1683 static __isl_give isl_basic_set *intra_coefficients(
1684 struct isl_sched_graph *graph, struct isl_sched_node *node,
1685 __isl_take isl_map *map)
1687 isl_set *delta;
1688 isl_map *key;
1689 isl_basic_set *coef;
1690 isl_maybe_isl_basic_set m;
1692 m = isl_map_to_basic_set_try_get(graph->intra_hmap, map);
1693 if (m.valid < 0 || m.valid) {
1694 isl_map_free(map);
1695 return m.value;
1698 key = isl_map_copy(map);
1699 if (node->compressed) {
1700 map = isl_map_preimage_domain_multi_aff(map,
1701 isl_multi_aff_copy(node->decompress));
1702 map = isl_map_preimage_range_multi_aff(map,
1703 isl_multi_aff_copy(node->decompress));
1705 delta = isl_set_remove_divs(isl_map_deltas(map));
1706 coef = isl_set_coefficients(delta);
1707 graph->intra_hmap = isl_map_to_basic_set_set(graph->intra_hmap, key,
1708 isl_basic_set_copy(coef));
1710 return coef;
1713 /* Given a dependence relation R, construct the set of coefficients
1714 * of valid constraints for elements in that dependence relation.
1715 * In particular, the result contains tuples of coefficients
1716 * c_0, c_n, c_x, c_y such that
1718 * c_0 + c_n n + c_x x + c_y y >= 0 for each (x,y) in R
1720 * If the source or destination nodes of "edge" have been compressed,
1721 * then the dependence relation is also compressed before
1722 * the set of coefficients is computed.
1724 static __isl_give isl_basic_set *inter_coefficients(
1725 struct isl_sched_graph *graph, struct isl_sched_edge *edge,
1726 __isl_take isl_map *map)
1728 isl_set *set;
1729 isl_map *key;
1730 isl_basic_set *coef;
1731 isl_maybe_isl_basic_set m;
1733 m = isl_map_to_basic_set_try_get(graph->inter_hmap, map);
1734 if (m.valid < 0 || m.valid) {
1735 isl_map_free(map);
1736 return m.value;
1739 key = isl_map_copy(map);
1740 if (edge->src->compressed)
1741 map = isl_map_preimage_domain_multi_aff(map,
1742 isl_multi_aff_copy(edge->src->decompress));
1743 if (edge->dst->compressed)
1744 map = isl_map_preimage_range_multi_aff(map,
1745 isl_multi_aff_copy(edge->dst->decompress));
1746 set = isl_map_wrap(isl_map_remove_divs(map));
1747 coef = isl_set_coefficients(set);
1748 graph->inter_hmap = isl_map_to_basic_set_set(graph->inter_hmap, key,
1749 isl_basic_set_copy(coef));
1751 return coef;
1754 /* Return the position of the coefficients of the variables in
1755 * the coefficients constraints "coef".
1757 * The space of "coef" is of the form
1759 * { coefficients[[cst, params] -> S] }
1761 * Return the position of S.
1763 static int coef_var_offset(__isl_keep isl_basic_set *coef)
1765 int offset;
1766 isl_space *space;
1768 space = isl_space_unwrap(isl_basic_set_get_space(coef));
1769 offset = isl_space_dim(space, isl_dim_in);
1770 isl_space_free(space);
1772 return offset;
1775 /* Return the offset of the coefficients of the variables of "node"
1776 * within the (I)LP.
1778 * Within each node, the coefficients have the following order:
1779 * - c_i_0
1780 * - c_i_n (if parametric)
1781 * - positive and negative parts of c_i_x
1783 static int node_var_coef_offset(struct isl_sched_node *node)
1785 return node->start + 1 + node->nparam;
1788 /* Construct an isl_dim_map for mapping constraints on coefficients
1789 * for "node" to the corresponding positions in graph->lp.
1790 * "offset" is the offset of the coefficients for the variables
1791 * in the input constraints.
1792 * "s" is the sign of the mapping.
1794 * The input constraints are given in terms of the coefficients (c_0, c_n, c_x).
1795 * The mapping produced by this function essentially plugs in
1796 * (0, 0, c_i_x^+ - c_i_x^-) if s = 1 and
1797 * (0, 0, -c_i_x^+ + c_i_x^-) if s = -1.
1798 * In graph->lp, the c_i_x^- appear before their c_i_x^+ counterpart.
1800 * The caller can extend the mapping to also map the other coefficients
1801 * (and therefore not plug in 0).
1803 static __isl_give isl_dim_map *intra_dim_map(isl_ctx *ctx,
1804 struct isl_sched_graph *graph, struct isl_sched_node *node,
1805 int offset, int s)
1807 int pos;
1808 unsigned total;
1809 isl_dim_map *dim_map;
1811 total = isl_basic_set_total_dim(graph->lp);
1812 pos = node_var_coef_offset(node);
1813 dim_map = isl_dim_map_alloc(ctx, total);
1814 isl_dim_map_range(dim_map, pos, 2, offset, 1, node->nvar, -s);
1815 isl_dim_map_range(dim_map, pos + 1, 2, offset, 1, node->nvar, s);
1817 return dim_map;
1820 /* Construct an isl_dim_map for mapping constraints on coefficients
1821 * for "src" (node i) and "dst" (node j) to the corresponding positions
1822 * in graph->lp.
1823 * "offset" is the offset of the coefficients for the variables of "src"
1824 * in the input constraints.
1825 * "s" is the sign of the mapping.
1827 * The input constraints are given in terms of the coefficients
1828 * (c_0, c_n, c_x, c_y).
1829 * The mapping produced by this function essentially plugs in
1830 * (c_j_0 - c_i_0, c_j_n - c_i_n,
1831 * c_j_x^+ - c_j_x^-, -(c_i_x^+ - c_i_x^-)) if s = 1 and
1832 * (-c_j_0 + c_i_0, -c_j_n + c_i_n,
1833 * - (c_j_x^+ - c_j_x^-), c_i_x^+ - c_i_x^-) if s = -1.
1834 * In graph->lp, the c_*^- appear before their c_*^+ counterpart.
1836 * The caller can further extend the mapping.
1838 static __isl_give isl_dim_map *inter_dim_map(isl_ctx *ctx,
1839 struct isl_sched_graph *graph, struct isl_sched_node *src,
1840 struct isl_sched_node *dst, int offset, int s)
1842 int pos;
1843 unsigned total;
1844 isl_dim_map *dim_map;
1846 total = isl_basic_set_total_dim(graph->lp);
1847 dim_map = isl_dim_map_alloc(ctx, total);
1849 isl_dim_map_range(dim_map, dst->start, 0, 0, 0, 1, s);
1850 isl_dim_map_range(dim_map, dst->start + 1, 1, 1, 1, dst->nparam, s);
1851 pos = node_var_coef_offset(dst);
1852 isl_dim_map_range(dim_map, pos, 2, offset + src->nvar, 1,
1853 dst->nvar, -s);
1854 isl_dim_map_range(dim_map, pos + 1, 2, offset + src->nvar, 1,
1855 dst->nvar, s);
1857 isl_dim_map_range(dim_map, src->start, 0, 0, 0, 1, -s);
1858 isl_dim_map_range(dim_map, src->start + 1, 1, 1, 1, src->nparam, -s);
1859 pos = node_var_coef_offset(src);
1860 isl_dim_map_range(dim_map, pos, 2, offset, 1, src->nvar, s);
1861 isl_dim_map_range(dim_map, pos + 1, 2, offset, 1, src->nvar, -s);
1863 return dim_map;
1866 /* Add constraints to graph->lp that force validity for the given
1867 * dependence from a node i to itself.
1868 * That is, add constraints that enforce
1870 * (c_i_0 + c_i_n n + c_i_x y) - (c_i_0 + c_i_n n + c_i_x x)
1871 * = c_i_x (y - x) >= 0
1873 * for each (x,y) in R.
1874 * We obtain general constraints on coefficients (c_0, c_n, c_x)
1875 * of valid constraints for (y - x) and then plug in (0, 0, c_i_x^+ - c_i_x^-),
1876 * where c_i_x = c_i_x^+ - c_i_x^-, with c_i_x^+ and c_i_x^- non-negative.
1877 * In graph->lp, the c_i_x^- appear before their c_i_x^+ counterpart.
1879 * Actually, we do not construct constraints for the c_i_x themselves,
1880 * but for the coefficients of c_i_x written as a linear combination
1881 * of the columns in node->cmap.
1883 static isl_stat add_intra_validity_constraints(struct isl_sched_graph *graph,
1884 struct isl_sched_edge *edge)
1886 int offset;
1887 isl_map *map = isl_map_copy(edge->map);
1888 isl_ctx *ctx = isl_map_get_ctx(map);
1889 isl_dim_map *dim_map;
1890 isl_basic_set *coef;
1891 struct isl_sched_node *node = edge->src;
1893 coef = intra_coefficients(graph, node, map);
1895 offset = coef_var_offset(coef);
1897 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
1898 offset, isl_mat_copy(node->cmap));
1899 if (!coef)
1900 return isl_stat_error;
1902 dim_map = intra_dim_map(ctx, graph, node, offset, 1);
1903 graph->lp = isl_basic_set_extend_constraints(graph->lp,
1904 coef->n_eq, coef->n_ineq);
1905 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
1906 coef, dim_map);
1908 return isl_stat_ok;
1911 /* Add constraints to graph->lp that force validity for the given
1912 * dependence from node i to node j.
1913 * That is, add constraints that enforce
1915 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x) >= 0
1917 * for each (x,y) in R.
1918 * We obtain general constraints on coefficients (c_0, c_n, c_x, c_y)
1919 * of valid constraints for R and then plug in
1920 * (c_j_0 - c_i_0, c_j_n - c_i_n, c_j_x^+ - c_j_x^- - (c_i_x^+ - c_i_x^-)),
1921 * where c_* = c_*^+ - c_*^-, with c_*^+ and c_*^- non-negative.
1922 * In graph->lp, the c_*^- appear before their c_*^+ counterpart.
1924 * Actually, we do not construct constraints for the c_*_x themselves,
1925 * but for the coefficients of c_*_x written as a linear combination
1926 * of the columns in node->cmap.
1928 static isl_stat add_inter_validity_constraints(struct isl_sched_graph *graph,
1929 struct isl_sched_edge *edge)
1931 int offset;
1932 isl_map *map = isl_map_copy(edge->map);
1933 isl_ctx *ctx = isl_map_get_ctx(map);
1934 isl_dim_map *dim_map;
1935 isl_basic_set *coef;
1936 struct isl_sched_node *src = edge->src;
1937 struct isl_sched_node *dst = edge->dst;
1939 coef = inter_coefficients(graph, edge, map);
1941 offset = coef_var_offset(coef);
1943 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
1944 offset, isl_mat_copy(src->cmap));
1945 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
1946 offset + src->nvar, isl_mat_copy(dst->cmap));
1947 if (!coef)
1948 return isl_stat_error;
1950 dim_map = inter_dim_map(ctx, graph, src, dst, offset, 1);
1952 edge->start = graph->lp->n_ineq;
1953 graph->lp = isl_basic_set_extend_constraints(graph->lp,
1954 coef->n_eq, coef->n_ineq);
1955 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
1956 coef, dim_map);
1957 if (!graph->lp)
1958 return isl_stat_error;
1959 edge->end = graph->lp->n_ineq;
1961 return isl_stat_ok;
1964 /* Add constraints to graph->lp that bound the dependence distance for the given
1965 * dependence from a node i to itself.
1966 * If s = 1, we add the constraint
1968 * c_i_x (y - x) <= m_0 + m_n n
1970 * or
1972 * -c_i_x (y - x) + m_0 + m_n n >= 0
1974 * for each (x,y) in R.
1975 * If s = -1, we add the constraint
1977 * -c_i_x (y - x) <= m_0 + m_n n
1979 * or
1981 * c_i_x (y - x) + m_0 + m_n n >= 0
1983 * for each (x,y) in R.
1984 * We obtain general constraints on coefficients (c_0, c_n, c_x)
1985 * of valid constraints for (y - x) and then plug in (m_0, m_n, -s * c_i_x),
1986 * with each coefficient (except m_0) represented as a pair of non-negative
1987 * coefficients.
1989 * Actually, we do not construct constraints for the c_i_x themselves,
1990 * but for the coefficients of c_i_x written as a linear combination
1991 * of the columns in node->cmap.
1994 * If "local" is set, then we add constraints
1996 * c_i_x (y - x) <= 0
1998 * or
2000 * -c_i_x (y - x) <= 0
2002 * instead, forcing the dependence distance to be (less than or) equal to 0.
2003 * That is, we plug in (0, 0, -s * c_i_x),
2004 * Note that dependences marked local are treated as validity constraints
2005 * by add_all_validity_constraints and therefore also have
2006 * their distances bounded by 0 from below.
2008 static isl_stat add_intra_proximity_constraints(struct isl_sched_graph *graph,
2009 struct isl_sched_edge *edge, int s, int local)
2011 int offset;
2012 unsigned nparam;
2013 isl_map *map = isl_map_copy(edge->map);
2014 isl_ctx *ctx = isl_map_get_ctx(map);
2015 isl_dim_map *dim_map;
2016 isl_basic_set *coef;
2017 struct isl_sched_node *node = edge->src;
2019 coef = intra_coefficients(graph, node, map);
2021 offset = coef_var_offset(coef);
2023 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
2024 offset, isl_mat_copy(node->cmap));
2025 if (!coef)
2026 return isl_stat_error;
2028 nparam = isl_space_dim(node->space, isl_dim_param);
2029 dim_map = intra_dim_map(ctx, graph, node, offset, -s);
2031 if (!local) {
2032 isl_dim_map_range(dim_map, 1, 0, 0, 0, 1, 1);
2033 isl_dim_map_range(dim_map, 4, 2, 1, 1, nparam, -1);
2034 isl_dim_map_range(dim_map, 5, 2, 1, 1, nparam, 1);
2036 graph->lp = isl_basic_set_extend_constraints(graph->lp,
2037 coef->n_eq, coef->n_ineq);
2038 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
2039 coef, dim_map);
2041 return isl_stat_ok;
2044 /* Add constraints to graph->lp that bound the dependence distance for the given
2045 * dependence from node i to node j.
2046 * If s = 1, we add the constraint
2048 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x)
2049 * <= m_0 + m_n n
2051 * or
2053 * -(c_j_0 + c_j_n n + c_j_x y) + (c_i_0 + c_i_n n + c_i_x x) +
2054 * m_0 + m_n n >= 0
2056 * for each (x,y) in R.
2057 * If s = -1, we add the constraint
2059 * -((c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x))
2060 * <= m_0 + m_n n
2062 * or
2064 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x) +
2065 * m_0 + m_n n >= 0
2067 * for each (x,y) in R.
2068 * We obtain general constraints on coefficients (c_0, c_n, c_x, c_y)
2069 * of valid constraints for R and then plug in
2070 * (m_0 - s*c_j_0 + s*c_i_0, m_n - s*c_j_n + s*c_i_n,
2071 * -s*c_j_x+s*c_i_x)
2072 * with each coefficient (except m_0, c_*_0 and c_*_n)
2073 * represented as a pair of non-negative coefficients.
2075 * Actually, we do not construct constraints for the c_*_x themselves,
2076 * but for the coefficients of c_*_x written as a linear combination
2077 * of the columns in node->cmap.
2080 * If "local" is set, then we add constraints
2082 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x) <= 0
2084 * or
2086 * -((c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x)) <= 0
2088 * instead, forcing the dependence distance to be (less than or) equal to 0.
2089 * That is, we plug in
2090 * (-s*c_j_0 + s*c_i_0, -s*c_j_n + s*c_i_n, -s*c_j_x+s*c_i_x).
2091 * Note that dependences marked local are treated as validity constraints
2092 * by add_all_validity_constraints and therefore also have
2093 * their distances bounded by 0 from below.
2095 static isl_stat add_inter_proximity_constraints(struct isl_sched_graph *graph,
2096 struct isl_sched_edge *edge, int s, int local)
2098 int offset;
2099 unsigned nparam;
2100 isl_map *map = isl_map_copy(edge->map);
2101 isl_ctx *ctx = isl_map_get_ctx(map);
2102 isl_dim_map *dim_map;
2103 isl_basic_set *coef;
2104 struct isl_sched_node *src = edge->src;
2105 struct isl_sched_node *dst = edge->dst;
2107 coef = inter_coefficients(graph, edge, map);
2109 offset = coef_var_offset(coef);
2111 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
2112 offset, isl_mat_copy(src->cmap));
2113 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
2114 offset + src->nvar, isl_mat_copy(dst->cmap));
2115 if (!coef)
2116 return isl_stat_error;
2118 nparam = isl_space_dim(src->space, isl_dim_param);
2119 dim_map = inter_dim_map(ctx, graph, src, dst, offset, -s);
2121 if (!local) {
2122 isl_dim_map_range(dim_map, 1, 0, 0, 0, 1, 1);
2123 isl_dim_map_range(dim_map, 4, 2, 1, 1, nparam, -1);
2124 isl_dim_map_range(dim_map, 5, 2, 1, 1, nparam, 1);
2127 graph->lp = isl_basic_set_extend_constraints(graph->lp,
2128 coef->n_eq, coef->n_ineq);
2129 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
2130 coef, dim_map);
2132 return isl_stat_ok;
2135 /* Add all validity constraints to graph->lp.
2137 * An edge that is forced to be local needs to have its dependence
2138 * distances equal to zero. We take care of bounding them by 0 from below
2139 * here. add_all_proximity_constraints takes care of bounding them by 0
2140 * from above.
2142 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
2143 * Otherwise, we ignore them.
2145 static int add_all_validity_constraints(struct isl_sched_graph *graph,
2146 int use_coincidence)
2148 int i;
2150 for (i = 0; i < graph->n_edge; ++i) {
2151 struct isl_sched_edge *edge= &graph->edge[i];
2152 int local;
2154 local = is_local(edge) ||
2155 (is_coincidence(edge) && use_coincidence);
2156 if (!is_validity(edge) && !local)
2157 continue;
2158 if (edge->src != edge->dst)
2159 continue;
2160 if (add_intra_validity_constraints(graph, edge) < 0)
2161 return -1;
2164 for (i = 0; i < graph->n_edge; ++i) {
2165 struct isl_sched_edge *edge = &graph->edge[i];
2166 int local;
2168 local = is_local(edge) ||
2169 (is_coincidence(edge) && use_coincidence);
2170 if (!is_validity(edge) && !local)
2171 continue;
2172 if (edge->src == edge->dst)
2173 continue;
2174 if (add_inter_validity_constraints(graph, edge) < 0)
2175 return -1;
2178 return 0;
2181 /* Add constraints to graph->lp that bound the dependence distance
2182 * for all dependence relations.
2183 * If a given proximity dependence is identical to a validity
2184 * dependence, then the dependence distance is already bounded
2185 * from below (by zero), so we only need to bound the distance
2186 * from above. (This includes the case of "local" dependences
2187 * which are treated as validity dependence by add_all_validity_constraints.)
2188 * Otherwise, we need to bound the distance both from above and from below.
2190 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
2191 * Otherwise, we ignore them.
2193 static int add_all_proximity_constraints(struct isl_sched_graph *graph,
2194 int use_coincidence)
2196 int i;
2198 for (i = 0; i < graph->n_edge; ++i) {
2199 struct isl_sched_edge *edge= &graph->edge[i];
2200 int local;
2202 local = is_local(edge) ||
2203 (is_coincidence(edge) && use_coincidence);
2204 if (!is_proximity(edge) && !local)
2205 continue;
2206 if (edge->src == edge->dst &&
2207 add_intra_proximity_constraints(graph, edge, 1, local) < 0)
2208 return -1;
2209 if (edge->src != edge->dst &&
2210 add_inter_proximity_constraints(graph, edge, 1, local) < 0)
2211 return -1;
2212 if (is_validity(edge) || local)
2213 continue;
2214 if (edge->src == edge->dst &&
2215 add_intra_proximity_constraints(graph, edge, -1, 0) < 0)
2216 return -1;
2217 if (edge->src != edge->dst &&
2218 add_inter_proximity_constraints(graph, edge, -1, 0) < 0)
2219 return -1;
2222 return 0;
2225 /* Compute a basis for the rows in the linear part of the schedule
2226 * and extend this basis to a full basis. The remaining rows
2227 * can then be used to force linear independence from the rows
2228 * in the schedule.
2230 * In particular, given the schedule rows S, we compute
2232 * S = H Q
2233 * S U = H
2235 * with H the Hermite normal form of S. That is, all but the
2236 * first rank columns of H are zero and so each row in S is
2237 * a linear combination of the first rank rows of Q.
2238 * The matrix Q is then transposed because we will write the
2239 * coefficients of the next schedule row as a column vector s
2240 * and express this s as a linear combination s = Q c of the
2241 * computed basis.
2242 * Similarly, the matrix U is transposed such that we can
2243 * compute the coefficients c = U s from a schedule row s.
2245 static int node_update_cmap(struct isl_sched_node *node)
2247 isl_mat *H, *U, *Q;
2248 int n_row = isl_mat_rows(node->sched);
2250 H = isl_mat_sub_alloc(node->sched, 0, n_row,
2251 1 + node->nparam, node->nvar);
2253 H = isl_mat_left_hermite(H, 0, &U, &Q);
2254 isl_mat_free(node->cmap);
2255 isl_mat_free(node->cinv);
2256 isl_mat_free(node->ctrans);
2257 node->ctrans = isl_mat_copy(Q);
2258 node->cmap = isl_mat_transpose(Q);
2259 node->cinv = isl_mat_transpose(U);
2260 node->rank = isl_mat_initial_non_zero_cols(H);
2261 isl_mat_free(H);
2263 if (!node->cmap || !node->cinv || !node->ctrans || node->rank < 0)
2264 return -1;
2265 return 0;
2268 /* Is "edge" marked as a validity or a conditional validity edge?
2270 static int is_any_validity(struct isl_sched_edge *edge)
2272 return is_validity(edge) || is_conditional_validity(edge);
2275 /* How many times should we count the constraints in "edge"?
2277 * If carry is set, then we are counting the number of
2278 * (validity or conditional validity) constraints that will be added
2279 * in setup_carry_lp and we count each edge exactly once.
2281 * Otherwise, we count as follows
2282 * validity -> 1 (>= 0)
2283 * validity+proximity -> 2 (>= 0 and upper bound)
2284 * proximity -> 2 (lower and upper bound)
2285 * local(+any) -> 2 (>= 0 and <= 0)
2287 * If an edge is only marked conditional_validity then it counts
2288 * as zero since it is only checked afterwards.
2290 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
2291 * Otherwise, we ignore them.
2293 static int edge_multiplicity(struct isl_sched_edge *edge, int carry,
2294 int use_coincidence)
2296 if (carry)
2297 return 1;
2298 if (is_proximity(edge) || is_local(edge))
2299 return 2;
2300 if (use_coincidence && is_coincidence(edge))
2301 return 2;
2302 if (is_validity(edge))
2303 return 1;
2304 return 0;
2307 /* Count the number of equality and inequality constraints
2308 * that will be added for the given map.
2310 * "use_coincidence" is set if we should take into account coincidence edges.
2312 static int count_map_constraints(struct isl_sched_graph *graph,
2313 struct isl_sched_edge *edge, __isl_take isl_map *map,
2314 int *n_eq, int *n_ineq, int carry, int use_coincidence)
2316 isl_basic_set *coef;
2317 int f = edge_multiplicity(edge, carry, use_coincidence);
2319 if (f == 0) {
2320 isl_map_free(map);
2321 return 0;
2324 if (edge->src == edge->dst)
2325 coef = intra_coefficients(graph, edge->src, map);
2326 else
2327 coef = inter_coefficients(graph, edge, map);
2328 if (!coef)
2329 return -1;
2330 *n_eq += f * coef->n_eq;
2331 *n_ineq += f * coef->n_ineq;
2332 isl_basic_set_free(coef);
2334 return 0;
2337 /* Count the number of equality and inequality constraints
2338 * that will be added to the main lp problem.
2339 * We count as follows
2340 * validity -> 1 (>= 0)
2341 * validity+proximity -> 2 (>= 0 and upper bound)
2342 * proximity -> 2 (lower and upper bound)
2343 * local(+any) -> 2 (>= 0 and <= 0)
2345 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
2346 * Otherwise, we ignore them.
2348 static int count_constraints(struct isl_sched_graph *graph,
2349 int *n_eq, int *n_ineq, int use_coincidence)
2351 int i;
2353 *n_eq = *n_ineq = 0;
2354 for (i = 0; i < graph->n_edge; ++i) {
2355 struct isl_sched_edge *edge= &graph->edge[i];
2356 isl_map *map = isl_map_copy(edge->map);
2358 if (count_map_constraints(graph, edge, map, n_eq, n_ineq,
2359 0, use_coincidence) < 0)
2360 return -1;
2363 return 0;
2366 /* Count the number of constraints that will be added by
2367 * add_bound_constant_constraints to bound the values of the constant terms
2368 * and increment *n_eq and *n_ineq accordingly.
2370 * In practice, add_bound_constant_constraints only adds inequalities.
2372 static isl_stat count_bound_constant_constraints(isl_ctx *ctx,
2373 struct isl_sched_graph *graph, int *n_eq, int *n_ineq)
2375 if (isl_options_get_schedule_max_constant_term(ctx) == -1)
2376 return isl_stat_ok;
2378 *n_ineq += graph->n;
2380 return isl_stat_ok;
2383 /* Add constraints to bound the values of the constant terms in the schedule,
2384 * if requested by the user.
2386 * The maximal value of the constant terms is defined by the option
2387 * "schedule_max_constant_term".
2389 * Within each node, the coefficients have the following order:
2390 * - c_i_0
2391 * - c_i_n (if parametric)
2392 * - positive and negative parts of c_i_x
2394 static isl_stat add_bound_constant_constraints(isl_ctx *ctx,
2395 struct isl_sched_graph *graph)
2397 int i, k;
2398 int max;
2399 int total;
2401 max = isl_options_get_schedule_max_constant_term(ctx);
2402 if (max == -1)
2403 return isl_stat_ok;
2405 total = isl_basic_set_dim(graph->lp, isl_dim_set);
2407 for (i = 0; i < graph->n; ++i) {
2408 struct isl_sched_node *node = &graph->node[i];
2409 k = isl_basic_set_alloc_inequality(graph->lp);
2410 if (k < 0)
2411 return isl_stat_error;
2412 isl_seq_clr(graph->lp->ineq[k], 1 + total);
2413 isl_int_set_si(graph->lp->ineq[k][1 + node->start], -1);
2414 isl_int_set_si(graph->lp->ineq[k][0], max);
2417 return isl_stat_ok;
2420 /* Count the number of constraints that will be added by
2421 * add_bound_coefficient_constraints and increment *n_eq and *n_ineq
2422 * accordingly.
2424 * In practice, add_bound_coefficient_constraints only adds inequalities.
2426 static int count_bound_coefficient_constraints(isl_ctx *ctx,
2427 struct isl_sched_graph *graph, int *n_eq, int *n_ineq)
2429 int i;
2431 if (isl_options_get_schedule_max_coefficient(ctx) == -1)
2432 return 0;
2434 for (i = 0; i < graph->n; ++i)
2435 *n_ineq += graph->node[i].nparam + 2 * graph->node[i].nvar;
2437 return 0;
2440 /* Add constraints to graph->lp that bound the values of
2441 * the variable and parameter schedule coefficients of "node" to "max".
2443 * For parameter coefficients, this amounts to adding a constraint
2445 * c_n <= max
2447 * i.e.,
2449 * -c_n + max >= 0
2451 * The variables coefficients are, however, not represented directly.
2452 * Instead, the variables coefficients c_x are written as a linear
2453 * combination c_x = cmap c_z of some other coefficients c_z,
2454 * which are in turn encoded as c_z = c_z^+ - c_z^-.
2455 * Let a_j be the elements of row i of node->cmap, then
2457 * -max <= c_x_i <= max
2459 * is encoded as
2461 * -max <= \sum_j a_j (c_z_j^+ - c_z_j^-) <= max
2463 * or
2465 * -\sum_j a_j (c_z_j^+ - c_z_j^-) + max >= 0
2466 * \sum_j a_j (c_z_j^+ - c_z_j^-) + max >= 0
2468 static isl_stat node_add_coefficient_constraints(isl_ctx *ctx,
2469 struct isl_sched_graph *graph, struct isl_sched_node *node, int max)
2471 int i, j, k;
2472 int total;
2473 isl_vec *ineq;
2475 total = isl_basic_set_dim(graph->lp, isl_dim_set);
2477 for (j = 0; j < node->nparam; ++j) {
2478 int dim;
2479 k = isl_basic_set_alloc_inequality(graph->lp);
2480 if (k < 0)
2481 return isl_stat_error;
2482 dim = 1 + node->start + 1 + j;
2483 isl_seq_clr(graph->lp->ineq[k], 1 + total);
2484 isl_int_set_si(graph->lp->ineq[k][dim], -1);
2485 isl_int_set_si(graph->lp->ineq[k][0], max);
2488 ineq = isl_vec_alloc(ctx, 1 + total);
2489 ineq = isl_vec_clr(ineq);
2490 if (!ineq)
2491 return isl_stat_error;
2492 for (i = 0; i < node->nvar; ++i) {
2493 int pos = 1 + node_var_coef_offset(node);
2495 for (j = 0; j < node->nvar; ++j) {
2496 isl_int_set(ineq->el[pos + 2 * j],
2497 node->cmap->row[i][j]);
2498 isl_int_neg(ineq->el[pos + 2 * j + 1],
2499 node->cmap->row[i][j]);
2501 isl_int_set_si(ineq->el[0], max);
2503 k = isl_basic_set_alloc_inequality(graph->lp);
2504 if (k < 0)
2505 goto error;
2506 isl_seq_cpy(graph->lp->ineq[k], ineq->el, 1 + total);
2508 isl_seq_neg(ineq->el + pos, ineq->el + pos, 2 * node->nvar);
2509 k = isl_basic_set_alloc_inequality(graph->lp);
2510 if (k < 0)
2511 goto error;
2512 isl_seq_cpy(graph->lp->ineq[k], ineq->el, 1 + total);
2514 isl_vec_free(ineq);
2516 return isl_stat_ok;
2517 error:
2518 isl_vec_free(ineq);
2519 return isl_stat_error;
2522 /* Add constraints that bound the values of the variable and parameter
2523 * coefficients of the schedule.
2525 * The maximal value of the coefficients is defined by the option
2526 * 'schedule_max_coefficient'.
2528 static isl_stat add_bound_coefficient_constraints(isl_ctx *ctx,
2529 struct isl_sched_graph *graph)
2531 int i;
2532 int max;
2534 max = isl_options_get_schedule_max_coefficient(ctx);
2536 if (max == -1)
2537 return isl_stat_ok;
2539 for (i = 0; i < graph->n; ++i) {
2540 struct isl_sched_node *node = &graph->node[i];
2542 if (node_add_coefficient_constraints(ctx, graph, node, max) < 0)
2543 return isl_stat_error;
2546 return isl_stat_ok;
2549 /* Add a constraint to graph->lp that equates the value at position
2550 * "sum_pos" to the sum of the "n" values starting at "first".
2552 static isl_stat add_sum_constraint(struct isl_sched_graph *graph,
2553 int sum_pos, int first, int n)
2555 int i, k;
2556 int total;
2558 total = isl_basic_set_dim(graph->lp, isl_dim_set);
2560 k = isl_basic_set_alloc_equality(graph->lp);
2561 if (k < 0)
2562 return isl_stat_error;
2563 isl_seq_clr(graph->lp->eq[k], 1 + total);
2564 isl_int_set_si(graph->lp->eq[k][1 + sum_pos], -1);
2565 for (i = 0; i < n; ++i)
2566 isl_int_set_si(graph->lp->eq[k][1 + first + i], 1);
2568 return isl_stat_ok;
2571 /* Add a constraint to graph->lp that equates the value at position
2572 * "sum_pos" to the sum of the parameter coefficients of all nodes.
2574 * Within each node, the coefficients have the following order:
2575 * - c_i_0
2576 * - c_i_n (if parametric)
2577 * - positive and negative parts of c_i_x
2579 static isl_stat add_param_sum_constraint(struct isl_sched_graph *graph,
2580 int sum_pos)
2582 int i, j, k;
2583 int total;
2585 total = isl_basic_set_dim(graph->lp, isl_dim_set);
2587 k = isl_basic_set_alloc_equality(graph->lp);
2588 if (k < 0)
2589 return isl_stat_error;
2590 isl_seq_clr(graph->lp->eq[k], 1 + total);
2591 isl_int_set_si(graph->lp->eq[k][1 + sum_pos], -1);
2592 for (i = 0; i < graph->n; ++i) {
2593 int pos = 1 + graph->node[i].start + 1;
2595 for (j = 0; j < graph->node[i].nparam; ++j)
2596 isl_int_set_si(graph->lp->eq[k][pos + j], 1);
2599 return isl_stat_ok;
2602 /* Add a constraint to graph->lp that equates the value at position
2603 * "sum_pos" to the sum of the variable coefficients of all nodes.
2605 * Within each node, the coefficients have the following order:
2606 * - c_i_0
2607 * - c_i_n (if parametric)
2608 * - positive and negative parts of c_i_x
2610 static isl_stat add_var_sum_constraint(struct isl_sched_graph *graph,
2611 int sum_pos)
2613 int i, j, k;
2614 int total;
2616 total = isl_basic_set_dim(graph->lp, isl_dim_set);
2618 k = isl_basic_set_alloc_equality(graph->lp);
2619 if (k < 0)
2620 return isl_stat_error;
2621 isl_seq_clr(graph->lp->eq[k], 1 + total);
2622 isl_int_set_si(graph->lp->eq[k][1 + sum_pos], -1);
2623 for (i = 0; i < graph->n; ++i) {
2624 struct isl_sched_node *node = &graph->node[i];
2625 int pos = 1 + node_var_coef_offset(node);
2627 for (j = 0; j < 2 * node->nvar; ++j)
2628 isl_int_set_si(graph->lp->eq[k][pos + j], 1);
2631 return isl_stat_ok;
2634 /* Construct an ILP problem for finding schedule coefficients
2635 * that result in non-negative, but small dependence distances
2636 * over all dependences.
2637 * In particular, the dependence distances over proximity edges
2638 * are bounded by m_0 + m_n n and we compute schedule coefficients
2639 * with small values (preferably zero) of m_n and m_0.
2641 * All variables of the ILP are non-negative. The actual coefficients
2642 * may be negative, so each coefficient is represented as the difference
2643 * of two non-negative variables. The negative part always appears
2644 * immediately before the positive part.
2645 * Other than that, the variables have the following order
2647 * - sum of positive and negative parts of m_n coefficients
2648 * - m_0
2649 * - sum of all c_n coefficients
2650 * (unconstrained when computing non-parametric schedules)
2651 * - sum of positive and negative parts of all c_x coefficients
2652 * - positive and negative parts of m_n coefficients
2653 * - for each node
2654 * - c_i_0
2655 * - c_i_n (if parametric)
2656 * - positive and negative parts of c_i_x
2658 * The c_i_x are not represented directly, but through the columns of
2659 * node->cmap. That is, the computed values are for variable t_i_x
2660 * such that c_i_x = Q t_i_x with Q equal to node->cmap.
2662 * The constraints are those from the edges plus two or three equalities
2663 * to express the sums.
2665 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
2666 * Otherwise, we ignore them.
2668 static isl_stat setup_lp(isl_ctx *ctx, struct isl_sched_graph *graph,
2669 int use_coincidence)
2671 int i;
2672 unsigned nparam;
2673 unsigned total;
2674 isl_space *space;
2675 int parametric;
2676 int param_pos;
2677 int n_eq, n_ineq;
2679 parametric = ctx->opt->schedule_parametric;
2680 nparam = isl_space_dim(graph->node[0].space, isl_dim_param);
2681 param_pos = 4;
2682 total = param_pos + 2 * nparam;
2683 for (i = 0; i < graph->n; ++i) {
2684 struct isl_sched_node *node = &graph->node[graph->sorted[i]];
2685 if (node_update_cmap(node) < 0)
2686 return isl_stat_error;
2687 node->start = total;
2688 total += 1 + node->nparam + 2 * node->nvar;
2691 if (count_constraints(graph, &n_eq, &n_ineq, use_coincidence) < 0)
2692 return isl_stat_error;
2693 if (count_bound_constant_constraints(ctx, graph, &n_eq, &n_ineq) < 0)
2694 return isl_stat_error;
2695 if (count_bound_coefficient_constraints(ctx, graph, &n_eq, &n_ineq) < 0)
2696 return isl_stat_error;
2698 space = isl_space_set_alloc(ctx, 0, total);
2699 isl_basic_set_free(graph->lp);
2700 n_eq += 2 + parametric;
2702 graph->lp = isl_basic_set_alloc_space(space, 0, n_eq, n_ineq);
2704 if (add_sum_constraint(graph, 0, param_pos, 2 * nparam) < 0)
2705 return isl_stat_error;
2706 if (parametric && add_param_sum_constraint(graph, 2) < 0)
2707 return isl_stat_error;
2708 if (add_var_sum_constraint(graph, 3) < 0)
2709 return isl_stat_error;
2710 if (add_bound_constant_constraints(ctx, graph) < 0)
2711 return isl_stat_error;
2712 if (add_bound_coefficient_constraints(ctx, graph) < 0)
2713 return isl_stat_error;
2714 if (add_all_validity_constraints(graph, use_coincidence) < 0)
2715 return isl_stat_error;
2716 if (add_all_proximity_constraints(graph, use_coincidence) < 0)
2717 return isl_stat_error;
2719 return isl_stat_ok;
2722 /* Analyze the conflicting constraint found by
2723 * isl_tab_basic_set_non_trivial_lexmin. If it corresponds to the validity
2724 * constraint of one of the edges between distinct nodes, living, moreover
2725 * in distinct SCCs, then record the source and sink SCC as this may
2726 * be a good place to cut between SCCs.
2728 static int check_conflict(int con, void *user)
2730 int i;
2731 struct isl_sched_graph *graph = user;
2733 if (graph->src_scc >= 0)
2734 return 0;
2736 con -= graph->lp->n_eq;
2738 if (con >= graph->lp->n_ineq)
2739 return 0;
2741 for (i = 0; i < graph->n_edge; ++i) {
2742 if (!is_validity(&graph->edge[i]))
2743 continue;
2744 if (graph->edge[i].src == graph->edge[i].dst)
2745 continue;
2746 if (graph->edge[i].src->scc == graph->edge[i].dst->scc)
2747 continue;
2748 if (graph->edge[i].start > con)
2749 continue;
2750 if (graph->edge[i].end <= con)
2751 continue;
2752 graph->src_scc = graph->edge[i].src->scc;
2753 graph->dst_scc = graph->edge[i].dst->scc;
2756 return 0;
2759 /* Check whether the next schedule row of the given node needs to be
2760 * non-trivial. Lower-dimensional domains may have some trivial rows,
2761 * but as soon as the number of remaining required non-trivial rows
2762 * is as large as the number or remaining rows to be computed,
2763 * all remaining rows need to be non-trivial.
2765 static int needs_row(struct isl_sched_graph *graph, struct isl_sched_node *node)
2767 return node->nvar - node->rank >= graph->maxvar - graph->n_row;
2770 /* Solve the ILP problem constructed in setup_lp.
2771 * For each node such that all the remaining rows of its schedule
2772 * need to be non-trivial, we construct a non-triviality region.
2773 * This region imposes that the next row is independent of previous rows.
2774 * In particular the coefficients c_i_x are represented by t_i_x
2775 * variables with c_i_x = Q t_i_x and Q a unimodular matrix such that
2776 * its first columns span the rows of the previously computed part
2777 * of the schedule. The non-triviality region enforces that at least
2778 * one of the remaining components of t_i_x is non-zero, i.e.,
2779 * that the new schedule row depends on at least one of the remaining
2780 * columns of Q.
2782 static __isl_give isl_vec *solve_lp(struct isl_sched_graph *graph)
2784 int i;
2785 isl_vec *sol;
2786 isl_basic_set *lp;
2788 for (i = 0; i < graph->n; ++i) {
2789 struct isl_sched_node *node = &graph->node[i];
2790 int skip = node->rank;
2791 graph->region[i].pos = node_var_coef_offset(node) + 2 * skip;
2792 if (needs_row(graph, node))
2793 graph->region[i].len = 2 * (node->nvar - skip);
2794 else
2795 graph->region[i].len = 0;
2797 lp = isl_basic_set_copy(graph->lp);
2798 sol = isl_tab_basic_set_non_trivial_lexmin(lp, 2, graph->n,
2799 graph->region, &check_conflict, graph);
2800 return sol;
2803 /* Extract the coefficients for the variables of "node" from "sol".
2805 * Within each node, the coefficients have the following order:
2806 * - c_i_0
2807 * - c_i_n (if parametric)
2808 * - positive and negative parts of c_i_x
2810 * The c_i_x^- appear before their c_i_x^+ counterpart.
2812 * Return c_i_x = c_i_x^+ - c_i_x^-
2814 static __isl_give isl_vec *extract_var_coef(struct isl_sched_node *node,
2815 __isl_keep isl_vec *sol)
2817 int i;
2818 int pos;
2819 isl_vec *csol;
2821 if (!sol)
2822 return NULL;
2823 csol = isl_vec_alloc(isl_vec_get_ctx(sol), node->nvar);
2824 if (!csol)
2825 return NULL;
2827 pos = 1 + node_var_coef_offset(node);
2828 for (i = 0; i < node->nvar; ++i)
2829 isl_int_sub(csol->el[i],
2830 sol->el[pos + 2 * i + 1], sol->el[pos + 2 * i]);
2832 return csol;
2835 /* Update the schedules of all nodes based on the given solution
2836 * of the LP problem.
2837 * The new row is added to the current band.
2838 * All possibly negative coefficients are encoded as a difference
2839 * of two non-negative variables, so we need to perform the subtraction
2840 * here. Moreover, if use_cmap is set, then the solution does
2841 * not refer to the actual coefficients c_i_x, but instead to variables
2842 * t_i_x such that c_i_x = Q t_i_x and Q is equal to node->cmap.
2843 * In this case, we then also need to perform this multiplication
2844 * to obtain the values of c_i_x.
2846 * If coincident is set, then the caller guarantees that the new
2847 * row satisfies the coincidence constraints.
2849 static int update_schedule(struct isl_sched_graph *graph,
2850 __isl_take isl_vec *sol, int use_cmap, int coincident)
2852 int i, j;
2853 isl_vec *csol = NULL;
2855 if (!sol)
2856 goto error;
2857 if (sol->size == 0)
2858 isl_die(sol->ctx, isl_error_internal,
2859 "no solution found", goto error);
2860 if (graph->n_total_row >= graph->max_row)
2861 isl_die(sol->ctx, isl_error_internal,
2862 "too many schedule rows", goto error);
2864 for (i = 0; i < graph->n; ++i) {
2865 struct isl_sched_node *node = &graph->node[i];
2866 int pos = node->start;
2867 int row = isl_mat_rows(node->sched);
2869 isl_vec_free(csol);
2870 csol = extract_var_coef(node, sol);
2871 if (!csol)
2872 goto error;
2874 isl_map_free(node->sched_map);
2875 node->sched_map = NULL;
2876 node->sched = isl_mat_add_rows(node->sched, 1);
2877 if (!node->sched)
2878 goto error;
2879 for (j = 0; j < 1 + node->nparam; ++j)
2880 node->sched = isl_mat_set_element(node->sched,
2881 row, j, sol->el[1 + pos + j]);
2882 if (use_cmap)
2883 csol = isl_mat_vec_product(isl_mat_copy(node->cmap),
2884 csol);
2885 if (!csol)
2886 goto error;
2887 for (j = 0; j < node->nvar; ++j)
2888 node->sched = isl_mat_set_element(node->sched,
2889 row, 1 + node->nparam + j, csol->el[j]);
2890 node->coincident[graph->n_total_row] = coincident;
2892 isl_vec_free(sol);
2893 isl_vec_free(csol);
2895 graph->n_row++;
2896 graph->n_total_row++;
2898 return 0;
2899 error:
2900 isl_vec_free(sol);
2901 isl_vec_free(csol);
2902 return -1;
2905 /* Convert row "row" of node->sched into an isl_aff living in "ls"
2906 * and return this isl_aff.
2908 static __isl_give isl_aff *extract_schedule_row(__isl_take isl_local_space *ls,
2909 struct isl_sched_node *node, int row)
2911 int j;
2912 isl_int v;
2913 isl_aff *aff;
2915 isl_int_init(v);
2917 aff = isl_aff_zero_on_domain(ls);
2918 isl_mat_get_element(node->sched, row, 0, &v);
2919 aff = isl_aff_set_constant(aff, v);
2920 for (j = 0; j < node->nparam; ++j) {
2921 isl_mat_get_element(node->sched, row, 1 + j, &v);
2922 aff = isl_aff_set_coefficient(aff, isl_dim_param, j, v);
2924 for (j = 0; j < node->nvar; ++j) {
2925 isl_mat_get_element(node->sched, row, 1 + node->nparam + j, &v);
2926 aff = isl_aff_set_coefficient(aff, isl_dim_in, j, v);
2929 isl_int_clear(v);
2931 return aff;
2934 /* Convert the "n" rows starting at "first" of node->sched into a multi_aff
2935 * and return this multi_aff.
2937 * The result is defined over the uncompressed node domain.
2939 static __isl_give isl_multi_aff *node_extract_partial_schedule_multi_aff(
2940 struct isl_sched_node *node, int first, int n)
2942 int i;
2943 isl_space *space;
2944 isl_local_space *ls;
2945 isl_aff *aff;
2946 isl_multi_aff *ma;
2947 int nrow;
2949 if (!node)
2950 return NULL;
2951 nrow = isl_mat_rows(node->sched);
2952 if (node->compressed)
2953 space = isl_multi_aff_get_domain_space(node->decompress);
2954 else
2955 space = isl_space_copy(node->space);
2956 ls = isl_local_space_from_space(isl_space_copy(space));
2957 space = isl_space_from_domain(space);
2958 space = isl_space_add_dims(space, isl_dim_out, n);
2959 ma = isl_multi_aff_zero(space);
2961 for (i = first; i < first + n; ++i) {
2962 aff = extract_schedule_row(isl_local_space_copy(ls), node, i);
2963 ma = isl_multi_aff_set_aff(ma, i - first, aff);
2966 isl_local_space_free(ls);
2968 if (node->compressed)
2969 ma = isl_multi_aff_pullback_multi_aff(ma,
2970 isl_multi_aff_copy(node->compress));
2972 return ma;
2975 /* Convert node->sched into a multi_aff and return this multi_aff.
2977 * The result is defined over the uncompressed node domain.
2979 static __isl_give isl_multi_aff *node_extract_schedule_multi_aff(
2980 struct isl_sched_node *node)
2982 int nrow;
2984 nrow = isl_mat_rows(node->sched);
2985 return node_extract_partial_schedule_multi_aff(node, 0, nrow);
2988 /* Convert node->sched into a map and return this map.
2990 * The result is cached in node->sched_map, which needs to be released
2991 * whenever node->sched is updated.
2992 * It is defined over the uncompressed node domain.
2994 static __isl_give isl_map *node_extract_schedule(struct isl_sched_node *node)
2996 if (!node->sched_map) {
2997 isl_multi_aff *ma;
2999 ma = node_extract_schedule_multi_aff(node);
3000 node->sched_map = isl_map_from_multi_aff(ma);
3003 return isl_map_copy(node->sched_map);
3006 /* Construct a map that can be used to update a dependence relation
3007 * based on the current schedule.
3008 * That is, construct a map expressing that source and sink
3009 * are executed within the same iteration of the current schedule.
3010 * This map can then be intersected with the dependence relation.
3011 * This is not the most efficient way, but this shouldn't be a critical
3012 * operation.
3014 static __isl_give isl_map *specializer(struct isl_sched_node *src,
3015 struct isl_sched_node *dst)
3017 isl_map *src_sched, *dst_sched;
3019 src_sched = node_extract_schedule(src);
3020 dst_sched = node_extract_schedule(dst);
3021 return isl_map_apply_range(src_sched, isl_map_reverse(dst_sched));
3024 /* Intersect the domains of the nested relations in domain and range
3025 * of "umap" with "map".
3027 static __isl_give isl_union_map *intersect_domains(
3028 __isl_take isl_union_map *umap, __isl_keep isl_map *map)
3030 isl_union_set *uset;
3032 umap = isl_union_map_zip(umap);
3033 uset = isl_union_set_from_set(isl_map_wrap(isl_map_copy(map)));
3034 umap = isl_union_map_intersect_domain(umap, uset);
3035 umap = isl_union_map_zip(umap);
3036 return umap;
3039 /* Update the dependence relation of the given edge based
3040 * on the current schedule.
3041 * If the dependence is carried completely by the current schedule, then
3042 * it is removed from the edge_tables. It is kept in the list of edges
3043 * as otherwise all edge_tables would have to be recomputed.
3045 static int update_edge(struct isl_sched_graph *graph,
3046 struct isl_sched_edge *edge)
3048 int empty;
3049 isl_map *id;
3051 id = specializer(edge->src, edge->dst);
3052 edge->map = isl_map_intersect(edge->map, isl_map_copy(id));
3053 if (!edge->map)
3054 goto error;
3056 if (edge->tagged_condition) {
3057 edge->tagged_condition =
3058 intersect_domains(edge->tagged_condition, id);
3059 if (!edge->tagged_condition)
3060 goto error;
3062 if (edge->tagged_validity) {
3063 edge->tagged_validity =
3064 intersect_domains(edge->tagged_validity, id);
3065 if (!edge->tagged_validity)
3066 goto error;
3069 empty = isl_map_plain_is_empty(edge->map);
3070 if (empty < 0)
3071 goto error;
3072 if (empty)
3073 graph_remove_edge(graph, edge);
3075 isl_map_free(id);
3076 return 0;
3077 error:
3078 isl_map_free(id);
3079 return -1;
3082 /* Does the domain of "umap" intersect "uset"?
3084 static int domain_intersects(__isl_keep isl_union_map *umap,
3085 __isl_keep isl_union_set *uset)
3087 int empty;
3089 umap = isl_union_map_copy(umap);
3090 umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(uset));
3091 empty = isl_union_map_is_empty(umap);
3092 isl_union_map_free(umap);
3094 return empty < 0 ? -1 : !empty;
3097 /* Does the range of "umap" intersect "uset"?
3099 static int range_intersects(__isl_keep isl_union_map *umap,
3100 __isl_keep isl_union_set *uset)
3102 int empty;
3104 umap = isl_union_map_copy(umap);
3105 umap = isl_union_map_intersect_range(umap, isl_union_set_copy(uset));
3106 empty = isl_union_map_is_empty(umap);
3107 isl_union_map_free(umap);
3109 return empty < 0 ? -1 : !empty;
3112 /* Are the condition dependences of "edge" local with respect to
3113 * the current schedule?
3115 * That is, are domain and range of the condition dependences mapped
3116 * to the same point?
3118 * In other words, is the condition false?
3120 static int is_condition_false(struct isl_sched_edge *edge)
3122 isl_union_map *umap;
3123 isl_map *map, *sched, *test;
3124 int empty, local;
3126 empty = isl_union_map_is_empty(edge->tagged_condition);
3127 if (empty < 0 || empty)
3128 return empty;
3130 umap = isl_union_map_copy(edge->tagged_condition);
3131 umap = isl_union_map_zip(umap);
3132 umap = isl_union_set_unwrap(isl_union_map_domain(umap));
3133 map = isl_map_from_union_map(umap);
3135 sched = node_extract_schedule(edge->src);
3136 map = isl_map_apply_domain(map, sched);
3137 sched = node_extract_schedule(edge->dst);
3138 map = isl_map_apply_range(map, sched);
3140 test = isl_map_identity(isl_map_get_space(map));
3141 local = isl_map_is_subset(map, test);
3142 isl_map_free(map);
3143 isl_map_free(test);
3145 return local;
3148 /* For each conditional validity constraint that is adjacent
3149 * to a condition with domain in condition_source or range in condition_sink,
3150 * turn it into an unconditional validity constraint.
3152 static int unconditionalize_adjacent_validity(struct isl_sched_graph *graph,
3153 __isl_take isl_union_set *condition_source,
3154 __isl_take isl_union_set *condition_sink)
3156 int i;
3158 condition_source = isl_union_set_coalesce(condition_source);
3159 condition_sink = isl_union_set_coalesce(condition_sink);
3161 for (i = 0; i < graph->n_edge; ++i) {
3162 int adjacent;
3163 isl_union_map *validity;
3165 if (!is_conditional_validity(&graph->edge[i]))
3166 continue;
3167 if (is_validity(&graph->edge[i]))
3168 continue;
3170 validity = graph->edge[i].tagged_validity;
3171 adjacent = domain_intersects(validity, condition_sink);
3172 if (adjacent >= 0 && !adjacent)
3173 adjacent = range_intersects(validity, condition_source);
3174 if (adjacent < 0)
3175 goto error;
3176 if (!adjacent)
3177 continue;
3179 set_validity(&graph->edge[i]);
3182 isl_union_set_free(condition_source);
3183 isl_union_set_free(condition_sink);
3184 return 0;
3185 error:
3186 isl_union_set_free(condition_source);
3187 isl_union_set_free(condition_sink);
3188 return -1;
3191 /* Update the dependence relations of all edges based on the current schedule
3192 * and enforce conditional validity constraints that are adjacent
3193 * to satisfied condition constraints.
3195 * First check if any of the condition constraints are satisfied
3196 * (i.e., not local to the outer schedule) and keep track of
3197 * their domain and range.
3198 * Then update all dependence relations (which removes the non-local
3199 * constraints).
3200 * Finally, if any condition constraints turned out to be satisfied,
3201 * then turn all adjacent conditional validity constraints into
3202 * unconditional validity constraints.
3204 static int update_edges(isl_ctx *ctx, struct isl_sched_graph *graph)
3206 int i;
3207 int any = 0;
3208 isl_union_set *source, *sink;
3210 source = isl_union_set_empty(isl_space_params_alloc(ctx, 0));
3211 sink = isl_union_set_empty(isl_space_params_alloc(ctx, 0));
3212 for (i = 0; i < graph->n_edge; ++i) {
3213 int local;
3214 isl_union_set *uset;
3215 isl_union_map *umap;
3217 if (!is_condition(&graph->edge[i]))
3218 continue;
3219 if (is_local(&graph->edge[i]))
3220 continue;
3221 local = is_condition_false(&graph->edge[i]);
3222 if (local < 0)
3223 goto error;
3224 if (local)
3225 continue;
3227 any = 1;
3229 umap = isl_union_map_copy(graph->edge[i].tagged_condition);
3230 uset = isl_union_map_domain(umap);
3231 source = isl_union_set_union(source, uset);
3233 umap = isl_union_map_copy(graph->edge[i].tagged_condition);
3234 uset = isl_union_map_range(umap);
3235 sink = isl_union_set_union(sink, uset);
3238 for (i = graph->n_edge - 1; i >= 0; --i) {
3239 if (update_edge(graph, &graph->edge[i]) < 0)
3240 goto error;
3243 if (any)
3244 return unconditionalize_adjacent_validity(graph, source, sink);
3246 isl_union_set_free(source);
3247 isl_union_set_free(sink);
3248 return 0;
3249 error:
3250 isl_union_set_free(source);
3251 isl_union_set_free(sink);
3252 return -1;
3255 static void next_band(struct isl_sched_graph *graph)
3257 graph->band_start = graph->n_total_row;
3260 /* Return the union of the universe domains of the nodes in "graph"
3261 * that satisfy "pred".
3263 static __isl_give isl_union_set *isl_sched_graph_domain(isl_ctx *ctx,
3264 struct isl_sched_graph *graph,
3265 int (*pred)(struct isl_sched_node *node, int data), int data)
3267 int i;
3268 isl_set *set;
3269 isl_union_set *dom;
3271 for (i = 0; i < graph->n; ++i)
3272 if (pred(&graph->node[i], data))
3273 break;
3275 if (i >= graph->n)
3276 isl_die(ctx, isl_error_internal,
3277 "empty component", return NULL);
3279 set = isl_set_universe(isl_space_copy(graph->node[i].space));
3280 dom = isl_union_set_from_set(set);
3282 for (i = i + 1; i < graph->n; ++i) {
3283 if (!pred(&graph->node[i], data))
3284 continue;
3285 set = isl_set_universe(isl_space_copy(graph->node[i].space));
3286 dom = isl_union_set_union(dom, isl_union_set_from_set(set));
3289 return dom;
3292 /* Return a list of unions of universe domains, where each element
3293 * in the list corresponds to an SCC (or WCC) indexed by node->scc.
3295 static __isl_give isl_union_set_list *extract_sccs(isl_ctx *ctx,
3296 struct isl_sched_graph *graph)
3298 int i;
3299 isl_union_set_list *filters;
3301 filters = isl_union_set_list_alloc(ctx, graph->scc);
3302 for (i = 0; i < graph->scc; ++i) {
3303 isl_union_set *dom;
3305 dom = isl_sched_graph_domain(ctx, graph, &node_scc_exactly, i);
3306 filters = isl_union_set_list_add(filters, dom);
3309 return filters;
3312 /* Return a list of two unions of universe domains, one for the SCCs up
3313 * to and including graph->src_scc and another for the other SCCs.
3315 static __isl_give isl_union_set_list *extract_split(isl_ctx *ctx,
3316 struct isl_sched_graph *graph)
3318 isl_union_set *dom;
3319 isl_union_set_list *filters;
3321 filters = isl_union_set_list_alloc(ctx, 2);
3322 dom = isl_sched_graph_domain(ctx, graph,
3323 &node_scc_at_most, graph->src_scc);
3324 filters = isl_union_set_list_add(filters, dom);
3325 dom = isl_sched_graph_domain(ctx, graph,
3326 &node_scc_at_least, graph->src_scc + 1);
3327 filters = isl_union_set_list_add(filters, dom);
3329 return filters;
3332 /* Copy nodes that satisfy node_pred from the src dependence graph
3333 * to the dst dependence graph.
3335 static int copy_nodes(struct isl_sched_graph *dst, struct isl_sched_graph *src,
3336 int (*node_pred)(struct isl_sched_node *node, int data), int data)
3338 int i;
3340 dst->n = 0;
3341 for (i = 0; i < src->n; ++i) {
3342 int j;
3344 if (!node_pred(&src->node[i], data))
3345 continue;
3347 j = dst->n;
3348 dst->node[j].space = isl_space_copy(src->node[i].space);
3349 dst->node[j].compressed = src->node[i].compressed;
3350 dst->node[j].hull = isl_set_copy(src->node[i].hull);
3351 dst->node[j].compress =
3352 isl_multi_aff_copy(src->node[i].compress);
3353 dst->node[j].decompress =
3354 isl_multi_aff_copy(src->node[i].decompress);
3355 dst->node[j].nvar = src->node[i].nvar;
3356 dst->node[j].nparam = src->node[i].nparam;
3357 dst->node[j].sched = isl_mat_copy(src->node[i].sched);
3358 dst->node[j].sched_map = isl_map_copy(src->node[i].sched_map);
3359 dst->node[j].coincident = src->node[i].coincident;
3360 dst->n++;
3362 if (!dst->node[j].space || !dst->node[j].sched)
3363 return -1;
3364 if (dst->node[j].compressed &&
3365 (!dst->node[j].hull || !dst->node[j].compress ||
3366 !dst->node[j].decompress))
3367 return -1;
3370 return 0;
3373 /* Copy non-empty edges that satisfy edge_pred from the src dependence graph
3374 * to the dst dependence graph.
3375 * If the source or destination node of the edge is not in the destination
3376 * graph, then it must be a backward proximity edge and it should simply
3377 * be ignored.
3379 static int copy_edges(isl_ctx *ctx, struct isl_sched_graph *dst,
3380 struct isl_sched_graph *src,
3381 int (*edge_pred)(struct isl_sched_edge *edge, int data), int data)
3383 int i;
3384 enum isl_edge_type t;
3386 dst->n_edge = 0;
3387 for (i = 0; i < src->n_edge; ++i) {
3388 struct isl_sched_edge *edge = &src->edge[i];
3389 isl_map *map;
3390 isl_union_map *tagged_condition;
3391 isl_union_map *tagged_validity;
3392 struct isl_sched_node *dst_src, *dst_dst;
3394 if (!edge_pred(edge, data))
3395 continue;
3397 if (isl_map_plain_is_empty(edge->map))
3398 continue;
3400 dst_src = graph_find_node(ctx, dst, edge->src->space);
3401 dst_dst = graph_find_node(ctx, dst, edge->dst->space);
3402 if (!dst_src || !dst_dst) {
3403 if (is_validity(edge) || is_conditional_validity(edge))
3404 isl_die(ctx, isl_error_internal,
3405 "backward (conditional) validity edge",
3406 return -1);
3407 continue;
3410 map = isl_map_copy(edge->map);
3411 tagged_condition = isl_union_map_copy(edge->tagged_condition);
3412 tagged_validity = isl_union_map_copy(edge->tagged_validity);
3414 dst->edge[dst->n_edge].src = dst_src;
3415 dst->edge[dst->n_edge].dst = dst_dst;
3416 dst->edge[dst->n_edge].map = map;
3417 dst->edge[dst->n_edge].tagged_condition = tagged_condition;
3418 dst->edge[dst->n_edge].tagged_validity = tagged_validity;
3419 dst->edge[dst->n_edge].types = edge->types;
3420 dst->n_edge++;
3422 if (edge->tagged_condition && !tagged_condition)
3423 return -1;
3424 if (edge->tagged_validity && !tagged_validity)
3425 return -1;
3427 for (t = isl_edge_first; t <= isl_edge_last; ++t) {
3428 if (edge !=
3429 graph_find_edge(src, t, edge->src, edge->dst))
3430 continue;
3431 if (graph_edge_table_add(ctx, dst, t,
3432 &dst->edge[dst->n_edge - 1]) < 0)
3433 return -1;
3437 return 0;
3440 /* Compute the maximal number of variables over all nodes.
3441 * This is the maximal number of linearly independent schedule
3442 * rows that we need to compute.
3443 * Just in case we end up in a part of the dependence graph
3444 * with only lower-dimensional domains, we make sure we will
3445 * compute the required amount of extra linearly independent rows.
3447 static int compute_maxvar(struct isl_sched_graph *graph)
3449 int i;
3451 graph->maxvar = 0;
3452 for (i = 0; i < graph->n; ++i) {
3453 struct isl_sched_node *node = &graph->node[i];
3454 int nvar;
3456 if (node_update_cmap(node) < 0)
3457 return -1;
3458 nvar = node->nvar + graph->n_row - node->rank;
3459 if (nvar > graph->maxvar)
3460 graph->maxvar = nvar;
3463 return 0;
3466 /* Extract the subgraph of "graph" that consists of the node satisfying
3467 * "node_pred" and the edges satisfying "edge_pred" and store
3468 * the result in "sub".
3470 static int extract_sub_graph(isl_ctx *ctx, struct isl_sched_graph *graph,
3471 int (*node_pred)(struct isl_sched_node *node, int data),
3472 int (*edge_pred)(struct isl_sched_edge *edge, int data),
3473 int data, struct isl_sched_graph *sub)
3475 int i, n = 0, n_edge = 0;
3476 int t;
3478 for (i = 0; i < graph->n; ++i)
3479 if (node_pred(&graph->node[i], data))
3480 ++n;
3481 for (i = 0; i < graph->n_edge; ++i)
3482 if (edge_pred(&graph->edge[i], data))
3483 ++n_edge;
3484 if (graph_alloc(ctx, sub, n, n_edge) < 0)
3485 return -1;
3486 if (copy_nodes(sub, graph, node_pred, data) < 0)
3487 return -1;
3488 if (graph_init_table(ctx, sub) < 0)
3489 return -1;
3490 for (t = 0; t <= isl_edge_last; ++t)
3491 sub->max_edge[t] = graph->max_edge[t];
3492 if (graph_init_edge_tables(ctx, sub) < 0)
3493 return -1;
3494 if (copy_edges(ctx, sub, graph, edge_pred, data) < 0)
3495 return -1;
3496 sub->n_row = graph->n_row;
3497 sub->max_row = graph->max_row;
3498 sub->n_total_row = graph->n_total_row;
3499 sub->band_start = graph->band_start;
3501 return 0;
3504 static __isl_give isl_schedule_node *compute_schedule(isl_schedule_node *node,
3505 struct isl_sched_graph *graph);
3506 static __isl_give isl_schedule_node *compute_schedule_wcc(
3507 isl_schedule_node *node, struct isl_sched_graph *graph);
3509 /* Compute a schedule for a subgraph of "graph". In particular, for
3510 * the graph composed of nodes that satisfy node_pred and edges that
3511 * that satisfy edge_pred.
3512 * If the subgraph is known to consist of a single component, then wcc should
3513 * be set and then we call compute_schedule_wcc on the constructed subgraph.
3514 * Otherwise, we call compute_schedule, which will check whether the subgraph
3515 * is connected.
3517 * The schedule is inserted at "node" and the updated schedule node
3518 * is returned.
3520 static __isl_give isl_schedule_node *compute_sub_schedule(
3521 __isl_take isl_schedule_node *node, isl_ctx *ctx,
3522 struct isl_sched_graph *graph,
3523 int (*node_pred)(struct isl_sched_node *node, int data),
3524 int (*edge_pred)(struct isl_sched_edge *edge, int data),
3525 int data, int wcc)
3527 struct isl_sched_graph split = { 0 };
3529 if (extract_sub_graph(ctx, graph, node_pred, edge_pred, data,
3530 &split) < 0)
3531 goto error;
3533 if (wcc)
3534 node = compute_schedule_wcc(node, &split);
3535 else
3536 node = compute_schedule(node, &split);
3538 graph_free(ctx, &split);
3539 return node;
3540 error:
3541 graph_free(ctx, &split);
3542 return isl_schedule_node_free(node);
3545 static int edge_scc_exactly(struct isl_sched_edge *edge, int scc)
3547 return edge->src->scc == scc && edge->dst->scc == scc;
3550 static int edge_dst_scc_at_most(struct isl_sched_edge *edge, int scc)
3552 return edge->dst->scc <= scc;
3555 static int edge_src_scc_at_least(struct isl_sched_edge *edge, int scc)
3557 return edge->src->scc >= scc;
3560 /* Reset the current band by dropping all its schedule rows.
3562 static int reset_band(struct isl_sched_graph *graph)
3564 int i;
3565 int drop;
3567 drop = graph->n_total_row - graph->band_start;
3568 graph->n_total_row -= drop;
3569 graph->n_row -= drop;
3571 for (i = 0; i < graph->n; ++i) {
3572 struct isl_sched_node *node = &graph->node[i];
3574 isl_map_free(node->sched_map);
3575 node->sched_map = NULL;
3577 node->sched = isl_mat_drop_rows(node->sched,
3578 graph->band_start, drop);
3580 if (!node->sched)
3581 return -1;
3584 return 0;
3587 /* Split the current graph into two parts and compute a schedule for each
3588 * part individually. In particular, one part consists of all SCCs up
3589 * to and including graph->src_scc, while the other part contains the other
3590 * SCCs. The split is enforced by a sequence node inserted at position "node"
3591 * in the schedule tree. Return the updated schedule node.
3592 * If either of these two parts consists of a sequence, then it is spliced
3593 * into the sequence containing the two parts.
3595 * The current band is reset. It would be possible to reuse
3596 * the previously computed rows as the first rows in the next
3597 * band, but recomputing them may result in better rows as we are looking
3598 * at a smaller part of the dependence graph.
3600 static __isl_give isl_schedule_node *compute_split_schedule(
3601 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph)
3603 int is_seq;
3604 isl_ctx *ctx;
3605 isl_union_set_list *filters;
3607 if (!node)
3608 return NULL;
3610 if (reset_band(graph) < 0)
3611 return isl_schedule_node_free(node);
3613 next_band(graph);
3615 ctx = isl_schedule_node_get_ctx(node);
3616 filters = extract_split(ctx, graph);
3617 node = isl_schedule_node_insert_sequence(node, filters);
3618 node = isl_schedule_node_child(node, 1);
3619 node = isl_schedule_node_child(node, 0);
3621 node = compute_sub_schedule(node, ctx, graph,
3622 &node_scc_at_least, &edge_src_scc_at_least,
3623 graph->src_scc + 1, 0);
3624 is_seq = isl_schedule_node_get_type(node) == isl_schedule_node_sequence;
3625 node = isl_schedule_node_parent(node);
3626 node = isl_schedule_node_parent(node);
3627 if (is_seq)
3628 node = isl_schedule_node_sequence_splice_child(node, 1);
3629 node = isl_schedule_node_child(node, 0);
3630 node = isl_schedule_node_child(node, 0);
3631 node = compute_sub_schedule(node, ctx, graph,
3632 &node_scc_at_most, &edge_dst_scc_at_most,
3633 graph->src_scc, 0);
3634 is_seq = isl_schedule_node_get_type(node) == isl_schedule_node_sequence;
3635 node = isl_schedule_node_parent(node);
3636 node = isl_schedule_node_parent(node);
3637 if (is_seq)
3638 node = isl_schedule_node_sequence_splice_child(node, 0);
3640 return node;
3643 /* Insert a band node at position "node" in the schedule tree corresponding
3644 * to the current band in "graph". Mark the band node permutable
3645 * if "permutable" is set.
3646 * The partial schedules and the coincidence property are extracted
3647 * from the graph nodes.
3648 * Return the updated schedule node.
3650 static __isl_give isl_schedule_node *insert_current_band(
3651 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph,
3652 int permutable)
3654 int i;
3655 int start, end, n;
3656 isl_multi_aff *ma;
3657 isl_multi_pw_aff *mpa;
3658 isl_multi_union_pw_aff *mupa;
3660 if (!node)
3661 return NULL;
3663 if (graph->n < 1)
3664 isl_die(isl_schedule_node_get_ctx(node), isl_error_internal,
3665 "graph should have at least one node",
3666 return isl_schedule_node_free(node));
3668 start = graph->band_start;
3669 end = graph->n_total_row;
3670 n = end - start;
3672 ma = node_extract_partial_schedule_multi_aff(&graph->node[0], start, n);
3673 mpa = isl_multi_pw_aff_from_multi_aff(ma);
3674 mupa = isl_multi_union_pw_aff_from_multi_pw_aff(mpa);
3676 for (i = 1; i < graph->n; ++i) {
3677 isl_multi_union_pw_aff *mupa_i;
3679 ma = node_extract_partial_schedule_multi_aff(&graph->node[i],
3680 start, n);
3681 mpa = isl_multi_pw_aff_from_multi_aff(ma);
3682 mupa_i = isl_multi_union_pw_aff_from_multi_pw_aff(mpa);
3683 mupa = isl_multi_union_pw_aff_union_add(mupa, mupa_i);
3685 node = isl_schedule_node_insert_partial_schedule(node, mupa);
3687 for (i = 0; i < n; ++i)
3688 node = isl_schedule_node_band_member_set_coincident(node, i,
3689 graph->node[0].coincident[start + i]);
3690 node = isl_schedule_node_band_set_permutable(node, permutable);
3692 return node;
3695 /* Update the dependence relations based on the current schedule,
3696 * add the current band to "node" and then continue with the computation
3697 * of the next band.
3698 * Return the updated schedule node.
3700 static __isl_give isl_schedule_node *compute_next_band(
3701 __isl_take isl_schedule_node *node,
3702 struct isl_sched_graph *graph, int permutable)
3704 isl_ctx *ctx;
3706 if (!node)
3707 return NULL;
3709 ctx = isl_schedule_node_get_ctx(node);
3710 if (update_edges(ctx, graph) < 0)
3711 return isl_schedule_node_free(node);
3712 node = insert_current_band(node, graph, permutable);
3713 next_band(graph);
3715 node = isl_schedule_node_child(node, 0);
3716 node = compute_schedule(node, graph);
3717 node = isl_schedule_node_parent(node);
3719 return node;
3722 /* Add constraints to graph->lp that force the dependence "map" (which
3723 * is part of the dependence relation of "edge")
3724 * to be respected and attempt to carry it, where the edge is one from
3725 * a node j to itself. "pos" is the sequence number of the given map.
3726 * That is, add constraints that enforce
3728 * (c_j_0 + c_j_n n + c_j_x y) - (c_j_0 + c_j_n n + c_j_x x)
3729 * = c_j_x (y - x) >= e_i
3731 * for each (x,y) in R.
3732 * We obtain general constraints on coefficients (c_0, c_n, c_x)
3733 * of valid constraints for (y - x) and then plug in (-e_i, 0, c_j_x),
3734 * with each coefficient in c_j_x represented as a pair of non-negative
3735 * coefficients.
3737 static int add_intra_constraints(struct isl_sched_graph *graph,
3738 struct isl_sched_edge *edge, __isl_take isl_map *map, int pos)
3740 int offset;
3741 isl_ctx *ctx = isl_map_get_ctx(map);
3742 isl_dim_map *dim_map;
3743 isl_basic_set *coef;
3744 struct isl_sched_node *node = edge->src;
3746 coef = intra_coefficients(graph, node, map);
3747 if (!coef)
3748 return -1;
3750 offset = coef_var_offset(coef);
3751 dim_map = intra_dim_map(ctx, graph, node, offset, 1);
3752 isl_dim_map_range(dim_map, 3 + pos, 0, 0, 0, 1, -1);
3753 graph->lp = isl_basic_set_extend_constraints(graph->lp,
3754 coef->n_eq, coef->n_ineq);
3755 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
3756 coef, dim_map);
3758 return 0;
3761 /* Add constraints to graph->lp that force the dependence "map" (which
3762 * is part of the dependence relation of "edge")
3763 * to be respected and attempt to carry it, where the edge is one from
3764 * node j to node k. "pos" is the sequence number of the given map.
3765 * That is, add constraints that enforce
3767 * (c_k_0 + c_k_n n + c_k_x y) - (c_j_0 + c_j_n n + c_j_x x) >= e_i
3769 * for each (x,y) in R.
3770 * We obtain general constraints on coefficients (c_0, c_n, c_x)
3771 * of valid constraints for R and then plug in
3772 * (-e_i + c_k_0 - c_j_0, c_k_n - c_j_n, c_k_x - c_j_x)
3773 * with each coefficient (except e_i, c_*_0 and c_*_n)
3774 * represented as a pair of non-negative coefficients.
3776 static int add_inter_constraints(struct isl_sched_graph *graph,
3777 struct isl_sched_edge *edge, __isl_take isl_map *map, int pos)
3779 int offset;
3780 isl_ctx *ctx = isl_map_get_ctx(map);
3781 isl_dim_map *dim_map;
3782 isl_basic_set *coef;
3783 struct isl_sched_node *src = edge->src;
3784 struct isl_sched_node *dst = edge->dst;
3786 coef = inter_coefficients(graph, edge, map);
3787 if (!coef)
3788 return -1;
3790 offset = coef_var_offset(coef);
3791 dim_map = inter_dim_map(ctx, graph, src, dst, offset, 1);
3792 isl_dim_map_range(dim_map, 3 + pos, 0, 0, 0, 1, -1);
3793 graph->lp = isl_basic_set_extend_constraints(graph->lp,
3794 coef->n_eq, coef->n_ineq);
3795 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
3796 coef, dim_map);
3798 return 0;
3801 /* Add constraints to graph->lp that force all (conditional) validity
3802 * dependences to be respected and attempt to carry them.
3804 static int add_all_constraints(struct isl_sched_graph *graph)
3806 int i, j;
3807 int pos;
3809 pos = 0;
3810 for (i = 0; i < graph->n_edge; ++i) {
3811 struct isl_sched_edge *edge= &graph->edge[i];
3813 if (!is_any_validity(edge))
3814 continue;
3816 for (j = 0; j < edge->map->n; ++j) {
3817 isl_basic_map *bmap;
3818 isl_map *map;
3820 bmap = isl_basic_map_copy(edge->map->p[j]);
3821 map = isl_map_from_basic_map(bmap);
3823 if (edge->src == edge->dst &&
3824 add_intra_constraints(graph, edge, map, pos) < 0)
3825 return -1;
3826 if (edge->src != edge->dst &&
3827 add_inter_constraints(graph, edge, map, pos) < 0)
3828 return -1;
3829 ++pos;
3833 return 0;
3836 /* Count the number of equality and inequality constraints
3837 * that will be added to the carry_lp problem.
3838 * We count each edge exactly once.
3840 static int count_all_constraints(struct isl_sched_graph *graph,
3841 int *n_eq, int *n_ineq)
3843 int i, j;
3845 *n_eq = *n_ineq = 0;
3846 for (i = 0; i < graph->n_edge; ++i) {
3847 struct isl_sched_edge *edge= &graph->edge[i];
3849 if (!is_any_validity(edge))
3850 continue;
3852 for (j = 0; j < edge->map->n; ++j) {
3853 isl_basic_map *bmap;
3854 isl_map *map;
3856 bmap = isl_basic_map_copy(edge->map->p[j]);
3857 map = isl_map_from_basic_map(bmap);
3859 if (count_map_constraints(graph, edge, map,
3860 n_eq, n_ineq, 1, 0) < 0)
3861 return -1;
3865 return 0;
3868 /* Construct an LP problem for finding schedule coefficients
3869 * such that the schedule carries as many dependences as possible.
3870 * In particular, for each dependence i, we bound the dependence distance
3871 * from below by e_i, with 0 <= e_i <= 1 and then maximize the sum
3872 * of all e_i's. Dependences with e_i = 0 in the solution are simply
3873 * respected, while those with e_i > 0 (in practice e_i = 1) are carried.
3874 * Note that if the dependence relation is a union of basic maps,
3875 * then we have to consider each basic map individually as it may only
3876 * be possible to carry the dependences expressed by some of those
3877 * basic maps and not all of them.
3878 * Below, we consider each of those basic maps as a separate "edge".
3880 * All variables of the LP are non-negative. The actual coefficients
3881 * may be negative, so each coefficient is represented as the difference
3882 * of two non-negative variables. The negative part always appears
3883 * immediately before the positive part.
3884 * Other than that, the variables have the following order
3886 * - sum of (1 - e_i) over all edges
3887 * - sum of all c_n coefficients
3888 * (unconstrained when computing non-parametric schedules)
3889 * - sum of positive and negative parts of all c_x coefficients
3890 * - for each edge
3891 * - e_i
3892 * - for each node
3893 * - c_i_0
3894 * - c_i_n (if parametric)
3895 * - positive and negative parts of c_i_x
3897 * The constraints are those from the (validity) edges plus three equalities
3898 * to express the sums and n_edge inequalities to express e_i <= 1.
3900 static isl_stat setup_carry_lp(isl_ctx *ctx, struct isl_sched_graph *graph)
3902 int i;
3903 int k;
3904 isl_space *dim;
3905 unsigned total;
3906 int n_eq, n_ineq;
3907 int n_edge;
3909 n_edge = 0;
3910 for (i = 0; i < graph->n_edge; ++i)
3911 n_edge += graph->edge[i].map->n;
3913 total = 3 + n_edge;
3914 for (i = 0; i < graph->n; ++i) {
3915 struct isl_sched_node *node = &graph->node[graph->sorted[i]];
3916 node->start = total;
3917 total += 1 + node->nparam + 2 * node->nvar;
3920 if (count_all_constraints(graph, &n_eq, &n_ineq) < 0)
3921 return isl_stat_error;
3923 dim = isl_space_set_alloc(ctx, 0, total);
3924 isl_basic_set_free(graph->lp);
3925 n_eq += 3;
3926 n_ineq += n_edge;
3927 graph->lp = isl_basic_set_alloc_space(dim, 0, n_eq, n_ineq);
3928 graph->lp = isl_basic_set_set_rational(graph->lp);
3930 k = isl_basic_set_alloc_equality(graph->lp);
3931 if (k < 0)
3932 return isl_stat_error;
3933 isl_seq_clr(graph->lp->eq[k], 1 + total);
3934 isl_int_set_si(graph->lp->eq[k][0], -n_edge);
3935 isl_int_set_si(graph->lp->eq[k][1], 1);
3936 for (i = 0; i < n_edge; ++i)
3937 isl_int_set_si(graph->lp->eq[k][4 + i], 1);
3939 if (add_param_sum_constraint(graph, 1) < 0)
3940 return isl_stat_error;
3941 if (add_var_sum_constraint(graph, 2) < 0)
3942 return isl_stat_error;
3944 for (i = 0; i < n_edge; ++i) {
3945 k = isl_basic_set_alloc_inequality(graph->lp);
3946 if (k < 0)
3947 return isl_stat_error;
3948 isl_seq_clr(graph->lp->ineq[k], 1 + total);
3949 isl_int_set_si(graph->lp->ineq[k][4 + i], -1);
3950 isl_int_set_si(graph->lp->ineq[k][0], 1);
3953 if (add_all_constraints(graph) < 0)
3954 return isl_stat_error;
3956 return isl_stat_ok;
3959 static __isl_give isl_schedule_node *compute_component_schedule(
3960 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph,
3961 int wcc);
3963 /* Comparison function for sorting the statements based on
3964 * the corresponding value in "r".
3966 static int smaller_value(const void *a, const void *b, void *data)
3968 isl_vec *r = data;
3969 const int *i1 = a;
3970 const int *i2 = b;
3972 return isl_int_cmp(r->el[*i1], r->el[*i2]);
3975 /* If the schedule_split_scaled option is set and if the linear
3976 * parts of the scheduling rows for all nodes in the graphs have
3977 * a non-trivial common divisor, then split off the remainder of the
3978 * constant term modulo this common divisor from the linear part.
3979 * Otherwise, insert a band node directly and continue with
3980 * the construction of the schedule.
3982 * If a non-trivial common divisor is found, then
3983 * the linear part is reduced and the remainder is enforced
3984 * by a sequence node with the children placed in the order
3985 * of this remainder.
3986 * In particular, we assign an scc index based on the remainder and
3987 * then rely on compute_component_schedule to insert the sequence and
3988 * to continue the schedule construction on each part.
3990 static __isl_give isl_schedule_node *split_scaled(
3991 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph)
3993 int i;
3994 int row;
3995 int scc;
3996 isl_ctx *ctx;
3997 isl_int gcd, gcd_i;
3998 isl_vec *r;
3999 int *order;
4001 if (!node)
4002 return NULL;
4004 ctx = isl_schedule_node_get_ctx(node);
4005 if (!ctx->opt->schedule_split_scaled)
4006 return compute_next_band(node, graph, 0);
4007 if (graph->n <= 1)
4008 return compute_next_band(node, graph, 0);
4010 isl_int_init(gcd);
4011 isl_int_init(gcd_i);
4013 isl_int_set_si(gcd, 0);
4015 row = isl_mat_rows(graph->node[0].sched) - 1;
4017 for (i = 0; i < graph->n; ++i) {
4018 struct isl_sched_node *node = &graph->node[i];
4019 int cols = isl_mat_cols(node->sched);
4021 isl_seq_gcd(node->sched->row[row] + 1, cols - 1, &gcd_i);
4022 isl_int_gcd(gcd, gcd, gcd_i);
4025 isl_int_clear(gcd_i);
4027 if (isl_int_cmp_si(gcd, 1) <= 0) {
4028 isl_int_clear(gcd);
4029 return compute_next_band(node, graph, 0);
4032 r = isl_vec_alloc(ctx, graph->n);
4033 order = isl_calloc_array(ctx, int, graph->n);
4034 if (!r || !order)
4035 goto error;
4037 for (i = 0; i < graph->n; ++i) {
4038 struct isl_sched_node *node = &graph->node[i];
4040 order[i] = i;
4041 isl_int_fdiv_r(r->el[i], node->sched->row[row][0], gcd);
4042 isl_int_fdiv_q(node->sched->row[row][0],
4043 node->sched->row[row][0], gcd);
4044 isl_int_mul(node->sched->row[row][0],
4045 node->sched->row[row][0], gcd);
4046 node->sched = isl_mat_scale_down_row(node->sched, row, gcd);
4047 if (!node->sched)
4048 goto error;
4051 if (isl_sort(order, graph->n, sizeof(order[0]), &smaller_value, r) < 0)
4052 goto error;
4054 scc = 0;
4055 for (i = 0; i < graph->n; ++i) {
4056 if (i > 0 && isl_int_ne(r->el[order[i - 1]], r->el[order[i]]))
4057 ++scc;
4058 graph->node[order[i]].scc = scc;
4060 graph->scc = ++scc;
4061 graph->weak = 0;
4063 isl_int_clear(gcd);
4064 isl_vec_free(r);
4065 free(order);
4067 if (update_edges(ctx, graph) < 0)
4068 return isl_schedule_node_free(node);
4069 node = insert_current_band(node, graph, 0);
4070 next_band(graph);
4072 node = isl_schedule_node_child(node, 0);
4073 node = compute_component_schedule(node, graph, 0);
4074 node = isl_schedule_node_parent(node);
4076 return node;
4077 error:
4078 isl_vec_free(r);
4079 free(order);
4080 isl_int_clear(gcd);
4081 return isl_schedule_node_free(node);
4084 /* Is the schedule row "sol" trivial on node "node"?
4085 * That is, is the solution zero on the dimensions orthogonal to
4086 * the previously found solutions?
4087 * Return 1 if the solution is trivial, 0 if it is not and -1 on error.
4089 * Each coefficient is represented as the difference between
4090 * two non-negative values in "sol". "sol" has been computed
4091 * in terms of the original iterators (i.e., without use of cmap).
4092 * We construct the schedule row s and write it as a linear
4093 * combination of (linear combinations of) previously computed schedule rows.
4094 * s = Q c or c = U s.
4095 * If the final entries of c are all zero, then the solution is trivial.
4097 static int is_trivial(struct isl_sched_node *node, __isl_keep isl_vec *sol)
4099 int trivial;
4100 isl_vec *node_sol;
4102 if (!sol)
4103 return -1;
4104 if (node->nvar == node->rank)
4105 return 0;
4107 node_sol = extract_var_coef(node, sol);
4108 node_sol = isl_mat_vec_product(isl_mat_copy(node->cinv), node_sol);
4109 if (!node_sol)
4110 return -1;
4112 trivial = isl_seq_first_non_zero(node_sol->el + node->rank,
4113 node->nvar - node->rank) == -1;
4115 isl_vec_free(node_sol);
4117 return trivial;
4120 /* Is the schedule row "sol" trivial on any node where it should
4121 * not be trivial?
4122 * "sol" has been computed in terms of the original iterators
4123 * (i.e., without use of cmap).
4124 * Return 1 if any solution is trivial, 0 if they are not and -1 on error.
4126 static int is_any_trivial(struct isl_sched_graph *graph,
4127 __isl_keep isl_vec *sol)
4129 int i;
4131 for (i = 0; i < graph->n; ++i) {
4132 struct isl_sched_node *node = &graph->node[i];
4133 int trivial;
4135 if (!needs_row(graph, node))
4136 continue;
4137 trivial = is_trivial(node, sol);
4138 if (trivial < 0 || trivial)
4139 return trivial;
4142 return 0;
4145 /* Construct a schedule row for each node such that as many dependences
4146 * as possible are carried and then continue with the next band.
4148 * Note that despite the fact that the problem is solved using a rational
4149 * solver, the solution is guaranteed to be integral.
4150 * Specifically, the dependence distance lower bounds e_i (and therefore
4151 * also their sum) are integers. See Lemma 5 of [1].
4153 * If the computed schedule row turns out to be trivial on one or
4154 * more nodes where it should not be trivial, then we throw it away
4155 * and try again on each component separately.
4157 * If there is only one component, then we accept the schedule row anyway,
4158 * but we do not consider it as a complete row and therefore do not
4159 * increment graph->n_row. Note that the ranks of the nodes that
4160 * do get a non-trivial schedule part will get updated regardless and
4161 * graph->maxvar is computed based on these ranks. The test for
4162 * whether more schedule rows are required in compute_schedule_wcc
4163 * is therefore not affected.
4165 * Insert a band corresponding to the schedule row at position "node"
4166 * of the schedule tree and continue with the construction of the schedule.
4167 * This insertion and the continued construction is performed by split_scaled
4168 * after optionally checking for non-trivial common divisors.
4170 * [1] P. Feautrier, Some Efficient Solutions to the Affine Scheduling
4171 * Problem, Part II: Multi-Dimensional Time.
4172 * In Intl. Journal of Parallel Programming, 1992.
4174 static __isl_give isl_schedule_node *carry_dependences(
4175 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph)
4177 int i;
4178 int n_edge;
4179 int trivial;
4180 isl_ctx *ctx;
4181 isl_vec *sol;
4182 isl_basic_set *lp;
4184 if (!node)
4185 return NULL;
4187 n_edge = 0;
4188 for (i = 0; i < graph->n_edge; ++i)
4189 n_edge += graph->edge[i].map->n;
4191 ctx = isl_schedule_node_get_ctx(node);
4192 if (setup_carry_lp(ctx, graph) < 0)
4193 return isl_schedule_node_free(node);
4195 lp = isl_basic_set_copy(graph->lp);
4196 sol = isl_tab_basic_set_non_neg_lexmin(lp);
4197 if (!sol)
4198 return isl_schedule_node_free(node);
4200 if (sol->size == 0) {
4201 isl_vec_free(sol);
4202 isl_die(ctx, isl_error_internal,
4203 "error in schedule construction",
4204 return isl_schedule_node_free(node));
4207 isl_int_divexact(sol->el[1], sol->el[1], sol->el[0]);
4208 if (isl_int_cmp_si(sol->el[1], n_edge) >= 0) {
4209 isl_vec_free(sol);
4210 isl_die(ctx, isl_error_unknown,
4211 "unable to carry dependences",
4212 return isl_schedule_node_free(node));
4215 trivial = is_any_trivial(graph, sol);
4216 if (trivial < 0) {
4217 sol = isl_vec_free(sol);
4218 } else if (trivial && graph->scc > 1) {
4219 isl_vec_free(sol);
4220 return compute_component_schedule(node, graph, 1);
4223 if (update_schedule(graph, sol, 0, 0) < 0)
4224 return isl_schedule_node_free(node);
4225 if (trivial)
4226 graph->n_row--;
4228 return split_scaled(node, graph);
4231 /* Topologically sort statements mapped to the same schedule iteration
4232 * and add insert a sequence node in front of "node"
4233 * corresponding to this order.
4234 * If "initialized" is set, then it may be assumed that compute_maxvar
4235 * has been called on the current band. Otherwise, call
4236 * compute_maxvar if and before carry_dependences gets called.
4238 * If it turns out to be impossible to sort the statements apart,
4239 * because different dependences impose different orderings
4240 * on the statements, then we extend the schedule such that
4241 * it carries at least one more dependence.
4243 static __isl_give isl_schedule_node *sort_statements(
4244 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph,
4245 int initialized)
4247 isl_ctx *ctx;
4248 isl_union_set_list *filters;
4250 if (!node)
4251 return NULL;
4253 ctx = isl_schedule_node_get_ctx(node);
4254 if (graph->n < 1)
4255 isl_die(ctx, isl_error_internal,
4256 "graph should have at least one node",
4257 return isl_schedule_node_free(node));
4259 if (graph->n == 1)
4260 return node;
4262 if (update_edges(ctx, graph) < 0)
4263 return isl_schedule_node_free(node);
4265 if (graph->n_edge == 0)
4266 return node;
4268 if (detect_sccs(ctx, graph) < 0)
4269 return isl_schedule_node_free(node);
4271 next_band(graph);
4272 if (graph->scc < graph->n) {
4273 if (!initialized && compute_maxvar(graph) < 0)
4274 return isl_schedule_node_free(node);
4275 return carry_dependences(node, graph);
4278 filters = extract_sccs(ctx, graph);
4279 node = isl_schedule_node_insert_sequence(node, filters);
4281 return node;
4284 /* Are there any (non-empty) (conditional) validity edges in the graph?
4286 static int has_validity_edges(struct isl_sched_graph *graph)
4288 int i;
4290 for (i = 0; i < graph->n_edge; ++i) {
4291 int empty;
4293 empty = isl_map_plain_is_empty(graph->edge[i].map);
4294 if (empty < 0)
4295 return -1;
4296 if (empty)
4297 continue;
4298 if (is_any_validity(&graph->edge[i]))
4299 return 1;
4302 return 0;
4305 /* Should we apply a Feautrier step?
4306 * That is, did the user request the Feautrier algorithm and are
4307 * there any validity dependences (left)?
4309 static int need_feautrier_step(isl_ctx *ctx, struct isl_sched_graph *graph)
4311 if (ctx->opt->schedule_algorithm != ISL_SCHEDULE_ALGORITHM_FEAUTRIER)
4312 return 0;
4314 return has_validity_edges(graph);
4317 /* Compute a schedule for a connected dependence graph using Feautrier's
4318 * multi-dimensional scheduling algorithm and return the updated schedule node.
4320 * The original algorithm is described in [1].
4321 * The main idea is to minimize the number of scheduling dimensions, by
4322 * trying to satisfy as many dependences as possible per scheduling dimension.
4324 * [1] P. Feautrier, Some Efficient Solutions to the Affine Scheduling
4325 * Problem, Part II: Multi-Dimensional Time.
4326 * In Intl. Journal of Parallel Programming, 1992.
4328 static __isl_give isl_schedule_node *compute_schedule_wcc_feautrier(
4329 isl_schedule_node *node, struct isl_sched_graph *graph)
4331 return carry_dependences(node, graph);
4334 /* Turn off the "local" bit on all (condition) edges.
4336 static void clear_local_edges(struct isl_sched_graph *graph)
4338 int i;
4340 for (i = 0; i < graph->n_edge; ++i)
4341 if (is_condition(&graph->edge[i]))
4342 clear_local(&graph->edge[i]);
4345 /* Does "graph" have both condition and conditional validity edges?
4347 static int need_condition_check(struct isl_sched_graph *graph)
4349 int i;
4350 int any_condition = 0;
4351 int any_conditional_validity = 0;
4353 for (i = 0; i < graph->n_edge; ++i) {
4354 if (is_condition(&graph->edge[i]))
4355 any_condition = 1;
4356 if (is_conditional_validity(&graph->edge[i]))
4357 any_conditional_validity = 1;
4360 return any_condition && any_conditional_validity;
4363 /* Does "graph" contain any coincidence edge?
4365 static int has_any_coincidence(struct isl_sched_graph *graph)
4367 int i;
4369 for (i = 0; i < graph->n_edge; ++i)
4370 if (is_coincidence(&graph->edge[i]))
4371 return 1;
4373 return 0;
4376 /* Extract the final schedule row as a map with the iteration domain
4377 * of "node" as domain.
4379 static __isl_give isl_map *final_row(struct isl_sched_node *node)
4381 isl_local_space *ls;
4382 isl_aff *aff;
4383 int row;
4385 row = isl_mat_rows(node->sched) - 1;
4386 ls = isl_local_space_from_space(isl_space_copy(node->space));
4387 aff = extract_schedule_row(ls, node, row);
4388 return isl_map_from_aff(aff);
4391 /* Is the conditional validity dependence in the edge with index "edge_index"
4392 * violated by the latest (i.e., final) row of the schedule?
4393 * That is, is i scheduled after j
4394 * for any conditional validity dependence i -> j?
4396 static int is_violated(struct isl_sched_graph *graph, int edge_index)
4398 isl_map *src_sched, *dst_sched, *map;
4399 struct isl_sched_edge *edge = &graph->edge[edge_index];
4400 int empty;
4402 src_sched = final_row(edge->src);
4403 dst_sched = final_row(edge->dst);
4404 map = isl_map_copy(edge->map);
4405 map = isl_map_apply_domain(map, src_sched);
4406 map = isl_map_apply_range(map, dst_sched);
4407 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_out, 0);
4408 empty = isl_map_is_empty(map);
4409 isl_map_free(map);
4411 if (empty < 0)
4412 return -1;
4414 return !empty;
4417 /* Does "graph" have any satisfied condition edges that
4418 * are adjacent to the conditional validity constraint with
4419 * domain "conditional_source" and range "conditional_sink"?
4421 * A satisfied condition is one that is not local.
4422 * If a condition was forced to be local already (i.e., marked as local)
4423 * then there is no need to check if it is in fact local.
4425 * Additionally, mark all adjacent condition edges found as local.
4427 static int has_adjacent_true_conditions(struct isl_sched_graph *graph,
4428 __isl_keep isl_union_set *conditional_source,
4429 __isl_keep isl_union_set *conditional_sink)
4431 int i;
4432 int any = 0;
4434 for (i = 0; i < graph->n_edge; ++i) {
4435 int adjacent, local;
4436 isl_union_map *condition;
4438 if (!is_condition(&graph->edge[i]))
4439 continue;
4440 if (is_local(&graph->edge[i]))
4441 continue;
4443 condition = graph->edge[i].tagged_condition;
4444 adjacent = domain_intersects(condition, conditional_sink);
4445 if (adjacent >= 0 && !adjacent)
4446 adjacent = range_intersects(condition,
4447 conditional_source);
4448 if (adjacent < 0)
4449 return -1;
4450 if (!adjacent)
4451 continue;
4453 set_local(&graph->edge[i]);
4455 local = is_condition_false(&graph->edge[i]);
4456 if (local < 0)
4457 return -1;
4458 if (!local)
4459 any = 1;
4462 return any;
4465 /* Are there any violated conditional validity dependences with
4466 * adjacent condition dependences that are not local with respect
4467 * to the current schedule?
4468 * That is, is the conditional validity constraint violated?
4470 * Additionally, mark all those adjacent condition dependences as local.
4471 * We also mark those adjacent condition dependences that were not marked
4472 * as local before, but just happened to be local already. This ensures
4473 * that they remain local if the schedule is recomputed.
4475 * We first collect domain and range of all violated conditional validity
4476 * dependences and then check if there are any adjacent non-local
4477 * condition dependences.
4479 static int has_violated_conditional_constraint(isl_ctx *ctx,
4480 struct isl_sched_graph *graph)
4482 int i;
4483 int any = 0;
4484 isl_union_set *source, *sink;
4486 source = isl_union_set_empty(isl_space_params_alloc(ctx, 0));
4487 sink = isl_union_set_empty(isl_space_params_alloc(ctx, 0));
4488 for (i = 0; i < graph->n_edge; ++i) {
4489 isl_union_set *uset;
4490 isl_union_map *umap;
4491 int violated;
4493 if (!is_conditional_validity(&graph->edge[i]))
4494 continue;
4496 violated = is_violated(graph, i);
4497 if (violated < 0)
4498 goto error;
4499 if (!violated)
4500 continue;
4502 any = 1;
4504 umap = isl_union_map_copy(graph->edge[i].tagged_validity);
4505 uset = isl_union_map_domain(umap);
4506 source = isl_union_set_union(source, uset);
4507 source = isl_union_set_coalesce(source);
4509 umap = isl_union_map_copy(graph->edge[i].tagged_validity);
4510 uset = isl_union_map_range(umap);
4511 sink = isl_union_set_union(sink, uset);
4512 sink = isl_union_set_coalesce(sink);
4515 if (any)
4516 any = has_adjacent_true_conditions(graph, source, sink);
4518 isl_union_set_free(source);
4519 isl_union_set_free(sink);
4520 return any;
4521 error:
4522 isl_union_set_free(source);
4523 isl_union_set_free(sink);
4524 return -1;
4527 /* Examine the current band (the rows between graph->band_start and
4528 * graph->n_total_row), deciding whether to drop it or add it to "node"
4529 * and then continue with the computation of the next band, if any.
4530 * If "initialized" is set, then it may be assumed that compute_maxvar
4531 * has been called on the current band. Otherwise, call
4532 * compute_maxvar if and before carry_dependences gets called.
4534 * The caller keeps looking for a new row as long as
4535 * graph->n_row < graph->maxvar. If the latest attempt to find
4536 * such a row failed (i.e., we still have graph->n_row < graph->maxvar),
4537 * then we either
4538 * - split between SCCs and start over (assuming we found an interesting
4539 * pair of SCCs between which to split)
4540 * - continue with the next band (assuming the current band has at least
4541 * one row)
4542 * - try to carry as many dependences as possible and continue with the next
4543 * band
4544 * In each case, we first insert a band node in the schedule tree
4545 * if any rows have been computed.
4547 * If the caller managed to complete the schedule, we insert a band node
4548 * (if any schedule rows were computed) and we finish off by topologically
4549 * sorting the statements based on the remaining dependences.
4551 static __isl_give isl_schedule_node *compute_schedule_finish_band(
4552 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph,
4553 int initialized)
4555 int insert;
4557 if (!node)
4558 return NULL;
4560 if (graph->n_row < graph->maxvar) {
4561 isl_ctx *ctx;
4562 int empty = graph->n_total_row == graph->band_start;
4564 ctx = isl_schedule_node_get_ctx(node);
4565 if (!ctx->opt->schedule_maximize_band_depth && !empty)
4566 return compute_next_band(node, graph, 1);
4567 if (graph->src_scc >= 0)
4568 return compute_split_schedule(node, graph);
4569 if (!empty)
4570 return compute_next_band(node, graph, 1);
4571 if (!initialized && compute_maxvar(graph) < 0)
4572 return isl_schedule_node_free(node);
4573 return carry_dependences(node, graph);
4576 insert = graph->n_total_row > graph->band_start;
4577 if (insert) {
4578 node = insert_current_band(node, graph, 1);
4579 node = isl_schedule_node_child(node, 0);
4581 node = sort_statements(node, graph, initialized);
4582 if (insert)
4583 node = isl_schedule_node_parent(node);
4585 return node;
4588 /* Construct a band of schedule rows for a connected dependence graph.
4589 * The caller is responsible for determining the strongly connected
4590 * components and calling compute_maxvar first.
4592 * We try to find a sequence of as many schedule rows as possible that result
4593 * in non-negative dependence distances (independent of the previous rows
4594 * in the sequence, i.e., such that the sequence is tilable), with as
4595 * many of the initial rows as possible satisfying the coincidence constraints.
4596 * The computation stops if we can't find any more rows or if we have found
4597 * all the rows we wanted to find.
4599 * If ctx->opt->schedule_outer_coincidence is set, then we force the
4600 * outermost dimension to satisfy the coincidence constraints. If this
4601 * turns out to be impossible, we fall back on the general scheme above
4602 * and try to carry as many dependences as possible.
4604 * If "graph" contains both condition and conditional validity dependences,
4605 * then we need to check that that the conditional schedule constraint
4606 * is satisfied, i.e., there are no violated conditional validity dependences
4607 * that are adjacent to any non-local condition dependences.
4608 * If there are, then we mark all those adjacent condition dependences
4609 * as local and recompute the current band. Those dependences that
4610 * are marked local will then be forced to be local.
4611 * The initial computation is performed with no dependences marked as local.
4612 * If we are lucky, then there will be no violated conditional validity
4613 * dependences adjacent to any non-local condition dependences.
4614 * Otherwise, we mark some additional condition dependences as local and
4615 * recompute. We continue this process until there are no violations left or
4616 * until we are no longer able to compute a schedule.
4617 * Since there are only a finite number of dependences,
4618 * there will only be a finite number of iterations.
4620 static isl_stat compute_schedule_wcc_band(isl_ctx *ctx,
4621 struct isl_sched_graph *graph)
4623 int has_coincidence;
4624 int use_coincidence;
4625 int force_coincidence = 0;
4626 int check_conditional;
4628 if (sort_sccs(graph) < 0)
4629 return isl_stat_error;
4631 clear_local_edges(graph);
4632 check_conditional = need_condition_check(graph);
4633 has_coincidence = has_any_coincidence(graph);
4635 if (ctx->opt->schedule_outer_coincidence)
4636 force_coincidence = 1;
4638 use_coincidence = has_coincidence;
4639 while (graph->n_row < graph->maxvar) {
4640 isl_vec *sol;
4641 int violated;
4642 int coincident;
4644 graph->src_scc = -1;
4645 graph->dst_scc = -1;
4647 if (setup_lp(ctx, graph, use_coincidence) < 0)
4648 return isl_stat_error;
4649 sol = solve_lp(graph);
4650 if (!sol)
4651 return isl_stat_error;
4652 if (sol->size == 0) {
4653 int empty = graph->n_total_row == graph->band_start;
4655 isl_vec_free(sol);
4656 if (use_coincidence && (!force_coincidence || !empty)) {
4657 use_coincidence = 0;
4658 continue;
4660 return isl_stat_ok;
4662 coincident = !has_coincidence || use_coincidence;
4663 if (update_schedule(graph, sol, 1, coincident) < 0)
4664 return isl_stat_error;
4666 if (!check_conditional)
4667 continue;
4668 violated = has_violated_conditional_constraint(ctx, graph);
4669 if (violated < 0)
4670 return isl_stat_error;
4671 if (!violated)
4672 continue;
4673 if (reset_band(graph) < 0)
4674 return isl_stat_error;
4675 use_coincidence = has_coincidence;
4678 return isl_stat_ok;
4681 /* Compute a schedule for a connected dependence graph by considering
4682 * the graph as a whole and return the updated schedule node.
4684 * The actual schedule rows of the current band are computed by
4685 * compute_schedule_wcc_band. compute_schedule_finish_band takes
4686 * care of integrating the band into "node" and continuing
4687 * the computation.
4689 static __isl_give isl_schedule_node *compute_schedule_wcc_whole(
4690 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph)
4692 isl_ctx *ctx;
4694 if (!node)
4695 return NULL;
4697 ctx = isl_schedule_node_get_ctx(node);
4698 if (compute_schedule_wcc_band(ctx, graph) < 0)
4699 return isl_schedule_node_free(node);
4701 return compute_schedule_finish_band(node, graph, 1);
4704 /* Clustering information used by compute_schedule_wcc_clustering.
4706 * "n" is the number of SCCs in the original dependence graph
4707 * "scc" is an array of "n" elements, each representing an SCC
4708 * of the original dependence graph. All entries in the same cluster
4709 * have the same number of schedule rows.
4710 * "scc_cluster" maps each SCC index to the cluster to which it belongs,
4711 * where each cluster is represented by the index of the first SCC
4712 * in the cluster. Initially, each SCC belongs to a cluster containing
4713 * only that SCC.
4715 * "scc_in_merge" is used by merge_clusters_along_edge to keep
4716 * track of which SCCs need to be merged.
4718 * "cluster" contains the merged clusters of SCCs after the clustering
4719 * has completed.
4721 * "scc_node" is a temporary data structure used inside copy_partial.
4722 * For each SCC, it keeps track of the number of nodes in the SCC
4723 * that have already been copied.
4725 struct isl_clustering {
4726 int n;
4727 struct isl_sched_graph *scc;
4728 struct isl_sched_graph *cluster;
4729 int *scc_cluster;
4730 int *scc_node;
4731 int *scc_in_merge;
4734 /* Initialize the clustering data structure "c" from "graph".
4736 * In particular, allocate memory, extract the SCCs from "graph"
4737 * into c->scc, initialize scc_cluster and construct
4738 * a band of schedule rows for each SCC.
4739 * Within each SCC, there is only one SCC by definition.
4740 * Each SCC initially belongs to a cluster containing only that SCC.
4742 static isl_stat clustering_init(isl_ctx *ctx, struct isl_clustering *c,
4743 struct isl_sched_graph *graph)
4745 int i;
4747 c->n = graph->scc;
4748 c->scc = isl_calloc_array(ctx, struct isl_sched_graph, c->n);
4749 c->cluster = isl_calloc_array(ctx, struct isl_sched_graph, c->n);
4750 c->scc_cluster = isl_calloc_array(ctx, int, c->n);
4751 c->scc_node = isl_calloc_array(ctx, int, c->n);
4752 c->scc_in_merge = isl_calloc_array(ctx, int, c->n);
4753 if (!c->scc || !c->cluster ||
4754 !c->scc_cluster || !c->scc_node || !c->scc_in_merge)
4755 return isl_stat_error;
4757 for (i = 0; i < c->n; ++i) {
4758 if (extract_sub_graph(ctx, graph, &node_scc_exactly,
4759 &edge_scc_exactly, i, &c->scc[i]) < 0)
4760 return isl_stat_error;
4761 c->scc[i].scc = 1;
4762 if (compute_maxvar(&c->scc[i]) < 0)
4763 return isl_stat_error;
4764 if (compute_schedule_wcc_band(ctx, &c->scc[i]) < 0)
4765 return isl_stat_error;
4766 c->scc_cluster[i] = i;
4769 return isl_stat_ok;
4772 /* Free all memory allocated for "c".
4774 static void clustering_free(isl_ctx *ctx, struct isl_clustering *c)
4776 int i;
4778 if (c->scc)
4779 for (i = 0; i < c->n; ++i)
4780 graph_free(ctx, &c->scc[i]);
4781 free(c->scc);
4782 if (c->cluster)
4783 for (i = 0; i < c->n; ++i)
4784 graph_free(ctx, &c->cluster[i]);
4785 free(c->cluster);
4786 free(c->scc_cluster);
4787 free(c->scc_node);
4788 free(c->scc_in_merge);
4791 /* Should we refrain from merging the cluster in "graph" with
4792 * any other cluster?
4793 * In particular, is its current schedule band empty and incomplete.
4795 static int bad_cluster(struct isl_sched_graph *graph)
4797 return graph->n_row < graph->maxvar &&
4798 graph->n_total_row == graph->band_start;
4801 /* Return the index of an edge in "graph" that can be used to merge
4802 * two clusters in "c".
4803 * Return graph->n_edge if no such edge can be found.
4804 * Return -1 on error.
4806 * In particular, return a proximity edge between two clusters
4807 * that is not marked "no_merge" and such that neither of the
4808 * two clusters has an incomplete, empty band.
4810 * If there are multiple such edges, then try and find the most
4811 * appropriate edge to use for merging. In particular, pick the edge
4812 * with the greatest weight. If there are multiple of those,
4813 * then pick one with the shortest distance between
4814 * the two cluster representatives.
4816 static int find_proximity(struct isl_sched_graph *graph,
4817 struct isl_clustering *c)
4819 int i, best = graph->n_edge, best_dist, best_weight;
4821 for (i = 0; i < graph->n_edge; ++i) {
4822 struct isl_sched_edge *edge = &graph->edge[i];
4823 int dist, weight;
4825 if (!is_proximity(edge))
4826 continue;
4827 if (edge->no_merge)
4828 continue;
4829 if (bad_cluster(&c->scc[edge->src->scc]) ||
4830 bad_cluster(&c->scc[edge->dst->scc]))
4831 continue;
4832 dist = c->scc_cluster[edge->dst->scc] -
4833 c->scc_cluster[edge->src->scc];
4834 if (dist == 0)
4835 continue;
4836 weight = edge->weight;
4837 if (best < graph->n_edge) {
4838 if (best_weight > weight)
4839 continue;
4840 if (best_weight == weight && best_dist <= dist)
4841 continue;
4843 best = i;
4844 best_dist = dist;
4845 best_weight = weight;
4848 return best;
4851 /* Internal data structure used in mark_merge_sccs.
4853 * "graph" is the dependence graph in which a strongly connected
4854 * component is constructed.
4855 * "scc_cluster" maps each SCC index to the cluster to which it belongs.
4856 * "src" and "dst" are the indices of the nodes that are being merged.
4858 struct isl_mark_merge_sccs_data {
4859 struct isl_sched_graph *graph;
4860 int *scc_cluster;
4861 int src;
4862 int dst;
4865 /* Check whether the cluster containing node "i" depends on the cluster
4866 * containing node "j". If "i" and "j" belong to the same cluster,
4867 * then they are taken to depend on each other to ensure that
4868 * the resulting strongly connected component consists of complete
4869 * clusters. Furthermore, if "i" and "j" are the two nodes that
4870 * are being merged, then they are taken to depend on each other as well.
4871 * Otherwise, check if there is a (conditional) validity dependence
4872 * from node[j] to node[i], forcing node[i] to follow node[j].
4874 static isl_bool cluster_follows(int i, int j, void *user)
4876 struct isl_mark_merge_sccs_data *data = user;
4877 struct isl_sched_graph *graph = data->graph;
4878 int *scc_cluster = data->scc_cluster;
4880 if (data->src == i && data->dst == j)
4881 return isl_bool_true;
4882 if (data->src == j && data->dst == i)
4883 return isl_bool_true;
4884 if (scc_cluster[graph->node[i].scc] == scc_cluster[graph->node[j].scc])
4885 return isl_bool_true;
4887 return graph_has_validity_edge(graph, &graph->node[j], &graph->node[i]);
4890 /* Mark all SCCs that belong to either of the two clusters in "c"
4891 * connected by the edge in "graph" with index "edge", or to any
4892 * of the intermediate clusters.
4893 * The marking is recorded in c->scc_in_merge.
4895 * The given edge has been selected for merging two clusters,
4896 * meaning that there is at least a proximity edge between the two nodes.
4897 * However, there may also be (indirect) validity dependences
4898 * between the two nodes. When merging the two clusters, all clusters
4899 * containing one or more of the intermediate nodes along the
4900 * indirect validity dependences need to be merged in as well.
4902 * First collect all such nodes by computing the strongly connected
4903 * component (SCC) containing the two nodes connected by the edge, where
4904 * the two nodes are considered to depend on each other to make
4905 * sure they end up in the same SCC. Similarly, each node is considered
4906 * to depend on every other node in the same cluster to ensure
4907 * that the SCC consists of complete clusters.
4909 * Then the original SCCs that contain any of these nodes are marked
4910 * in c->scc_in_merge.
4912 static isl_stat mark_merge_sccs(isl_ctx *ctx, struct isl_sched_graph *graph,
4913 int edge, struct isl_clustering *c)
4915 struct isl_mark_merge_sccs_data data;
4916 struct isl_tarjan_graph *g;
4917 int i;
4919 for (i = 0; i < c->n; ++i)
4920 c->scc_in_merge[i] = 0;
4922 data.graph = graph;
4923 data.scc_cluster = c->scc_cluster;
4924 data.src = graph->edge[edge].src - graph->node;
4925 data.dst = graph->edge[edge].dst - graph->node;
4927 g = isl_tarjan_graph_component(ctx, graph->n, data.dst,
4928 &cluster_follows, &data);
4929 if (!g)
4930 goto error;
4932 i = g->op;
4933 if (i < 3)
4934 isl_die(ctx, isl_error_internal,
4935 "expecting at least two nodes in component",
4936 goto error);
4937 if (g->order[--i] != -1)
4938 isl_die(ctx, isl_error_internal,
4939 "expecting end of component marker", goto error);
4941 for (--i; i >= 0 && g->order[i] != -1; --i) {
4942 int scc = graph->node[g->order[i]].scc;
4943 c->scc_in_merge[scc] = 1;
4946 isl_tarjan_graph_free(g);
4947 return isl_stat_ok;
4948 error:
4949 isl_tarjan_graph_free(g);
4950 return isl_stat_error;
4953 /* Construct the identifier "cluster_i".
4955 static __isl_give isl_id *cluster_id(isl_ctx *ctx, int i)
4957 char name[40];
4959 snprintf(name, sizeof(name), "cluster_%d", i);
4960 return isl_id_alloc(ctx, name, NULL);
4963 /* Construct the space of the cluster with index "i" containing
4964 * the strongly connected component "scc".
4966 * In particular, construct a space called cluster_i with dimension equal
4967 * to the number of schedule rows in the current band of "scc".
4969 static __isl_give isl_space *cluster_space(struct isl_sched_graph *scc, int i)
4971 int nvar;
4972 isl_space *space;
4973 isl_id *id;
4975 nvar = scc->n_total_row - scc->band_start;
4976 space = isl_space_copy(scc->node[0].space);
4977 space = isl_space_params(space);
4978 space = isl_space_set_from_params(space);
4979 space = isl_space_add_dims(space, isl_dim_set, nvar);
4980 id = cluster_id(isl_space_get_ctx(space), i);
4981 space = isl_space_set_tuple_id(space, isl_dim_set, id);
4983 return space;
4986 /* Collect the domain of the graph for merging clusters.
4988 * In particular, for each cluster with first SCC "i", construct
4989 * a set in the space called cluster_i with dimension equal
4990 * to the number of schedule rows in the current band of the cluster.
4992 static __isl_give isl_union_set *collect_domain(isl_ctx *ctx,
4993 struct isl_sched_graph *graph, struct isl_clustering *c)
4995 int i;
4996 isl_space *space;
4997 isl_union_set *domain;
4999 space = isl_space_params_alloc(ctx, 0);
5000 domain = isl_union_set_empty(space);
5002 for (i = 0; i < graph->scc; ++i) {
5003 isl_space *space;
5005 if (!c->scc_in_merge[i])
5006 continue;
5007 if (c->scc_cluster[i] != i)
5008 continue;
5009 space = cluster_space(&c->scc[i], i);
5010 domain = isl_union_set_add_set(domain, isl_set_universe(space));
5013 return domain;
5016 /* Construct a map from the original instances to the corresponding
5017 * cluster instance in the current bands of the clusters in "c".
5019 static __isl_give isl_union_map *collect_cluster_map(isl_ctx *ctx,
5020 struct isl_sched_graph *graph, struct isl_clustering *c)
5022 int i, j;
5023 isl_space *space;
5024 isl_union_map *cluster_map;
5026 space = isl_space_params_alloc(ctx, 0);
5027 cluster_map = isl_union_map_empty(space);
5028 for (i = 0; i < graph->scc; ++i) {
5029 int start, n;
5030 isl_id *id;
5032 if (!c->scc_in_merge[i])
5033 continue;
5035 id = cluster_id(ctx, c->scc_cluster[i]);
5036 start = c->scc[i].band_start;
5037 n = c->scc[i].n_total_row - start;
5038 for (j = 0; j < c->scc[i].n; ++j) {
5039 isl_multi_aff *ma;
5040 isl_map *map;
5041 struct isl_sched_node *node = &c->scc[i].node[j];
5043 ma = node_extract_partial_schedule_multi_aff(node,
5044 start, n);
5045 ma = isl_multi_aff_set_tuple_id(ma, isl_dim_out,
5046 isl_id_copy(id));
5047 map = isl_map_from_multi_aff(ma);
5048 cluster_map = isl_union_map_add_map(cluster_map, map);
5050 isl_id_free(id);
5053 return cluster_map;
5056 /* Add "umap" to the schedule constraints "sc" of all types of "edge"
5057 * that are not isl_edge_condition or isl_edge_conditional_validity.
5059 static __isl_give isl_schedule_constraints *add_non_conditional_constraints(
5060 struct isl_sched_edge *edge, __isl_keep isl_union_map *umap,
5061 __isl_take isl_schedule_constraints *sc)
5063 enum isl_edge_type t;
5065 if (!sc)
5066 return NULL;
5068 for (t = isl_edge_first; t <= isl_edge_last; ++t) {
5069 if (t == isl_edge_condition ||
5070 t == isl_edge_conditional_validity)
5071 continue;
5072 if (!is_type(edge, t))
5073 continue;
5074 sc->constraint[t] = isl_union_map_union(sc->constraint[t],
5075 isl_union_map_copy(umap));
5076 if (!sc->constraint[t])
5077 return isl_schedule_constraints_free(sc);
5080 return sc;
5083 /* Add schedule constraints of types isl_edge_condition and
5084 * isl_edge_conditional_validity to "sc" by applying "umap" to
5085 * the domains of the wrapped relations in domain and range
5086 * of the corresponding tagged constraints of "edge".
5088 static __isl_give isl_schedule_constraints *add_conditional_constraints(
5089 struct isl_sched_edge *edge, __isl_keep isl_union_map *umap,
5090 __isl_take isl_schedule_constraints *sc)
5092 enum isl_edge_type t;
5093 isl_union_map *tagged;
5095 for (t = isl_edge_condition; t <= isl_edge_conditional_validity; ++t) {
5096 if (!is_type(edge, t))
5097 continue;
5098 if (t == isl_edge_condition)
5099 tagged = isl_union_map_copy(edge->tagged_condition);
5100 else
5101 tagged = isl_union_map_copy(edge->tagged_validity);
5102 tagged = isl_union_map_zip(tagged);
5103 tagged = isl_union_map_apply_domain(tagged,
5104 isl_union_map_copy(umap));
5105 tagged = isl_union_map_zip(tagged);
5106 sc->constraint[t] = isl_union_map_union(sc->constraint[t],
5107 tagged);
5108 if (!sc->constraint[t])
5109 return isl_schedule_constraints_free(sc);
5112 return sc;
5115 /* Given a mapping "cluster_map" from the original instances to
5116 * the cluster instances, add schedule constraints on the clusters
5117 * to "sc" corresponding to the original constraints represented by "edge".
5119 * For non-tagged dependence constraints, the cluster constraints
5120 * are obtained by applying "cluster_map" to the edge->map.
5122 * For tagged dependence constraints, "cluster_map" needs to be applied
5123 * to the domains of the wrapped relations in domain and range
5124 * of the tagged dependence constraints. Pick out the mappings
5125 * from these domains from "cluster_map" and construct their product.
5126 * This mapping can then be applied to the pair of domains.
5128 static __isl_give isl_schedule_constraints *collect_edge_constraints(
5129 struct isl_sched_edge *edge, __isl_keep isl_union_map *cluster_map,
5130 __isl_take isl_schedule_constraints *sc)
5132 isl_union_map *umap;
5133 isl_space *space;
5134 isl_union_set *uset;
5135 isl_union_map *umap1, *umap2;
5137 if (!sc)
5138 return NULL;
5140 umap = isl_union_map_from_map(isl_map_copy(edge->map));
5141 umap = isl_union_map_apply_domain(umap,
5142 isl_union_map_copy(cluster_map));
5143 umap = isl_union_map_apply_range(umap,
5144 isl_union_map_copy(cluster_map));
5145 sc = add_non_conditional_constraints(edge, umap, sc);
5146 isl_union_map_free(umap);
5148 if (!sc || (!is_condition(edge) && !is_conditional_validity(edge)))
5149 return sc;
5151 space = isl_space_domain(isl_map_get_space(edge->map));
5152 uset = isl_union_set_from_set(isl_set_universe(space));
5153 umap1 = isl_union_map_copy(cluster_map);
5154 umap1 = isl_union_map_intersect_domain(umap1, uset);
5155 space = isl_space_range(isl_map_get_space(edge->map));
5156 uset = isl_union_set_from_set(isl_set_universe(space));
5157 umap2 = isl_union_map_copy(cluster_map);
5158 umap2 = isl_union_map_intersect_domain(umap2, uset);
5159 umap = isl_union_map_product(umap1, umap2);
5161 sc = add_conditional_constraints(edge, umap, sc);
5163 isl_union_map_free(umap);
5164 return sc;
5167 /* Given a mapping "cluster_map" from the original instances to
5168 * the cluster instances, add schedule constraints on the clusters
5169 * to "sc" corresponding to all edges in "graph" between nodes that
5170 * belong to SCCs that are marked for merging in "scc_in_merge".
5172 static __isl_give isl_schedule_constraints *collect_constraints(
5173 struct isl_sched_graph *graph, int *scc_in_merge,
5174 __isl_keep isl_union_map *cluster_map,
5175 __isl_take isl_schedule_constraints *sc)
5177 int i;
5179 for (i = 0; i < graph->n_edge; ++i) {
5180 struct isl_sched_edge *edge = &graph->edge[i];
5182 if (!scc_in_merge[edge->src->scc])
5183 continue;
5184 if (!scc_in_merge[edge->dst->scc])
5185 continue;
5186 sc = collect_edge_constraints(edge, cluster_map, sc);
5189 return sc;
5192 /* Construct a dependence graph for scheduling clusters with respect
5193 * to each other and store the result in "merge_graph".
5194 * In particular, the nodes of the graph correspond to the schedule
5195 * dimensions of the current bands of those clusters that have been
5196 * marked for merging in "c".
5198 * First construct an isl_schedule_constraints object for this domain
5199 * by transforming the edges in "graph" to the domain.
5200 * Then initialize a dependence graph for scheduling from these
5201 * constraints.
5203 static isl_stat init_merge_graph(isl_ctx *ctx, struct isl_sched_graph *graph,
5204 struct isl_clustering *c, struct isl_sched_graph *merge_graph)
5206 isl_union_set *domain;
5207 isl_union_map *cluster_map;
5208 isl_schedule_constraints *sc;
5209 isl_stat r;
5211 domain = collect_domain(ctx, graph, c);
5212 sc = isl_schedule_constraints_on_domain(domain);
5213 if (!sc)
5214 return isl_stat_error;
5215 cluster_map = collect_cluster_map(ctx, graph, c);
5216 sc = collect_constraints(graph, c->scc_in_merge, cluster_map, sc);
5217 isl_union_map_free(cluster_map);
5219 r = graph_init(merge_graph, sc);
5221 isl_schedule_constraints_free(sc);
5223 return r;
5226 /* Compute the maximal number of remaining schedule rows that still need
5227 * to be computed for the nodes that belong to clusters with the maximal
5228 * dimension for the current band (i.e., the band that is to be merged).
5229 * Only clusters that are about to be merged are considered.
5230 * "maxvar" is the maximal dimension for the current band.
5231 * "c" contains information about the clusters.
5233 * Return the maximal number of remaining schedule rows or -1 on error.
5235 static int compute_maxvar_max_slack(int maxvar, struct isl_clustering *c)
5237 int i, j;
5238 int max_slack;
5240 max_slack = 0;
5241 for (i = 0; i < c->n; ++i) {
5242 int nvar;
5243 struct isl_sched_graph *scc;
5245 if (!c->scc_in_merge[i])
5246 continue;
5247 scc = &c->scc[i];
5248 nvar = scc->n_total_row - scc->band_start;
5249 if (nvar != maxvar)
5250 continue;
5251 for (j = 0; j < scc->n; ++j) {
5252 struct isl_sched_node *node = &scc->node[j];
5253 int slack;
5255 if (node_update_cmap(node) < 0)
5256 return -1;
5257 slack = node->nvar - node->rank;
5258 if (slack > max_slack)
5259 max_slack = slack;
5263 return max_slack;
5266 /* If there are any clusters where the dimension of the current band
5267 * (i.e., the band that is to be merged) is smaller than "maxvar" and
5268 * if there are any nodes in such a cluster where the number
5269 * of remaining schedule rows that still need to be computed
5270 * is greater than "max_slack", then return the smallest current band
5271 * dimension of all these clusters. Otherwise return the original value
5272 * of "maxvar". Return -1 in case of any error.
5273 * Only clusters that are about to be merged are considered.
5274 * "c" contains information about the clusters.
5276 static int limit_maxvar_to_slack(int maxvar, int max_slack,
5277 struct isl_clustering *c)
5279 int i, j;
5281 for (i = 0; i < c->n; ++i) {
5282 int nvar;
5283 struct isl_sched_graph *scc;
5285 if (!c->scc_in_merge[i])
5286 continue;
5287 scc = &c->scc[i];
5288 nvar = scc->n_total_row - scc->band_start;
5289 if (nvar >= maxvar)
5290 continue;
5291 for (j = 0; j < scc->n; ++j) {
5292 struct isl_sched_node *node = &scc->node[j];
5293 int slack;
5295 if (node_update_cmap(node) < 0)
5296 return -1;
5297 slack = node->nvar - node->rank;
5298 if (slack > max_slack) {
5299 maxvar = nvar;
5300 break;
5305 return maxvar;
5308 /* Adjust merge_graph->maxvar based on the number of remaining schedule rows
5309 * that still need to be computed. In particular, if there is a node
5310 * in a cluster where the dimension of the current band is smaller
5311 * than merge_graph->maxvar, but the number of remaining schedule rows
5312 * is greater than that of any node in a cluster with the maximal
5313 * dimension for the current band (i.e., merge_graph->maxvar),
5314 * then adjust merge_graph->maxvar to the (smallest) current band dimension
5315 * of those clusters. Without this adjustment, the total number of
5316 * schedule dimensions would be increased, resulting in a skewed view
5317 * of the number of coincident dimensions.
5318 * "c" contains information about the clusters.
5320 * If the maximize_band_depth option is set and merge_graph->maxvar is reduced,
5321 * then there is no point in attempting any merge since it will be rejected
5322 * anyway. Set merge_graph->maxvar to zero in such cases.
5324 static isl_stat adjust_maxvar_to_slack(isl_ctx *ctx,
5325 struct isl_sched_graph *merge_graph, struct isl_clustering *c)
5327 int max_slack, maxvar;
5329 max_slack = compute_maxvar_max_slack(merge_graph->maxvar, c);
5330 if (max_slack < 0)
5331 return isl_stat_error;
5332 maxvar = limit_maxvar_to_slack(merge_graph->maxvar, max_slack, c);
5333 if (maxvar < 0)
5334 return isl_stat_error;
5336 if (maxvar < merge_graph->maxvar) {
5337 if (isl_options_get_schedule_maximize_band_depth(ctx))
5338 merge_graph->maxvar = 0;
5339 else
5340 merge_graph->maxvar = maxvar;
5343 return isl_stat_ok;
5346 /* Return the number of coincident dimensions in the current band of "graph",
5347 * where the nodes of "graph" are assumed to be scheduled by a single band.
5349 static int get_n_coincident(struct isl_sched_graph *graph)
5351 int i;
5353 for (i = graph->band_start; i < graph->n_total_row; ++i)
5354 if (!graph->node[0].coincident[i])
5355 break;
5357 return i - graph->band_start;
5360 /* Should the clusters be merged based on the cluster schedule
5361 * in the current (and only) band of "merge_graph", given that
5362 * coincidence should be maximized?
5364 * If the number of coincident schedule dimensions in the merged band
5365 * would be less than the maximal number of coincident schedule dimensions
5366 * in any of the merged clusters, then the clusters should not be merged.
5368 static isl_bool ok_to_merge_coincident(struct isl_clustering *c,
5369 struct isl_sched_graph *merge_graph)
5371 int i;
5372 int n_coincident;
5373 int max_coincident;
5375 max_coincident = 0;
5376 for (i = 0; i < c->n; ++i) {
5377 if (!c->scc_in_merge[i])
5378 continue;
5379 n_coincident = get_n_coincident(&c->scc[i]);
5380 if (n_coincident > max_coincident)
5381 max_coincident = n_coincident;
5384 n_coincident = get_n_coincident(merge_graph);
5386 return n_coincident >= max_coincident;
5389 /* Return the transformation on "node" expressed by the current (and only)
5390 * band of "merge_graph" applied to the clusters in "c".
5392 * First find the representation of "node" in its SCC in "c" and
5393 * extract the transformation expressed by the current band.
5394 * Then extract the transformation applied by "merge_graph"
5395 * to the cluster to which this SCC belongs.
5396 * Combine the two to obtain the complete transformation on the node.
5398 * Note that the range of the first transformation is an anonymous space,
5399 * while the domain of the second is named "cluster_X". The range
5400 * of the former therefore needs to be adjusted before the two
5401 * can be combined.
5403 static __isl_give isl_map *extract_node_transformation(isl_ctx *ctx,
5404 struct isl_sched_node *node, struct isl_clustering *c,
5405 struct isl_sched_graph *merge_graph)
5407 struct isl_sched_node *scc_node, *cluster_node;
5408 int start, n;
5409 isl_id *id;
5410 isl_space *space;
5411 isl_multi_aff *ma, *ma2;
5413 scc_node = graph_find_node(ctx, &c->scc[node->scc], node->space);
5414 start = c->scc[node->scc].band_start;
5415 n = c->scc[node->scc].n_total_row - start;
5416 ma = node_extract_partial_schedule_multi_aff(scc_node, start, n);
5417 space = cluster_space(&c->scc[node->scc], c->scc_cluster[node->scc]);
5418 cluster_node = graph_find_node(ctx, merge_graph, space);
5419 if (space && !cluster_node)
5420 isl_die(ctx, isl_error_internal, "unable to find cluster",
5421 space = isl_space_free(space));
5422 id = isl_space_get_tuple_id(space, isl_dim_set);
5423 ma = isl_multi_aff_set_tuple_id(ma, isl_dim_out, id);
5424 isl_space_free(space);
5425 n = merge_graph->n_total_row;
5426 ma2 = node_extract_partial_schedule_multi_aff(cluster_node, 0, n);
5427 ma = isl_multi_aff_pullback_multi_aff(ma2, ma);
5429 return isl_map_from_multi_aff(ma);
5432 /* Give a set of distances "set", are they bounded by a small constant
5433 * in direction "pos"?
5434 * In practice, check if they are bounded by 2 by checking that there
5435 * are no elements with a value greater than or equal to 3 or
5436 * smaller than or equal to -3.
5438 static isl_bool distance_is_bounded(__isl_keep isl_set *set, int pos)
5440 isl_bool bounded;
5441 isl_set *test;
5443 if (!set)
5444 return isl_bool_error;
5446 test = isl_set_copy(set);
5447 test = isl_set_lower_bound_si(test, isl_dim_set, pos, 3);
5448 bounded = isl_set_is_empty(test);
5449 isl_set_free(test);
5451 if (bounded < 0 || !bounded)
5452 return bounded;
5454 test = isl_set_copy(set);
5455 test = isl_set_upper_bound_si(test, isl_dim_set, pos, -3);
5456 bounded = isl_set_is_empty(test);
5457 isl_set_free(test);
5459 return bounded;
5462 /* Does the set "set" have a fixed (but possible parametric) value
5463 * at dimension "pos"?
5465 static isl_bool has_single_value(__isl_keep isl_set *set, int pos)
5467 int n;
5468 isl_bool single;
5470 if (!set)
5471 return isl_bool_error;
5472 set = isl_set_copy(set);
5473 n = isl_set_dim(set, isl_dim_set);
5474 set = isl_set_project_out(set, isl_dim_set, pos + 1, n - (pos + 1));
5475 set = isl_set_project_out(set, isl_dim_set, 0, pos);
5476 single = isl_set_is_singleton(set);
5477 isl_set_free(set);
5479 return single;
5482 /* Does "map" have a fixed (but possible parametric) value
5483 * at dimension "pos" of either its domain or its range?
5485 static isl_bool has_singular_src_or_dst(__isl_keep isl_map *map, int pos)
5487 isl_set *set;
5488 isl_bool single;
5490 set = isl_map_domain(isl_map_copy(map));
5491 single = has_single_value(set, pos);
5492 isl_set_free(set);
5494 if (single < 0 || single)
5495 return single;
5497 set = isl_map_range(isl_map_copy(map));
5498 single = has_single_value(set, pos);
5499 isl_set_free(set);
5501 return single;
5504 /* Does the edge "edge" from "graph" have bounded dependence distances
5505 * in the merged graph "merge_graph" of a selection of clusters in "c"?
5507 * Extract the complete transformations of the source and destination
5508 * nodes of the edge, apply them to the edge constraints and
5509 * compute the differences. Finally, check if these differences are bounded
5510 * in each direction.
5512 * If the dimension of the band is greater than the number of
5513 * dimensions that can be expected to be optimized by the edge
5514 * (based on its weight), then also allow the differences to be unbounded
5515 * in the remaining dimensions, but only if either the source or
5516 * the destination has a fixed value in that direction.
5517 * This allows a statement that produces values that are used by
5518 * several instance of another statement to be merged with that
5519 * other statement.
5520 * However, merging such clusters will introduce an inherently
5521 * large proximity distance inside the merged cluster, meaning
5522 * that proximity distances will no longer be optimized in
5523 * subsequent merges. These merges are therefore only allowed
5524 * after all other possible merges have been tried.
5525 * The first time such a merge is encountered, the weight of the edge
5526 * is replaced by a negative weight. The second time (i.e., after
5527 * all merges over edges with a non-negative weight have been tried),
5528 * the merge is allowed.
5530 static isl_bool has_bounded_distances(isl_ctx *ctx, struct isl_sched_edge *edge,
5531 struct isl_sched_graph *graph, struct isl_clustering *c,
5532 struct isl_sched_graph *merge_graph)
5534 int i, n, n_slack;
5535 isl_bool bounded;
5536 isl_map *map, *t;
5537 isl_set *dist;
5539 map = isl_map_copy(edge->map);
5540 t = extract_node_transformation(ctx, edge->src, c, merge_graph);
5541 map = isl_map_apply_domain(map, t);
5542 t = extract_node_transformation(ctx, edge->dst, c, merge_graph);
5543 map = isl_map_apply_range(map, t);
5544 dist = isl_map_deltas(isl_map_copy(map));
5546 bounded = isl_bool_true;
5547 n = isl_set_dim(dist, isl_dim_set);
5548 n_slack = n - edge->weight;
5549 if (edge->weight < 0)
5550 n_slack -= graph->max_weight + 1;
5551 for (i = 0; i < n; ++i) {
5552 isl_bool bounded_i, singular_i;
5554 bounded_i = distance_is_bounded(dist, i);
5555 if (bounded_i < 0)
5556 goto error;
5557 if (bounded_i)
5558 continue;
5559 if (edge->weight >= 0)
5560 bounded = isl_bool_false;
5561 n_slack--;
5562 if (n_slack < 0)
5563 break;
5564 singular_i = has_singular_src_or_dst(map, i);
5565 if (singular_i < 0)
5566 goto error;
5567 if (singular_i)
5568 continue;
5569 bounded = isl_bool_false;
5570 break;
5572 if (!bounded && i >= n && edge->weight >= 0)
5573 edge->weight -= graph->max_weight + 1;
5574 isl_map_free(map);
5575 isl_set_free(dist);
5577 return bounded;
5578 error:
5579 isl_map_free(map);
5580 isl_set_free(dist);
5581 return isl_bool_error;
5584 /* Should the clusters be merged based on the cluster schedule
5585 * in the current (and only) band of "merge_graph"?
5586 * "graph" is the original dependence graph, while "c" records
5587 * which SCCs are involved in the latest merge.
5589 * In particular, is there at least one proximity constraint
5590 * that is optimized by the merge?
5592 * A proximity constraint is considered to be optimized
5593 * if the dependence distances are small.
5595 static isl_bool ok_to_merge_proximity(isl_ctx *ctx,
5596 struct isl_sched_graph *graph, struct isl_clustering *c,
5597 struct isl_sched_graph *merge_graph)
5599 int i;
5601 for (i = 0; i < graph->n_edge; ++i) {
5602 struct isl_sched_edge *edge = &graph->edge[i];
5603 isl_bool bounded;
5605 if (!is_proximity(edge))
5606 continue;
5607 if (!c->scc_in_merge[edge->src->scc])
5608 continue;
5609 if (!c->scc_in_merge[edge->dst->scc])
5610 continue;
5611 if (c->scc_cluster[edge->dst->scc] ==
5612 c->scc_cluster[edge->src->scc])
5613 continue;
5614 bounded = has_bounded_distances(ctx, edge, graph, c,
5615 merge_graph);
5616 if (bounded < 0 || bounded)
5617 return bounded;
5620 return isl_bool_false;
5623 /* Should the clusters be merged based on the cluster schedule
5624 * in the current (and only) band of "merge_graph"?
5625 * "graph" is the original dependence graph, while "c" records
5626 * which SCCs are involved in the latest merge.
5628 * If the current band is empty, then the clusters should not be merged.
5630 * If the band depth should be maximized and the merge schedule
5631 * is incomplete (meaning that the dimension of some of the schedule
5632 * bands in the original schedule will be reduced), then the clusters
5633 * should not be merged.
5635 * If the schedule_maximize_coincidence option is set, then check that
5636 * the number of coincident schedule dimensions is not reduced.
5638 * Finally, only allow the merge if at least one proximity
5639 * constraint is optimized.
5641 static isl_bool ok_to_merge(isl_ctx *ctx, struct isl_sched_graph *graph,
5642 struct isl_clustering *c, struct isl_sched_graph *merge_graph)
5644 if (merge_graph->n_total_row == merge_graph->band_start)
5645 return isl_bool_false;
5647 if (isl_options_get_schedule_maximize_band_depth(ctx) &&
5648 merge_graph->n_total_row < merge_graph->maxvar)
5649 return isl_bool_false;
5651 if (isl_options_get_schedule_maximize_coincidence(ctx)) {
5652 isl_bool ok;
5654 ok = ok_to_merge_coincident(c, merge_graph);
5655 if (ok < 0 || !ok)
5656 return ok;
5659 return ok_to_merge_proximity(ctx, graph, c, merge_graph);
5662 /* Apply the schedule in "t_node" to the "n" rows starting at "first"
5663 * of the schedule in "node" and return the result.
5665 * That is, essentially compute
5667 * T * N(first:first+n-1)
5669 * taking into account the constant term and the parameter coefficients
5670 * in "t_node".
5672 static __isl_give isl_mat *node_transformation(isl_ctx *ctx,
5673 struct isl_sched_node *t_node, struct isl_sched_node *node,
5674 int first, int n)
5676 int i, j;
5677 isl_mat *t;
5678 int n_row, n_col, n_param, n_var;
5680 n_param = node->nparam;
5681 n_var = node->nvar;
5682 n_row = isl_mat_rows(t_node->sched);
5683 n_col = isl_mat_cols(node->sched);
5684 t = isl_mat_alloc(ctx, n_row, n_col);
5685 if (!t)
5686 return NULL;
5687 for (i = 0; i < n_row; ++i) {
5688 isl_seq_cpy(t->row[i], t_node->sched->row[i], 1 + n_param);
5689 isl_seq_clr(t->row[i] + 1 + n_param, n_var);
5690 for (j = 0; j < n; ++j)
5691 isl_seq_addmul(t->row[i],
5692 t_node->sched->row[i][1 + n_param + j],
5693 node->sched->row[first + j],
5694 1 + n_param + n_var);
5696 return t;
5699 /* Apply the cluster schedule in "t_node" to the current band
5700 * schedule of the nodes in "graph".
5702 * In particular, replace the rows starting at band_start
5703 * by the result of applying the cluster schedule in "t_node"
5704 * to the original rows.
5706 * The coincidence of the schedule is determined by the coincidence
5707 * of the cluster schedule.
5709 static isl_stat transform(isl_ctx *ctx, struct isl_sched_graph *graph,
5710 struct isl_sched_node *t_node)
5712 int i, j;
5713 int n_new;
5714 int start, n;
5716 start = graph->band_start;
5717 n = graph->n_total_row - start;
5719 n_new = isl_mat_rows(t_node->sched);
5720 for (i = 0; i < graph->n; ++i) {
5721 struct isl_sched_node *node = &graph->node[i];
5722 isl_mat *t;
5724 t = node_transformation(ctx, t_node, node, start, n);
5725 node->sched = isl_mat_drop_rows(node->sched, start, n);
5726 node->sched = isl_mat_concat(node->sched, t);
5727 node->sched_map = isl_map_free(node->sched_map);
5728 if (!node->sched)
5729 return isl_stat_error;
5730 for (j = 0; j < n_new; ++j)
5731 node->coincident[start + j] = t_node->coincident[j];
5733 graph->n_total_row -= n;
5734 graph->n_row -= n;
5735 graph->n_total_row += n_new;
5736 graph->n_row += n_new;
5738 return isl_stat_ok;
5741 /* Merge the clusters marked for merging in "c" into a single
5742 * cluster using the cluster schedule in the current band of "merge_graph".
5743 * The representative SCC for the new cluster is the SCC with
5744 * the smallest index.
5746 * The current band schedule of each SCC in the new cluster is obtained
5747 * by applying the schedule of the corresponding original cluster
5748 * to the original band schedule.
5749 * All SCCs in the new cluster have the same number of schedule rows.
5751 static isl_stat merge(isl_ctx *ctx, struct isl_clustering *c,
5752 struct isl_sched_graph *merge_graph)
5754 int i;
5755 int cluster = -1;
5756 isl_space *space;
5758 for (i = 0; i < c->n; ++i) {
5759 struct isl_sched_node *node;
5761 if (!c->scc_in_merge[i])
5762 continue;
5763 if (cluster < 0)
5764 cluster = i;
5765 space = cluster_space(&c->scc[i], c->scc_cluster[i]);
5766 if (!space)
5767 return isl_stat_error;
5768 node = graph_find_node(ctx, merge_graph, space);
5769 isl_space_free(space);
5770 if (!node)
5771 isl_die(ctx, isl_error_internal,
5772 "unable to find cluster",
5773 return isl_stat_error);
5774 if (transform(ctx, &c->scc[i], node) < 0)
5775 return isl_stat_error;
5776 c->scc_cluster[i] = cluster;
5779 return isl_stat_ok;
5782 /* Try and merge the clusters of SCCs marked in c->scc_in_merge
5783 * by scheduling the current cluster bands with respect to each other.
5785 * Construct a dependence graph with a space for each cluster and
5786 * with the coordinates of each space corresponding to the schedule
5787 * dimensions of the current band of that cluster.
5788 * Construct a cluster schedule in this cluster dependence graph and
5789 * apply it to the current cluster bands if it is applicable
5790 * according to ok_to_merge.
5792 * If the number of remaining schedule dimensions in a cluster
5793 * with a non-maximal current schedule dimension is greater than
5794 * the number of remaining schedule dimensions in clusters
5795 * with a maximal current schedule dimension, then restrict
5796 * the number of rows to be computed in the cluster schedule
5797 * to the minimal such non-maximal current schedule dimension.
5798 * Do this by adjusting merge_graph.maxvar.
5800 * Return isl_bool_true if the clusters have effectively been merged
5801 * into a single cluster.
5803 * Note that since the standard scheduling algorithm minimizes the maximal
5804 * distance over proximity constraints, the proximity constraints between
5805 * the merged clusters may not be optimized any further than what is
5806 * sufficient to bring the distances within the limits of the internal
5807 * proximity constraints inside the individual clusters.
5808 * It may therefore make sense to perform an additional translation step
5809 * to bring the clusters closer to each other, while maintaining
5810 * the linear part of the merging schedule found using the standard
5811 * scheduling algorithm.
5813 static isl_bool try_merge(isl_ctx *ctx, struct isl_sched_graph *graph,
5814 struct isl_clustering *c)
5816 struct isl_sched_graph merge_graph = { 0 };
5817 isl_bool merged;
5819 if (init_merge_graph(ctx, graph, c, &merge_graph) < 0)
5820 goto error;
5822 if (compute_maxvar(&merge_graph) < 0)
5823 goto error;
5824 if (adjust_maxvar_to_slack(ctx, &merge_graph,c) < 0)
5825 goto error;
5826 if (compute_schedule_wcc_band(ctx, &merge_graph) < 0)
5827 goto error;
5828 merged = ok_to_merge(ctx, graph, c, &merge_graph);
5829 if (merged && merge(ctx, c, &merge_graph) < 0)
5830 goto error;
5832 graph_free(ctx, &merge_graph);
5833 return merged;
5834 error:
5835 graph_free(ctx, &merge_graph);
5836 return isl_bool_error;
5839 /* Is there any edge marked "no_merge" between two SCCs that are
5840 * about to be merged (i.e., that are set in "scc_in_merge")?
5841 * "merge_edge" is the proximity edge along which the clusters of SCCs
5842 * are going to be merged.
5844 * If there is any edge between two SCCs with a negative weight,
5845 * while the weight of "merge_edge" is non-negative, then this
5846 * means that the edge was postponed. "merge_edge" should then
5847 * also be postponed since merging along the edge with negative weight should
5848 * be postponed until all edges with non-negative weight have been tried.
5849 * Replace the weight of "merge_edge" by a negative weight as well and
5850 * tell the caller not to attempt a merge.
5852 static int any_no_merge(struct isl_sched_graph *graph, int *scc_in_merge,
5853 struct isl_sched_edge *merge_edge)
5855 int i;
5857 for (i = 0; i < graph->n_edge; ++i) {
5858 struct isl_sched_edge *edge = &graph->edge[i];
5860 if (!scc_in_merge[edge->src->scc])
5861 continue;
5862 if (!scc_in_merge[edge->dst->scc])
5863 continue;
5864 if (edge->no_merge)
5865 return 1;
5866 if (merge_edge->weight >= 0 && edge->weight < 0) {
5867 merge_edge->weight -= graph->max_weight + 1;
5868 return 1;
5872 return 0;
5875 /* Merge the two clusters in "c" connected by the edge in "graph"
5876 * with index "edge" into a single cluster.
5877 * If it turns out to be impossible to merge these two clusters,
5878 * then mark the edge as "no_merge" such that it will not be
5879 * considered again.
5881 * First mark all SCCs that need to be merged. This includes the SCCs
5882 * in the two clusters, but it may also include the SCCs
5883 * of intermediate clusters.
5884 * If there is already a no_merge edge between any pair of such SCCs,
5885 * then simply mark the current edge as no_merge as well.
5886 * Likewise, if any of those edges was postponed by has_bounded_distances,
5887 * then postpone the current edge as well.
5888 * Otherwise, try and merge the clusters and mark "edge" as "no_merge"
5889 * if the clusters did not end up getting merged, unless the non-merge
5890 * is due to the fact that the edge was postponed. This postponement
5891 * can be recognized by a change in weight (from non-negative to negative).
5893 static isl_stat merge_clusters_along_edge(isl_ctx *ctx,
5894 struct isl_sched_graph *graph, int edge, struct isl_clustering *c)
5896 isl_bool merged;
5897 int edge_weight = graph->edge[edge].weight;
5899 if (mark_merge_sccs(ctx, graph, edge, c) < 0)
5900 return isl_stat_error;
5902 if (any_no_merge(graph, c->scc_in_merge, &graph->edge[edge]))
5903 merged = isl_bool_false;
5904 else
5905 merged = try_merge(ctx, graph, c);
5906 if (merged < 0)
5907 return isl_stat_error;
5908 if (!merged && edge_weight == graph->edge[edge].weight)
5909 graph->edge[edge].no_merge = 1;
5911 return isl_stat_ok;
5914 /* Does "node" belong to the cluster identified by "cluster"?
5916 static int node_cluster_exactly(struct isl_sched_node *node, int cluster)
5918 return node->cluster == cluster;
5921 /* Does "edge" connect two nodes belonging to the cluster
5922 * identified by "cluster"?
5924 static int edge_cluster_exactly(struct isl_sched_edge *edge, int cluster)
5926 return edge->src->cluster == cluster && edge->dst->cluster == cluster;
5929 /* Swap the schedule of "node1" and "node2".
5930 * Both nodes have been derived from the same node in a common parent graph.
5931 * Since the "coincident" field is shared with that node
5932 * in the parent graph, there is no need to also swap this field.
5934 static void swap_sched(struct isl_sched_node *node1,
5935 struct isl_sched_node *node2)
5937 isl_mat *sched;
5938 isl_map *sched_map;
5940 sched = node1->sched;
5941 node1->sched = node2->sched;
5942 node2->sched = sched;
5944 sched_map = node1->sched_map;
5945 node1->sched_map = node2->sched_map;
5946 node2->sched_map = sched_map;
5949 /* Copy the current band schedule from the SCCs that form the cluster
5950 * with index "pos" to the actual cluster at position "pos".
5951 * By construction, the index of the first SCC that belongs to the cluster
5952 * is also "pos".
5954 * The order of the nodes inside both the SCCs and the cluster
5955 * is assumed to be same as the order in the original "graph".
5957 * Since the SCC graphs will no longer be used after this function,
5958 * the schedules are actually swapped rather than copied.
5960 static isl_stat copy_partial(struct isl_sched_graph *graph,
5961 struct isl_clustering *c, int pos)
5963 int i, j;
5965 c->cluster[pos].n_total_row = c->scc[pos].n_total_row;
5966 c->cluster[pos].n_row = c->scc[pos].n_row;
5967 c->cluster[pos].maxvar = c->scc[pos].maxvar;
5968 j = 0;
5969 for (i = 0; i < graph->n; ++i) {
5970 int k;
5971 int s;
5973 if (graph->node[i].cluster != pos)
5974 continue;
5975 s = graph->node[i].scc;
5976 k = c->scc_node[s]++;
5977 swap_sched(&c->cluster[pos].node[j], &c->scc[s].node[k]);
5978 if (c->scc[s].maxvar > c->cluster[pos].maxvar)
5979 c->cluster[pos].maxvar = c->scc[s].maxvar;
5980 ++j;
5983 return isl_stat_ok;
5986 /* Is there a (conditional) validity dependence from node[j] to node[i],
5987 * forcing node[i] to follow node[j] or do the nodes belong to the same
5988 * cluster?
5990 static isl_bool node_follows_strong_or_same_cluster(int i, int j, void *user)
5992 struct isl_sched_graph *graph = user;
5994 if (graph->node[i].cluster == graph->node[j].cluster)
5995 return isl_bool_true;
5996 return graph_has_validity_edge(graph, &graph->node[j], &graph->node[i]);
5999 /* Extract the merged clusters of SCCs in "graph", sort them, and
6000 * store them in c->clusters. Update c->scc_cluster accordingly.
6002 * First keep track of the cluster containing the SCC to which a node
6003 * belongs in the node itself.
6004 * Then extract the clusters into c->clusters, copying the current
6005 * band schedule from the SCCs that belong to the cluster.
6006 * Do this only once per cluster.
6008 * Finally, topologically sort the clusters and update c->scc_cluster
6009 * to match the new scc numbering. While the SCCs were originally
6010 * sorted already, some SCCs that depend on some other SCCs may
6011 * have been merged with SCCs that appear before these other SCCs.
6012 * A reordering may therefore be required.
6014 static isl_stat extract_clusters(isl_ctx *ctx, struct isl_sched_graph *graph,
6015 struct isl_clustering *c)
6017 int i;
6019 for (i = 0; i < graph->n; ++i)
6020 graph->node[i].cluster = c->scc_cluster[graph->node[i].scc];
6022 for (i = 0; i < graph->scc; ++i) {
6023 if (c->scc_cluster[i] != i)
6024 continue;
6025 if (extract_sub_graph(ctx, graph, &node_cluster_exactly,
6026 &edge_cluster_exactly, i, &c->cluster[i]) < 0)
6027 return isl_stat_error;
6028 c->cluster[i].src_scc = -1;
6029 c->cluster[i].dst_scc = -1;
6030 if (copy_partial(graph, c, i) < 0)
6031 return isl_stat_error;
6034 if (detect_ccs(ctx, graph, &node_follows_strong_or_same_cluster) < 0)
6035 return isl_stat_error;
6036 for (i = 0; i < graph->n; ++i)
6037 c->scc_cluster[graph->node[i].scc] = graph->node[i].cluster;
6039 return isl_stat_ok;
6042 /* Compute weights on the proximity edges of "graph" that can
6043 * be used by find_proximity to find the most appropriate
6044 * proximity edge to use to merge two clusters in "c".
6045 * The weights are also used by has_bounded_distances to determine
6046 * whether the merge should be allowed.
6047 * Store the maximum of the computed weights in graph->max_weight.
6049 * The computed weight is a measure for the number of remaining schedule
6050 * dimensions that can still be completely aligned.
6051 * In particular, compute the number of equalities between
6052 * input dimensions and output dimensions in the proximity constraints.
6053 * The directions that are already handled by outer schedule bands
6054 * are projected out prior to determining this number.
6056 * Edges that will never be considered by find_proximity are ignored.
6058 static isl_stat compute_weights(struct isl_sched_graph *graph,
6059 struct isl_clustering *c)
6061 int i;
6063 graph->max_weight = 0;
6065 for (i = 0; i < graph->n_edge; ++i) {
6066 struct isl_sched_edge *edge = &graph->edge[i];
6067 struct isl_sched_node *src = edge->src;
6068 struct isl_sched_node *dst = edge->dst;
6069 isl_basic_map *hull;
6070 int n_in, n_out;
6072 if (!is_proximity(edge))
6073 continue;
6074 if (bad_cluster(&c->scc[edge->src->scc]) ||
6075 bad_cluster(&c->scc[edge->dst->scc]))
6076 continue;
6077 if (c->scc_cluster[edge->dst->scc] ==
6078 c->scc_cluster[edge->src->scc])
6079 continue;
6081 hull = isl_map_affine_hull(isl_map_copy(edge->map));
6082 hull = isl_basic_map_transform_dims(hull, isl_dim_in, 0,
6083 isl_mat_copy(src->ctrans));
6084 hull = isl_basic_map_transform_dims(hull, isl_dim_out, 0,
6085 isl_mat_copy(dst->ctrans));
6086 hull = isl_basic_map_project_out(hull,
6087 isl_dim_in, 0, src->rank);
6088 hull = isl_basic_map_project_out(hull,
6089 isl_dim_out, 0, dst->rank);
6090 hull = isl_basic_map_remove_divs(hull);
6091 n_in = isl_basic_map_dim(hull, isl_dim_in);
6092 n_out = isl_basic_map_dim(hull, isl_dim_out);
6093 hull = isl_basic_map_drop_constraints_not_involving_dims(hull,
6094 isl_dim_in, 0, n_in);
6095 hull = isl_basic_map_drop_constraints_not_involving_dims(hull,
6096 isl_dim_out, 0, n_out);
6097 if (!hull)
6098 return isl_stat_error;
6099 edge->weight = hull->n_eq;
6100 isl_basic_map_free(hull);
6102 if (edge->weight > graph->max_weight)
6103 graph->max_weight = edge->weight;
6106 return isl_stat_ok;
6109 /* Call compute_schedule_finish_band on each of the clusters in "c"
6110 * in their topological order. This order is determined by the scc
6111 * fields of the nodes in "graph".
6112 * Combine the results in a sequence expressing the topological order.
6114 * If there is only one cluster left, then there is no need to introduce
6115 * a sequence node. Also, in this case, the cluster necessarily contains
6116 * the SCC at position 0 in the original graph and is therefore also
6117 * stored in the first cluster of "c".
6119 static __isl_give isl_schedule_node *finish_bands_clustering(
6120 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph,
6121 struct isl_clustering *c)
6123 int i;
6124 isl_ctx *ctx;
6125 isl_union_set_list *filters;
6127 if (graph->scc == 1)
6128 return compute_schedule_finish_band(node, &c->cluster[0], 0);
6130 ctx = isl_schedule_node_get_ctx(node);
6132 filters = extract_sccs(ctx, graph);
6133 node = isl_schedule_node_insert_sequence(node, filters);
6135 for (i = 0; i < graph->scc; ++i) {
6136 int j = c->scc_cluster[i];
6137 node = isl_schedule_node_child(node, i);
6138 node = isl_schedule_node_child(node, 0);
6139 node = compute_schedule_finish_band(node, &c->cluster[j], 0);
6140 node = isl_schedule_node_parent(node);
6141 node = isl_schedule_node_parent(node);
6144 return node;
6147 /* Compute a schedule for a connected dependence graph by first considering
6148 * each strongly connected component (SCC) in the graph separately and then
6149 * incrementally combining them into clusters.
6150 * Return the updated schedule node.
6152 * Initially, each cluster consists of a single SCC, each with its
6153 * own band schedule. The algorithm then tries to merge pairs
6154 * of clusters along a proximity edge until no more suitable
6155 * proximity edges can be found. During this merging, the schedule
6156 * is maintained in the individual SCCs.
6157 * After the merging is completed, the full resulting clusters
6158 * are extracted and in finish_bands_clustering,
6159 * compute_schedule_finish_band is called on each of them to integrate
6160 * the band into "node" and to continue the computation.
6162 * compute_weights initializes the weights that are used by find_proximity.
6164 static __isl_give isl_schedule_node *compute_schedule_wcc_clustering(
6165 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph)
6167 isl_ctx *ctx;
6168 struct isl_clustering c;
6169 int i;
6171 ctx = isl_schedule_node_get_ctx(node);
6173 if (clustering_init(ctx, &c, graph) < 0)
6174 goto error;
6176 if (compute_weights(graph, &c) < 0)
6177 goto error;
6179 for (;;) {
6180 i = find_proximity(graph, &c);
6181 if (i < 0)
6182 goto error;
6183 if (i >= graph->n_edge)
6184 break;
6185 if (merge_clusters_along_edge(ctx, graph, i, &c) < 0)
6186 goto error;
6189 if (extract_clusters(ctx, graph, &c) < 0)
6190 goto error;
6192 node = finish_bands_clustering(node, graph, &c);
6194 clustering_free(ctx, &c);
6195 return node;
6196 error:
6197 clustering_free(ctx, &c);
6198 return isl_schedule_node_free(node);
6201 /* Compute a schedule for a connected dependence graph and return
6202 * the updated schedule node.
6204 * If Feautrier's algorithm is selected, we first recursively try to satisfy
6205 * as many validity dependences as possible. When all validity dependences
6206 * are satisfied we extend the schedule to a full-dimensional schedule.
6208 * Call compute_schedule_wcc_whole or compute_schedule_wcc_clustering
6209 * depending on whether the user has selected the option to try and
6210 * compute a schedule for the entire (weakly connected) component first.
6211 * If there is only a single strongly connected component (SCC), then
6212 * there is no point in trying to combine SCCs
6213 * in compute_schedule_wcc_clustering, so compute_schedule_wcc_whole
6214 * is called instead.
6216 static __isl_give isl_schedule_node *compute_schedule_wcc(
6217 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph)
6219 isl_ctx *ctx;
6221 if (!node)
6222 return NULL;
6224 ctx = isl_schedule_node_get_ctx(node);
6225 if (detect_sccs(ctx, graph) < 0)
6226 return isl_schedule_node_free(node);
6228 if (compute_maxvar(graph) < 0)
6229 return isl_schedule_node_free(node);
6231 if (need_feautrier_step(ctx, graph))
6232 return compute_schedule_wcc_feautrier(node, graph);
6234 if (graph->scc <= 1 || isl_options_get_schedule_whole_component(ctx))
6235 return compute_schedule_wcc_whole(node, graph);
6236 else
6237 return compute_schedule_wcc_clustering(node, graph);
6240 /* Compute a schedule for each group of nodes identified by node->scc
6241 * separately and then combine them in a sequence node (or as set node
6242 * if graph->weak is set) inserted at position "node" of the schedule tree.
6243 * Return the updated schedule node.
6245 * If "wcc" is set then each of the groups belongs to a single
6246 * weakly connected component in the dependence graph so that
6247 * there is no need for compute_sub_schedule to look for weakly
6248 * connected components.
6250 static __isl_give isl_schedule_node *compute_component_schedule(
6251 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph,
6252 int wcc)
6254 int component;
6255 isl_ctx *ctx;
6256 isl_union_set_list *filters;
6258 if (!node)
6259 return NULL;
6260 ctx = isl_schedule_node_get_ctx(node);
6262 filters = extract_sccs(ctx, graph);
6263 if (graph->weak)
6264 node = isl_schedule_node_insert_set(node, filters);
6265 else
6266 node = isl_schedule_node_insert_sequence(node, filters);
6268 for (component = 0; component < graph->scc; ++component) {
6269 node = isl_schedule_node_child(node, component);
6270 node = isl_schedule_node_child(node, 0);
6271 node = compute_sub_schedule(node, ctx, graph,
6272 &node_scc_exactly,
6273 &edge_scc_exactly, component, wcc);
6274 node = isl_schedule_node_parent(node);
6275 node = isl_schedule_node_parent(node);
6278 return node;
6281 /* Compute a schedule for the given dependence graph and insert it at "node".
6282 * Return the updated schedule node.
6284 * We first check if the graph is connected (through validity and conditional
6285 * validity dependences) and, if not, compute a schedule
6286 * for each component separately.
6287 * If the schedule_serialize_sccs option is set, then we check for strongly
6288 * connected components instead and compute a separate schedule for
6289 * each such strongly connected component.
6291 static __isl_give isl_schedule_node *compute_schedule(isl_schedule_node *node,
6292 struct isl_sched_graph *graph)
6294 isl_ctx *ctx;
6296 if (!node)
6297 return NULL;
6299 ctx = isl_schedule_node_get_ctx(node);
6300 if (isl_options_get_schedule_serialize_sccs(ctx)) {
6301 if (detect_sccs(ctx, graph) < 0)
6302 return isl_schedule_node_free(node);
6303 } else {
6304 if (detect_wccs(ctx, graph) < 0)
6305 return isl_schedule_node_free(node);
6308 if (graph->scc > 1)
6309 return compute_component_schedule(node, graph, 1);
6311 return compute_schedule_wcc(node, graph);
6314 /* Compute a schedule on sc->domain that respects the given schedule
6315 * constraints.
6317 * In particular, the schedule respects all the validity dependences.
6318 * If the default isl scheduling algorithm is used, it tries to minimize
6319 * the dependence distances over the proximity dependences.
6320 * If Feautrier's scheduling algorithm is used, the proximity dependence
6321 * distances are only minimized during the extension to a full-dimensional
6322 * schedule.
6324 * If there are any condition and conditional validity dependences,
6325 * then the conditional validity dependences may be violated inside
6326 * a tilable band, provided they have no adjacent non-local
6327 * condition dependences.
6329 __isl_give isl_schedule *isl_schedule_constraints_compute_schedule(
6330 __isl_take isl_schedule_constraints *sc)
6332 isl_ctx *ctx = isl_schedule_constraints_get_ctx(sc);
6333 struct isl_sched_graph graph = { 0 };
6334 isl_schedule *sched;
6335 isl_schedule_node *node;
6336 isl_union_set *domain;
6338 sc = isl_schedule_constraints_align_params(sc);
6340 domain = isl_schedule_constraints_get_domain(sc);
6341 if (isl_union_set_n_set(domain) == 0) {
6342 isl_schedule_constraints_free(sc);
6343 return isl_schedule_from_domain(domain);
6346 if (graph_init(&graph, sc) < 0)
6347 domain = isl_union_set_free(domain);
6349 node = isl_schedule_node_from_domain(domain);
6350 node = isl_schedule_node_child(node, 0);
6351 if (graph.n > 0)
6352 node = compute_schedule(node, &graph);
6353 sched = isl_schedule_node_get_schedule(node);
6354 isl_schedule_node_free(node);
6356 graph_free(ctx, &graph);
6357 isl_schedule_constraints_free(sc);
6359 return sched;
6362 /* Compute a schedule for the given union of domains that respects
6363 * all the validity dependences and minimizes
6364 * the dependence distances over the proximity dependences.
6366 * This function is kept for backward compatibility.
6368 __isl_give isl_schedule *isl_union_set_compute_schedule(
6369 __isl_take isl_union_set *domain,
6370 __isl_take isl_union_map *validity,
6371 __isl_take isl_union_map *proximity)
6373 isl_schedule_constraints *sc;
6375 sc = isl_schedule_constraints_on_domain(domain);
6376 sc = isl_schedule_constraints_set_validity(sc, validity);
6377 sc = isl_schedule_constraints_set_proximity(sc, proximity);
6379 return isl_schedule_constraints_compute_schedule(sc);