isl_scheduler.c: extract out shared intra_dim_map
[isl.git] / isl_scheduler.c
blob65dbc8b80b6559918f4eef9ad8f88f04481e094d
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 /* Construct an isl_dim_map for mapping constraints on coefficients
1755 * for "node" to the corresponding positions in graph->lp.
1756 * "offset" is the offset of the coefficients for the variables
1757 * in the input constraints.
1758 * "s" is the sign of the mapping.
1760 * The input constraints are given in terms of the coefficients (c_0, c_n, c_x).
1761 * The mapping produced by this function essentially plugs in
1762 * (0, 0, c_i_x^+ - c_i_x^-) if s = 1 and
1763 * (0, 0, -c_i_x^+ + c_i_x^-) if s = -1.
1764 * In graph->lp, the c_i_x^- appear before their c_i_x^+ counterpart.
1766 * The caller can extend the mapping to also map the other coefficients
1767 * (and therefore not plug in 0).
1769 static __isl_give isl_dim_map *intra_dim_map(isl_ctx *ctx,
1770 struct isl_sched_graph *graph, struct isl_sched_node *node,
1771 int offset, int s)
1773 int pos;
1774 unsigned total;
1775 isl_dim_map *dim_map;
1777 total = isl_basic_set_total_dim(graph->lp);
1778 pos = node->start + 1 + 2 * node->nparam;
1779 dim_map = isl_dim_map_alloc(ctx, total);
1780 isl_dim_map_range(dim_map, pos, 2, offset, 1, node->nvar, -s);
1781 isl_dim_map_range(dim_map, pos + 1, 2, offset, 1, node->nvar, s);
1783 return dim_map;
1786 /* Add constraints to graph->lp that force validity for the given
1787 * dependence from a node i to itself.
1788 * That is, add constraints that enforce
1790 * (c_i_0 + c_i_n n + c_i_x y) - (c_i_0 + c_i_n n + c_i_x x)
1791 * = c_i_x (y - x) >= 0
1793 * for each (x,y) in R.
1794 * We obtain general constraints on coefficients (c_0, c_n, c_x)
1795 * of valid constraints for (y - x) and then plug in (0, 0, c_i_x^+ - c_i_x^-),
1796 * where c_i_x = c_i_x^+ - c_i_x^-, with c_i_x^+ and c_i_x^- non-negative.
1797 * In graph->lp, the c_i_x^- appear before their c_i_x^+ counterpart.
1799 * Actually, we do not construct constraints for the c_i_x themselves,
1800 * but for the coefficients of c_i_x written as a linear combination
1801 * of the columns in node->cmap.
1803 static isl_stat add_intra_validity_constraints(struct isl_sched_graph *graph,
1804 struct isl_sched_edge *edge)
1806 int offset;
1807 isl_map *map = isl_map_copy(edge->map);
1808 isl_ctx *ctx = isl_map_get_ctx(map);
1809 isl_space *space;
1810 isl_dim_map *dim_map;
1811 isl_basic_set *coef;
1812 struct isl_sched_node *node = edge->src;
1814 coef = intra_coefficients(graph, node, map);
1816 space = isl_space_unwrap(isl_basic_set_get_space(coef));
1817 space = isl_space_domain(space);
1818 offset = isl_space_dim(space, isl_dim_set);
1819 isl_space_free(space);
1821 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
1822 offset, isl_mat_copy(node->cmap));
1823 if (!coef)
1824 return isl_stat_error;
1826 dim_map = intra_dim_map(ctx, graph, node, offset, 1);
1827 graph->lp = isl_basic_set_extend_constraints(graph->lp,
1828 coef->n_eq, coef->n_ineq);
1829 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
1830 coef, dim_map);
1832 return isl_stat_ok;
1835 /* Add constraints to graph->lp that force validity for the given
1836 * dependence from node i to node j.
1837 * That is, add constraints that enforce
1839 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x) >= 0
1841 * for each (x,y) in R.
1842 * We obtain general constraints on coefficients (c_0, c_n, c_x, c_y)
1843 * of valid constraints for R and then plug in
1844 * (c_j_0 - c_i_0, c_j_n^+ - c_j_n^- - (c_i_n^+ - c_i_n^-),
1845 * c_j_x^+ - c_j_x^- - (c_i_x^+ - c_i_x^-)),
1846 * where c_* = c_*^+ - c_*^-, with c_*^+ and c_*^- non-negative.
1847 * In graph->lp, the c_*^- appear before their c_*^+ counterpart.
1849 * Actually, we do not construct constraints for the c_*_x themselves,
1850 * but for the coefficients of c_*_x written as a linear combination
1851 * of the columns in node->cmap.
1853 static isl_stat add_inter_validity_constraints(struct isl_sched_graph *graph,
1854 struct isl_sched_edge *edge)
1856 unsigned total;
1857 isl_map *map = isl_map_copy(edge->map);
1858 isl_ctx *ctx = isl_map_get_ctx(map);
1859 isl_space *space;
1860 isl_dim_map *dim_map;
1861 isl_basic_set *coef;
1862 struct isl_sched_node *src = edge->src;
1863 struct isl_sched_node *dst = edge->dst;
1865 coef = inter_coefficients(graph, edge, map);
1867 space = isl_space_unwrap(isl_basic_set_get_space(coef));
1868 space = isl_space_domain(space);
1870 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
1871 isl_space_dim(space, isl_dim_set), isl_mat_copy(src->cmap));
1872 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
1873 isl_space_dim(space, isl_dim_set) + src->nvar,
1874 isl_mat_copy(dst->cmap));
1875 if (!coef)
1876 goto error;
1878 total = isl_basic_set_total_dim(graph->lp);
1879 dim_map = isl_dim_map_alloc(ctx, total);
1881 isl_dim_map_range(dim_map, dst->start, 0, 0, 0, 1, 1);
1882 isl_dim_map_range(dim_map, dst->start + 1, 2, 1, 1, dst->nparam, -1);
1883 isl_dim_map_range(dim_map, dst->start + 2, 2, 1, 1, dst->nparam, 1);
1884 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 1, 2,
1885 isl_space_dim(space, isl_dim_set) + src->nvar, 1,
1886 dst->nvar, -1);
1887 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 2, 2,
1888 isl_space_dim(space, isl_dim_set) + src->nvar, 1,
1889 dst->nvar, 1);
1891 isl_dim_map_range(dim_map, src->start, 0, 0, 0, 1, -1);
1892 isl_dim_map_range(dim_map, src->start + 1, 2, 1, 1, src->nparam, 1);
1893 isl_dim_map_range(dim_map, src->start + 2, 2, 1, 1, src->nparam, -1);
1894 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 1, 2,
1895 isl_space_dim(space, isl_dim_set), 1,
1896 src->nvar, 1);
1897 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 2, 2,
1898 isl_space_dim(space, isl_dim_set), 1,
1899 src->nvar, -1);
1901 edge->start = graph->lp->n_ineq;
1902 graph->lp = isl_basic_set_extend_constraints(graph->lp,
1903 coef->n_eq, coef->n_ineq);
1904 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
1905 coef, dim_map);
1906 if (!graph->lp)
1907 goto error;
1908 isl_space_free(space);
1909 edge->end = graph->lp->n_ineq;
1911 return isl_stat_ok;
1912 error:
1913 isl_space_free(space);
1914 return isl_stat_error;
1917 /* Add constraints to graph->lp that bound the dependence distance for the given
1918 * dependence from a node i to itself.
1919 * If s = 1, we add the constraint
1921 * c_i_x (y - x) <= m_0 + m_n n
1923 * or
1925 * -c_i_x (y - x) + m_0 + m_n n >= 0
1927 * for each (x,y) in R.
1928 * If s = -1, we add the constraint
1930 * -c_i_x (y - x) <= m_0 + m_n n
1932 * or
1934 * c_i_x (y - x) + m_0 + m_n n >= 0
1936 * for each (x,y) in R.
1937 * We obtain general constraints on coefficients (c_0, c_n, c_x)
1938 * of valid constraints for (y - x) and then plug in (m_0, m_n, -s * c_i_x),
1939 * with each coefficient (except m_0) represented as a pair of non-negative
1940 * coefficients.
1942 * Actually, we do not construct constraints for the c_i_x themselves,
1943 * but for the coefficients of c_i_x written as a linear combination
1944 * of the columns in node->cmap.
1947 * If "local" is set, then we add constraints
1949 * c_i_x (y - x) <= 0
1951 * or
1953 * -c_i_x (y - x) <= 0
1955 * instead, forcing the dependence distance to be (less than or) equal to 0.
1956 * That is, we plug in (0, 0, -s * c_i_x),
1957 * Note that dependences marked local are treated as validity constraints
1958 * by add_all_validity_constraints and therefore also have
1959 * their distances bounded by 0 from below.
1961 static isl_stat add_intra_proximity_constraints(struct isl_sched_graph *graph,
1962 struct isl_sched_edge *edge, int s, int local)
1964 int offset;
1965 unsigned nparam;
1966 isl_map *map = isl_map_copy(edge->map);
1967 isl_ctx *ctx = isl_map_get_ctx(map);
1968 isl_space *space;
1969 isl_dim_map *dim_map;
1970 isl_basic_set *coef;
1971 struct isl_sched_node *node = edge->src;
1973 coef = intra_coefficients(graph, node, map);
1975 space = isl_space_unwrap(isl_basic_set_get_space(coef));
1976 space = isl_space_domain(space);
1977 offset = isl_space_dim(space, isl_dim_set);
1978 isl_space_free(space);
1980 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
1981 offset, isl_mat_copy(node->cmap));
1982 if (!coef)
1983 return isl_stat_error;
1985 nparam = isl_space_dim(node->space, isl_dim_param);
1986 dim_map = intra_dim_map(ctx, graph, node, offset, -s);
1988 if (!local) {
1989 isl_dim_map_range(dim_map, 1, 0, 0, 0, 1, 1);
1990 isl_dim_map_range(dim_map, 4, 2, 1, 1, nparam, -1);
1991 isl_dim_map_range(dim_map, 5, 2, 1, 1, nparam, 1);
1993 graph->lp = isl_basic_set_extend_constraints(graph->lp,
1994 coef->n_eq, coef->n_ineq);
1995 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
1996 coef, dim_map);
1998 return isl_stat_ok;
2001 /* Add constraints to graph->lp that bound the dependence distance for the given
2002 * dependence from node i to node j.
2003 * If s = 1, we add the constraint
2005 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x)
2006 * <= m_0 + m_n n
2008 * or
2010 * -(c_j_0 + c_j_n n + c_j_x y) + (c_i_0 + c_i_n n + c_i_x x) +
2011 * m_0 + m_n n >= 0
2013 * for each (x,y) in R.
2014 * If s = -1, we add the constraint
2016 * -((c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x))
2017 * <= m_0 + m_n n
2019 * or
2021 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x) +
2022 * m_0 + m_n n >= 0
2024 * for each (x,y) in R.
2025 * We obtain general constraints on coefficients (c_0, c_n, c_x, c_y)
2026 * of valid constraints for R and then plug in
2027 * (m_0 - s*c_j_0 + s*c_i_0, m_n - s*c_j_n + s*c_i_n,
2028 * -s*c_j_x+s*c_i_x)
2029 * with each coefficient (except m_0, c_j_0 and c_i_0)
2030 * represented as a pair of non-negative coefficients.
2032 * Actually, we do not construct constraints for the c_*_x themselves,
2033 * but for the coefficients of c_*_x written as a linear combination
2034 * of the columns in node->cmap.
2037 * If "local" is set, then we add constraints
2039 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x) <= 0
2041 * or
2043 * -((c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x)) <= 0
2045 * instead, forcing the dependence distance to be (less than or) equal to 0.
2046 * That is, we plug in
2047 * (-s*c_j_0 + s*c_i_0, -s*c_j_n + s*c_i_n, -s*c_j_x+s*c_i_x).
2048 * Note that dependences marked local are treated as validity constraints
2049 * by add_all_validity_constraints and therefore also have
2050 * their distances bounded by 0 from below.
2052 static isl_stat add_inter_proximity_constraints(struct isl_sched_graph *graph,
2053 struct isl_sched_edge *edge, int s, int local)
2055 unsigned total;
2056 unsigned nparam;
2057 isl_map *map = isl_map_copy(edge->map);
2058 isl_ctx *ctx = isl_map_get_ctx(map);
2059 isl_space *space;
2060 isl_dim_map *dim_map;
2061 isl_basic_set *coef;
2062 struct isl_sched_node *src = edge->src;
2063 struct isl_sched_node *dst = edge->dst;
2065 coef = inter_coefficients(graph, edge, map);
2067 space = isl_space_unwrap(isl_basic_set_get_space(coef));
2068 space = isl_space_domain(space);
2070 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
2071 isl_space_dim(space, isl_dim_set), isl_mat_copy(src->cmap));
2072 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
2073 isl_space_dim(space, isl_dim_set) + src->nvar,
2074 isl_mat_copy(dst->cmap));
2075 if (!coef)
2076 goto error;
2078 nparam = isl_space_dim(src->space, isl_dim_param);
2079 total = isl_basic_set_total_dim(graph->lp);
2080 dim_map = isl_dim_map_alloc(ctx, total);
2082 if (!local) {
2083 isl_dim_map_range(dim_map, 1, 0, 0, 0, 1, 1);
2084 isl_dim_map_range(dim_map, 4, 2, 1, 1, nparam, -1);
2085 isl_dim_map_range(dim_map, 5, 2, 1, 1, nparam, 1);
2088 isl_dim_map_range(dim_map, dst->start, 0, 0, 0, 1, -s);
2089 isl_dim_map_range(dim_map, dst->start + 1, 2, 1, 1, dst->nparam, s);
2090 isl_dim_map_range(dim_map, dst->start + 2, 2, 1, 1, dst->nparam, -s);
2091 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 1, 2,
2092 isl_space_dim(space, isl_dim_set) + src->nvar, 1,
2093 dst->nvar, s);
2094 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 2, 2,
2095 isl_space_dim(space, isl_dim_set) + src->nvar, 1,
2096 dst->nvar, -s);
2098 isl_dim_map_range(dim_map, src->start, 0, 0, 0, 1, s);
2099 isl_dim_map_range(dim_map, src->start + 1, 2, 1, 1, src->nparam, -s);
2100 isl_dim_map_range(dim_map, src->start + 2, 2, 1, 1, src->nparam, s);
2101 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 1, 2,
2102 isl_space_dim(space, isl_dim_set), 1,
2103 src->nvar, -s);
2104 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 2, 2,
2105 isl_space_dim(space, isl_dim_set), 1,
2106 src->nvar, s);
2108 graph->lp = isl_basic_set_extend_constraints(graph->lp,
2109 coef->n_eq, coef->n_ineq);
2110 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
2111 coef, dim_map);
2112 isl_space_free(space);
2114 return isl_stat_ok;
2115 error:
2116 isl_space_free(space);
2117 return isl_stat_error;
2120 /* Add all validity constraints to graph->lp.
2122 * An edge that is forced to be local needs to have its dependence
2123 * distances equal to zero. We take care of bounding them by 0 from below
2124 * here. add_all_proximity_constraints takes care of bounding them by 0
2125 * from above.
2127 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
2128 * Otherwise, we ignore them.
2130 static int add_all_validity_constraints(struct isl_sched_graph *graph,
2131 int use_coincidence)
2133 int i;
2135 for (i = 0; i < graph->n_edge; ++i) {
2136 struct isl_sched_edge *edge= &graph->edge[i];
2137 int local;
2139 local = is_local(edge) ||
2140 (is_coincidence(edge) && use_coincidence);
2141 if (!is_validity(edge) && !local)
2142 continue;
2143 if (edge->src != edge->dst)
2144 continue;
2145 if (add_intra_validity_constraints(graph, edge) < 0)
2146 return -1;
2149 for (i = 0; i < graph->n_edge; ++i) {
2150 struct isl_sched_edge *edge = &graph->edge[i];
2151 int local;
2153 local = is_local(edge) ||
2154 (is_coincidence(edge) && use_coincidence);
2155 if (!is_validity(edge) && !local)
2156 continue;
2157 if (edge->src == edge->dst)
2158 continue;
2159 if (add_inter_validity_constraints(graph, edge) < 0)
2160 return -1;
2163 return 0;
2166 /* Add constraints to graph->lp that bound the dependence distance
2167 * for all dependence relations.
2168 * If a given proximity dependence is identical to a validity
2169 * dependence, then the dependence distance is already bounded
2170 * from below (by zero), so we only need to bound the distance
2171 * from above. (This includes the case of "local" dependences
2172 * which are treated as validity dependence by add_all_validity_constraints.)
2173 * Otherwise, we need to bound the distance both from above and from below.
2175 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
2176 * Otherwise, we ignore them.
2178 static int add_all_proximity_constraints(struct isl_sched_graph *graph,
2179 int use_coincidence)
2181 int i;
2183 for (i = 0; i < graph->n_edge; ++i) {
2184 struct isl_sched_edge *edge= &graph->edge[i];
2185 int local;
2187 local = is_local(edge) ||
2188 (is_coincidence(edge) && use_coincidence);
2189 if (!is_proximity(edge) && !local)
2190 continue;
2191 if (edge->src == edge->dst &&
2192 add_intra_proximity_constraints(graph, edge, 1, local) < 0)
2193 return -1;
2194 if (edge->src != edge->dst &&
2195 add_inter_proximity_constraints(graph, edge, 1, local) < 0)
2196 return -1;
2197 if (is_validity(edge) || local)
2198 continue;
2199 if (edge->src == edge->dst &&
2200 add_intra_proximity_constraints(graph, edge, -1, 0) < 0)
2201 return -1;
2202 if (edge->src != edge->dst &&
2203 add_inter_proximity_constraints(graph, edge, -1, 0) < 0)
2204 return -1;
2207 return 0;
2210 /* Compute a basis for the rows in the linear part of the schedule
2211 * and extend this basis to a full basis. The remaining rows
2212 * can then be used to force linear independence from the rows
2213 * in the schedule.
2215 * In particular, given the schedule rows S, we compute
2217 * S = H Q
2218 * S U = H
2220 * with H the Hermite normal form of S. That is, all but the
2221 * first rank columns of H are zero and so each row in S is
2222 * a linear combination of the first rank rows of Q.
2223 * The matrix Q is then transposed because we will write the
2224 * coefficients of the next schedule row as a column vector s
2225 * and express this s as a linear combination s = Q c of the
2226 * computed basis.
2227 * Similarly, the matrix U is transposed such that we can
2228 * compute the coefficients c = U s from a schedule row s.
2230 static int node_update_cmap(struct isl_sched_node *node)
2232 isl_mat *H, *U, *Q;
2233 int n_row = isl_mat_rows(node->sched);
2235 H = isl_mat_sub_alloc(node->sched, 0, n_row,
2236 1 + node->nparam, node->nvar);
2238 H = isl_mat_left_hermite(H, 0, &U, &Q);
2239 isl_mat_free(node->cmap);
2240 isl_mat_free(node->cinv);
2241 isl_mat_free(node->ctrans);
2242 node->ctrans = isl_mat_copy(Q);
2243 node->cmap = isl_mat_transpose(Q);
2244 node->cinv = isl_mat_transpose(U);
2245 node->rank = isl_mat_initial_non_zero_cols(H);
2246 isl_mat_free(H);
2248 if (!node->cmap || !node->cinv || !node->ctrans || node->rank < 0)
2249 return -1;
2250 return 0;
2253 /* Is "edge" marked as a validity or a conditional validity edge?
2255 static int is_any_validity(struct isl_sched_edge *edge)
2257 return is_validity(edge) || is_conditional_validity(edge);
2260 /* How many times should we count the constraints in "edge"?
2262 * If carry is set, then we are counting the number of
2263 * (validity or conditional validity) constraints that will be added
2264 * in setup_carry_lp and we count each edge exactly once.
2266 * Otherwise, we count as follows
2267 * validity -> 1 (>= 0)
2268 * validity+proximity -> 2 (>= 0 and upper bound)
2269 * proximity -> 2 (lower and upper bound)
2270 * local(+any) -> 2 (>= 0 and <= 0)
2272 * If an edge is only marked conditional_validity then it counts
2273 * as zero since it is only checked afterwards.
2275 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
2276 * Otherwise, we ignore them.
2278 static int edge_multiplicity(struct isl_sched_edge *edge, int carry,
2279 int use_coincidence)
2281 if (carry)
2282 return 1;
2283 if (is_proximity(edge) || is_local(edge))
2284 return 2;
2285 if (use_coincidence && is_coincidence(edge))
2286 return 2;
2287 if (is_validity(edge))
2288 return 1;
2289 return 0;
2292 /* Count the number of equality and inequality constraints
2293 * that will be added for the given map.
2295 * "use_coincidence" is set if we should take into account coincidence edges.
2297 static int count_map_constraints(struct isl_sched_graph *graph,
2298 struct isl_sched_edge *edge, __isl_take isl_map *map,
2299 int *n_eq, int *n_ineq, int carry, int use_coincidence)
2301 isl_basic_set *coef;
2302 int f = edge_multiplicity(edge, carry, use_coincidence);
2304 if (f == 0) {
2305 isl_map_free(map);
2306 return 0;
2309 if (edge->src == edge->dst)
2310 coef = intra_coefficients(graph, edge->src, map);
2311 else
2312 coef = inter_coefficients(graph, edge, map);
2313 if (!coef)
2314 return -1;
2315 *n_eq += f * coef->n_eq;
2316 *n_ineq += f * coef->n_ineq;
2317 isl_basic_set_free(coef);
2319 return 0;
2322 /* Count the number of equality and inequality constraints
2323 * that will be added to the main lp problem.
2324 * We count as follows
2325 * validity -> 1 (>= 0)
2326 * validity+proximity -> 2 (>= 0 and upper bound)
2327 * proximity -> 2 (lower and upper bound)
2328 * local(+any) -> 2 (>= 0 and <= 0)
2330 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
2331 * Otherwise, we ignore them.
2333 static int count_constraints(struct isl_sched_graph *graph,
2334 int *n_eq, int *n_ineq, int use_coincidence)
2336 int i;
2338 *n_eq = *n_ineq = 0;
2339 for (i = 0; i < graph->n_edge; ++i) {
2340 struct isl_sched_edge *edge= &graph->edge[i];
2341 isl_map *map = isl_map_copy(edge->map);
2343 if (count_map_constraints(graph, edge, map, n_eq, n_ineq,
2344 0, use_coincidence) < 0)
2345 return -1;
2348 return 0;
2351 /* Count the number of constraints that will be added by
2352 * add_bound_constant_constraints to bound the values of the constant terms
2353 * and increment *n_eq and *n_ineq accordingly.
2355 * In practice, add_bound_constant_constraints only adds inequalities.
2357 static isl_stat count_bound_constant_constraints(isl_ctx *ctx,
2358 struct isl_sched_graph *graph, int *n_eq, int *n_ineq)
2360 if (isl_options_get_schedule_max_constant_term(ctx) == -1)
2361 return isl_stat_ok;
2363 *n_ineq += graph->n;
2365 return isl_stat_ok;
2368 /* Add constraints to bound the values of the constant terms in the schedule,
2369 * if requested by the user.
2371 * The maximal value of the constant terms is defined by the option
2372 * "schedule_max_constant_term".
2374 * Within each node, the coefficients have the following order:
2375 * - c_i_0
2376 * - positive and negative parts of c_i_n (if parametric)
2377 * - positive and negative parts of c_i_x
2379 static isl_stat add_bound_constant_constraints(isl_ctx *ctx,
2380 struct isl_sched_graph *graph)
2382 int i, k;
2383 int max;
2384 int total;
2386 max = isl_options_get_schedule_max_constant_term(ctx);
2387 if (max == -1)
2388 return isl_stat_ok;
2390 total = isl_basic_set_dim(graph->lp, isl_dim_set);
2392 for (i = 0; i < graph->n; ++i) {
2393 struct isl_sched_node *node = &graph->node[i];
2394 k = isl_basic_set_alloc_inequality(graph->lp);
2395 if (k < 0)
2396 return isl_stat_error;
2397 isl_seq_clr(graph->lp->ineq[k], 1 + total);
2398 isl_int_set_si(graph->lp->ineq[k][1 + node->start], -1);
2399 isl_int_set_si(graph->lp->ineq[k][0], max);
2402 return isl_stat_ok;
2405 /* Count the number of constraints that will be added by
2406 * add_bound_coefficient_constraints and increment *n_eq and *n_ineq
2407 * accordingly.
2409 * In practice, add_bound_coefficient_constraints only adds inequalities.
2411 static int count_bound_coefficient_constraints(isl_ctx *ctx,
2412 struct isl_sched_graph *graph, int *n_eq, int *n_ineq)
2414 int i;
2416 if (ctx->opt->schedule_max_coefficient == -1)
2417 return 0;
2419 for (i = 0; i < graph->n; ++i)
2420 *n_ineq += 2 * graph->node[i].nparam + 2 * graph->node[i].nvar;
2422 return 0;
2425 /* Add constraints that bound the values of the variable and parameter
2426 * coefficients of the schedule.
2428 * The maximal value of the coefficients is defined by the option
2429 * 'schedule_max_coefficient'.
2431 static int add_bound_coefficient_constraints(isl_ctx *ctx,
2432 struct isl_sched_graph *graph)
2434 int i, j, k;
2435 int max_coefficient;
2436 int total;
2438 max_coefficient = ctx->opt->schedule_max_coefficient;
2440 if (max_coefficient == -1)
2441 return 0;
2443 total = isl_basic_set_total_dim(graph->lp);
2445 for (i = 0; i < graph->n; ++i) {
2446 struct isl_sched_node *node = &graph->node[i];
2447 for (j = 0; j < 2 * node->nparam + 2 * node->nvar; ++j) {
2448 int dim;
2449 k = isl_basic_set_alloc_inequality(graph->lp);
2450 if (k < 0)
2451 return -1;
2452 dim = 1 + node->start + 1 + j;
2453 isl_seq_clr(graph->lp->ineq[k], 1 + total);
2454 isl_int_set_si(graph->lp->ineq[k][dim], -1);
2455 isl_int_set_si(graph->lp->ineq[k][0], max_coefficient);
2459 return 0;
2462 /* Add a constraint to graph->lp that equates the value at position
2463 * "sum_pos" to the sum of the "n" values starting at "first".
2465 static isl_stat add_sum_constraint(struct isl_sched_graph *graph,
2466 int sum_pos, int first, int n)
2468 int i, k;
2469 int total;
2471 total = isl_basic_set_dim(graph->lp, isl_dim_set);
2473 k = isl_basic_set_alloc_equality(graph->lp);
2474 if (k < 0)
2475 return isl_stat_error;
2476 isl_seq_clr(graph->lp->eq[k], 1 + total);
2477 isl_int_set_si(graph->lp->eq[k][1 + sum_pos], -1);
2478 for (i = 0; i < n; ++i)
2479 isl_int_set_si(graph->lp->eq[k][1 + first + i], 1);
2481 return isl_stat_ok;
2484 /* Add a constraint to graph->lp that equates the value at position
2485 * "sum_pos" to the sum of the parameter coefficients of all nodes.
2487 * Within each node, the coefficients have the following order:
2488 * - c_i_0
2489 * - positive and negative parts of c_i_n (if parametric)
2490 * - positive and negative parts of c_i_x
2492 static isl_stat add_param_sum_constraint(struct isl_sched_graph *graph,
2493 int sum_pos)
2495 int i, j, k;
2496 int total;
2498 total = isl_basic_set_dim(graph->lp, isl_dim_set);
2500 k = isl_basic_set_alloc_equality(graph->lp);
2501 if (k < 0)
2502 return isl_stat_error;
2503 isl_seq_clr(graph->lp->eq[k], 1 + total);
2504 isl_int_set_si(graph->lp->eq[k][1 + sum_pos], -1);
2505 for (i = 0; i < graph->n; ++i) {
2506 int pos = 1 + graph->node[i].start + 1;
2508 for (j = 0; j < 2 * graph->node[i].nparam; ++j)
2509 isl_int_set_si(graph->lp->eq[k][pos + j], 1);
2512 return isl_stat_ok;
2515 /* Add a constraint to graph->lp that equates the value at position
2516 * "sum_pos" to the sum of the variable coefficients of all nodes.
2518 * Within each node, the coefficients have the following order:
2519 * - c_i_0
2520 * - positive and negative parts of c_i_n (if parametric)
2521 * - positive and negative parts of c_i_x
2523 static isl_stat add_var_sum_constraint(struct isl_sched_graph *graph,
2524 int sum_pos)
2526 int i, j, k;
2527 int total;
2529 total = isl_basic_set_dim(graph->lp, isl_dim_set);
2531 k = isl_basic_set_alloc_equality(graph->lp);
2532 if (k < 0)
2533 return isl_stat_error;
2534 isl_seq_clr(graph->lp->eq[k], 1 + total);
2535 isl_int_set_si(graph->lp->eq[k][1 + sum_pos], -1);
2536 for (i = 0; i < graph->n; ++i) {
2537 struct isl_sched_node *node = &graph->node[i];
2538 int pos = 1 + node->start + 1 + 2 * node->nparam;
2540 for (j = 0; j < 2 * node->nvar; ++j)
2541 isl_int_set_si(graph->lp->eq[k][pos + j], 1);
2544 return isl_stat_ok;
2547 /* Construct an ILP problem for finding schedule coefficients
2548 * that result in non-negative, but small dependence distances
2549 * over all dependences.
2550 * In particular, the dependence distances over proximity edges
2551 * are bounded by m_0 + m_n n and we compute schedule coefficients
2552 * with small values (preferably zero) of m_n and m_0.
2554 * All variables of the ILP are non-negative. The actual coefficients
2555 * may be negative, so each coefficient is represented as the difference
2556 * of two non-negative variables. The negative part always appears
2557 * immediately before the positive part.
2558 * Other than that, the variables have the following order
2560 * - sum of positive and negative parts of m_n coefficients
2561 * - m_0
2562 * - sum of positive and negative parts of all c_n coefficients
2563 * (unconstrained when computing non-parametric schedules)
2564 * - sum of positive and negative parts of all c_x coefficients
2565 * - positive and negative parts of m_n coefficients
2566 * - for each node
2567 * - c_i_0
2568 * - positive and negative parts of c_i_n (if parametric)
2569 * - positive and negative parts of c_i_x
2571 * The c_i_x are not represented directly, but through the columns of
2572 * node->cmap. That is, the computed values are for variable t_i_x
2573 * such that c_i_x = Q t_i_x with Q equal to node->cmap.
2575 * The constraints are those from the edges plus two or three equalities
2576 * to express the sums.
2578 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
2579 * Otherwise, we ignore them.
2581 static isl_stat setup_lp(isl_ctx *ctx, struct isl_sched_graph *graph,
2582 int use_coincidence)
2584 int i;
2585 unsigned nparam;
2586 unsigned total;
2587 isl_space *space;
2588 int parametric;
2589 int param_pos;
2590 int n_eq, n_ineq;
2592 parametric = ctx->opt->schedule_parametric;
2593 nparam = isl_space_dim(graph->node[0].space, isl_dim_param);
2594 param_pos = 4;
2595 total = param_pos + 2 * nparam;
2596 for (i = 0; i < graph->n; ++i) {
2597 struct isl_sched_node *node = &graph->node[graph->sorted[i]];
2598 if (node_update_cmap(node) < 0)
2599 return isl_stat_error;
2600 node->start = total;
2601 total += 1 + 2 * (node->nparam + node->nvar);
2604 if (count_constraints(graph, &n_eq, &n_ineq, use_coincidence) < 0)
2605 return isl_stat_error;
2606 if (count_bound_constant_constraints(ctx, graph, &n_eq, &n_ineq) < 0)
2607 return isl_stat_error;
2608 if (count_bound_coefficient_constraints(ctx, graph, &n_eq, &n_ineq) < 0)
2609 return isl_stat_error;
2611 space = isl_space_set_alloc(ctx, 0, total);
2612 isl_basic_set_free(graph->lp);
2613 n_eq += 2 + parametric;
2615 graph->lp = isl_basic_set_alloc_space(space, 0, n_eq, n_ineq);
2617 if (add_sum_constraint(graph, 0, param_pos, 2 * nparam) < 0)
2618 return isl_stat_error;
2619 if (parametric && add_param_sum_constraint(graph, 2) < 0)
2620 return isl_stat_error;
2621 if (add_var_sum_constraint(graph, 3) < 0)
2622 return isl_stat_error;
2623 if (add_bound_constant_constraints(ctx, graph) < 0)
2624 return isl_stat_error;
2625 if (add_bound_coefficient_constraints(ctx, graph) < 0)
2626 return isl_stat_error;
2627 if (add_all_validity_constraints(graph, use_coincidence) < 0)
2628 return isl_stat_error;
2629 if (add_all_proximity_constraints(graph, use_coincidence) < 0)
2630 return isl_stat_error;
2632 return isl_stat_ok;
2635 /* Analyze the conflicting constraint found by
2636 * isl_tab_basic_set_non_trivial_lexmin. If it corresponds to the validity
2637 * constraint of one of the edges between distinct nodes, living, moreover
2638 * in distinct SCCs, then record the source and sink SCC as this may
2639 * be a good place to cut between SCCs.
2641 static int check_conflict(int con, void *user)
2643 int i;
2644 struct isl_sched_graph *graph = user;
2646 if (graph->src_scc >= 0)
2647 return 0;
2649 con -= graph->lp->n_eq;
2651 if (con >= graph->lp->n_ineq)
2652 return 0;
2654 for (i = 0; i < graph->n_edge; ++i) {
2655 if (!is_validity(&graph->edge[i]))
2656 continue;
2657 if (graph->edge[i].src == graph->edge[i].dst)
2658 continue;
2659 if (graph->edge[i].src->scc == graph->edge[i].dst->scc)
2660 continue;
2661 if (graph->edge[i].start > con)
2662 continue;
2663 if (graph->edge[i].end <= con)
2664 continue;
2665 graph->src_scc = graph->edge[i].src->scc;
2666 graph->dst_scc = graph->edge[i].dst->scc;
2669 return 0;
2672 /* Check whether the next schedule row of the given node needs to be
2673 * non-trivial. Lower-dimensional domains may have some trivial rows,
2674 * but as soon as the number of remaining required non-trivial rows
2675 * is as large as the number or remaining rows to be computed,
2676 * all remaining rows need to be non-trivial.
2678 static int needs_row(struct isl_sched_graph *graph, struct isl_sched_node *node)
2680 return node->nvar - node->rank >= graph->maxvar - graph->n_row;
2683 /* Solve the ILP problem constructed in setup_lp.
2684 * For each node such that all the remaining rows of its schedule
2685 * need to be non-trivial, we construct a non-triviality region.
2686 * This region imposes that the next row is independent of previous rows.
2687 * In particular the coefficients c_i_x are represented by t_i_x
2688 * variables with c_i_x = Q t_i_x and Q a unimodular matrix such that
2689 * its first columns span the rows of the previously computed part
2690 * of the schedule. The non-triviality region enforces that at least
2691 * one of the remaining components of t_i_x is non-zero, i.e.,
2692 * that the new schedule row depends on at least one of the remaining
2693 * columns of Q.
2695 static __isl_give isl_vec *solve_lp(struct isl_sched_graph *graph)
2697 int i;
2698 isl_vec *sol;
2699 isl_basic_set *lp;
2701 for (i = 0; i < graph->n; ++i) {
2702 struct isl_sched_node *node = &graph->node[i];
2703 int skip = node->rank;
2704 graph->region[i].pos = node->start + 1 + 2*(node->nparam+skip);
2705 if (needs_row(graph, node))
2706 graph->region[i].len = 2 * (node->nvar - skip);
2707 else
2708 graph->region[i].len = 0;
2710 lp = isl_basic_set_copy(graph->lp);
2711 sol = isl_tab_basic_set_non_trivial_lexmin(lp, 2, graph->n,
2712 graph->region, &check_conflict, graph);
2713 return sol;
2716 /* Update the schedules of all nodes based on the given solution
2717 * of the LP problem.
2718 * The new row is added to the current band.
2719 * All possibly negative coefficients are encoded as a difference
2720 * of two non-negative variables, so we need to perform the subtraction
2721 * here. Moreover, if use_cmap is set, then the solution does
2722 * not refer to the actual coefficients c_i_x, but instead to variables
2723 * t_i_x such that c_i_x = Q t_i_x and Q is equal to node->cmap.
2724 * In this case, we then also need to perform this multiplication
2725 * to obtain the values of c_i_x.
2727 * If coincident is set, then the caller guarantees that the new
2728 * row satisfies the coincidence constraints.
2730 static int update_schedule(struct isl_sched_graph *graph,
2731 __isl_take isl_vec *sol, int use_cmap, int coincident)
2733 int i, j;
2734 isl_vec *csol = NULL;
2736 if (!sol)
2737 goto error;
2738 if (sol->size == 0)
2739 isl_die(sol->ctx, isl_error_internal,
2740 "no solution found", goto error);
2741 if (graph->n_total_row >= graph->max_row)
2742 isl_die(sol->ctx, isl_error_internal,
2743 "too many schedule rows", goto error);
2745 for (i = 0; i < graph->n; ++i) {
2746 struct isl_sched_node *node = &graph->node[i];
2747 int pos = node->start;
2748 int row = isl_mat_rows(node->sched);
2750 isl_vec_free(csol);
2751 csol = isl_vec_alloc(sol->ctx, node->nvar);
2752 if (!csol)
2753 goto error;
2755 isl_map_free(node->sched_map);
2756 node->sched_map = NULL;
2757 node->sched = isl_mat_add_rows(node->sched, 1);
2758 if (!node->sched)
2759 goto error;
2760 node->sched = isl_mat_set_element(node->sched, row, 0,
2761 sol->el[1 + pos]);
2762 for (j = 0; j < node->nparam + node->nvar; ++j)
2763 isl_int_sub(sol->el[1 + pos + 1 + 2 * j + 1],
2764 sol->el[1 + pos + 1 + 2 * j + 1],
2765 sol->el[1 + pos + 1 + 2 * j]);
2766 for (j = 0; j < node->nparam; ++j)
2767 node->sched = isl_mat_set_element(node->sched,
2768 row, 1 + j, sol->el[1+pos+1+2*j+1]);
2769 for (j = 0; j < node->nvar; ++j)
2770 isl_int_set(csol->el[j],
2771 sol->el[1+pos+1+2*(node->nparam+j)+1]);
2772 if (use_cmap)
2773 csol = isl_mat_vec_product(isl_mat_copy(node->cmap),
2774 csol);
2775 if (!csol)
2776 goto error;
2777 for (j = 0; j < node->nvar; ++j)
2778 node->sched = isl_mat_set_element(node->sched,
2779 row, 1 + node->nparam + j, csol->el[j]);
2780 node->coincident[graph->n_total_row] = coincident;
2782 isl_vec_free(sol);
2783 isl_vec_free(csol);
2785 graph->n_row++;
2786 graph->n_total_row++;
2788 return 0;
2789 error:
2790 isl_vec_free(sol);
2791 isl_vec_free(csol);
2792 return -1;
2795 /* Convert row "row" of node->sched into an isl_aff living in "ls"
2796 * and return this isl_aff.
2798 static __isl_give isl_aff *extract_schedule_row(__isl_take isl_local_space *ls,
2799 struct isl_sched_node *node, int row)
2801 int j;
2802 isl_int v;
2803 isl_aff *aff;
2805 isl_int_init(v);
2807 aff = isl_aff_zero_on_domain(ls);
2808 isl_mat_get_element(node->sched, row, 0, &v);
2809 aff = isl_aff_set_constant(aff, v);
2810 for (j = 0; j < node->nparam; ++j) {
2811 isl_mat_get_element(node->sched, row, 1 + j, &v);
2812 aff = isl_aff_set_coefficient(aff, isl_dim_param, j, v);
2814 for (j = 0; j < node->nvar; ++j) {
2815 isl_mat_get_element(node->sched, row, 1 + node->nparam + j, &v);
2816 aff = isl_aff_set_coefficient(aff, isl_dim_in, j, v);
2819 isl_int_clear(v);
2821 return aff;
2824 /* Convert the "n" rows starting at "first" of node->sched into a multi_aff
2825 * and return this multi_aff.
2827 * The result is defined over the uncompressed node domain.
2829 static __isl_give isl_multi_aff *node_extract_partial_schedule_multi_aff(
2830 struct isl_sched_node *node, int first, int n)
2832 int i;
2833 isl_space *space;
2834 isl_local_space *ls;
2835 isl_aff *aff;
2836 isl_multi_aff *ma;
2837 int nrow;
2839 if (!node)
2840 return NULL;
2841 nrow = isl_mat_rows(node->sched);
2842 if (node->compressed)
2843 space = isl_multi_aff_get_domain_space(node->decompress);
2844 else
2845 space = isl_space_copy(node->space);
2846 ls = isl_local_space_from_space(isl_space_copy(space));
2847 space = isl_space_from_domain(space);
2848 space = isl_space_add_dims(space, isl_dim_out, n);
2849 ma = isl_multi_aff_zero(space);
2851 for (i = first; i < first + n; ++i) {
2852 aff = extract_schedule_row(isl_local_space_copy(ls), node, i);
2853 ma = isl_multi_aff_set_aff(ma, i - first, aff);
2856 isl_local_space_free(ls);
2858 if (node->compressed)
2859 ma = isl_multi_aff_pullback_multi_aff(ma,
2860 isl_multi_aff_copy(node->compress));
2862 return ma;
2865 /* Convert node->sched into a multi_aff and return this multi_aff.
2867 * The result is defined over the uncompressed node domain.
2869 static __isl_give isl_multi_aff *node_extract_schedule_multi_aff(
2870 struct isl_sched_node *node)
2872 int nrow;
2874 nrow = isl_mat_rows(node->sched);
2875 return node_extract_partial_schedule_multi_aff(node, 0, nrow);
2878 /* Convert node->sched into a map and return this map.
2880 * The result is cached in node->sched_map, which needs to be released
2881 * whenever node->sched is updated.
2882 * It is defined over the uncompressed node domain.
2884 static __isl_give isl_map *node_extract_schedule(struct isl_sched_node *node)
2886 if (!node->sched_map) {
2887 isl_multi_aff *ma;
2889 ma = node_extract_schedule_multi_aff(node);
2890 node->sched_map = isl_map_from_multi_aff(ma);
2893 return isl_map_copy(node->sched_map);
2896 /* Construct a map that can be used to update a dependence relation
2897 * based on the current schedule.
2898 * That is, construct a map expressing that source and sink
2899 * are executed within the same iteration of the current schedule.
2900 * This map can then be intersected with the dependence relation.
2901 * This is not the most efficient way, but this shouldn't be a critical
2902 * operation.
2904 static __isl_give isl_map *specializer(struct isl_sched_node *src,
2905 struct isl_sched_node *dst)
2907 isl_map *src_sched, *dst_sched;
2909 src_sched = node_extract_schedule(src);
2910 dst_sched = node_extract_schedule(dst);
2911 return isl_map_apply_range(src_sched, isl_map_reverse(dst_sched));
2914 /* Intersect the domains of the nested relations in domain and range
2915 * of "umap" with "map".
2917 static __isl_give isl_union_map *intersect_domains(
2918 __isl_take isl_union_map *umap, __isl_keep isl_map *map)
2920 isl_union_set *uset;
2922 umap = isl_union_map_zip(umap);
2923 uset = isl_union_set_from_set(isl_map_wrap(isl_map_copy(map)));
2924 umap = isl_union_map_intersect_domain(umap, uset);
2925 umap = isl_union_map_zip(umap);
2926 return umap;
2929 /* Update the dependence relation of the given edge based
2930 * on the current schedule.
2931 * If the dependence is carried completely by the current schedule, then
2932 * it is removed from the edge_tables. It is kept in the list of edges
2933 * as otherwise all edge_tables would have to be recomputed.
2935 static int update_edge(struct isl_sched_graph *graph,
2936 struct isl_sched_edge *edge)
2938 int empty;
2939 isl_map *id;
2941 id = specializer(edge->src, edge->dst);
2942 edge->map = isl_map_intersect(edge->map, isl_map_copy(id));
2943 if (!edge->map)
2944 goto error;
2946 if (edge->tagged_condition) {
2947 edge->tagged_condition =
2948 intersect_domains(edge->tagged_condition, id);
2949 if (!edge->tagged_condition)
2950 goto error;
2952 if (edge->tagged_validity) {
2953 edge->tagged_validity =
2954 intersect_domains(edge->tagged_validity, id);
2955 if (!edge->tagged_validity)
2956 goto error;
2959 empty = isl_map_plain_is_empty(edge->map);
2960 if (empty < 0)
2961 goto error;
2962 if (empty)
2963 graph_remove_edge(graph, edge);
2965 isl_map_free(id);
2966 return 0;
2967 error:
2968 isl_map_free(id);
2969 return -1;
2972 /* Does the domain of "umap" intersect "uset"?
2974 static int domain_intersects(__isl_keep isl_union_map *umap,
2975 __isl_keep isl_union_set *uset)
2977 int empty;
2979 umap = isl_union_map_copy(umap);
2980 umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(uset));
2981 empty = isl_union_map_is_empty(umap);
2982 isl_union_map_free(umap);
2984 return empty < 0 ? -1 : !empty;
2987 /* Does the range of "umap" intersect "uset"?
2989 static int range_intersects(__isl_keep isl_union_map *umap,
2990 __isl_keep isl_union_set *uset)
2992 int empty;
2994 umap = isl_union_map_copy(umap);
2995 umap = isl_union_map_intersect_range(umap, isl_union_set_copy(uset));
2996 empty = isl_union_map_is_empty(umap);
2997 isl_union_map_free(umap);
2999 return empty < 0 ? -1 : !empty;
3002 /* Are the condition dependences of "edge" local with respect to
3003 * the current schedule?
3005 * That is, are domain and range of the condition dependences mapped
3006 * to the same point?
3008 * In other words, is the condition false?
3010 static int is_condition_false(struct isl_sched_edge *edge)
3012 isl_union_map *umap;
3013 isl_map *map, *sched, *test;
3014 int empty, local;
3016 empty = isl_union_map_is_empty(edge->tagged_condition);
3017 if (empty < 0 || empty)
3018 return empty;
3020 umap = isl_union_map_copy(edge->tagged_condition);
3021 umap = isl_union_map_zip(umap);
3022 umap = isl_union_set_unwrap(isl_union_map_domain(umap));
3023 map = isl_map_from_union_map(umap);
3025 sched = node_extract_schedule(edge->src);
3026 map = isl_map_apply_domain(map, sched);
3027 sched = node_extract_schedule(edge->dst);
3028 map = isl_map_apply_range(map, sched);
3030 test = isl_map_identity(isl_map_get_space(map));
3031 local = isl_map_is_subset(map, test);
3032 isl_map_free(map);
3033 isl_map_free(test);
3035 return local;
3038 /* For each conditional validity constraint that is adjacent
3039 * to a condition with domain in condition_source or range in condition_sink,
3040 * turn it into an unconditional validity constraint.
3042 static int unconditionalize_adjacent_validity(struct isl_sched_graph *graph,
3043 __isl_take isl_union_set *condition_source,
3044 __isl_take isl_union_set *condition_sink)
3046 int i;
3048 condition_source = isl_union_set_coalesce(condition_source);
3049 condition_sink = isl_union_set_coalesce(condition_sink);
3051 for (i = 0; i < graph->n_edge; ++i) {
3052 int adjacent;
3053 isl_union_map *validity;
3055 if (!is_conditional_validity(&graph->edge[i]))
3056 continue;
3057 if (is_validity(&graph->edge[i]))
3058 continue;
3060 validity = graph->edge[i].tagged_validity;
3061 adjacent = domain_intersects(validity, condition_sink);
3062 if (adjacent >= 0 && !adjacent)
3063 adjacent = range_intersects(validity, condition_source);
3064 if (adjacent < 0)
3065 goto error;
3066 if (!adjacent)
3067 continue;
3069 set_validity(&graph->edge[i]);
3072 isl_union_set_free(condition_source);
3073 isl_union_set_free(condition_sink);
3074 return 0;
3075 error:
3076 isl_union_set_free(condition_source);
3077 isl_union_set_free(condition_sink);
3078 return -1;
3081 /* Update the dependence relations of all edges based on the current schedule
3082 * and enforce conditional validity constraints that are adjacent
3083 * to satisfied condition constraints.
3085 * First check if any of the condition constraints are satisfied
3086 * (i.e., not local to the outer schedule) and keep track of
3087 * their domain and range.
3088 * Then update all dependence relations (which removes the non-local
3089 * constraints).
3090 * Finally, if any condition constraints turned out to be satisfied,
3091 * then turn all adjacent conditional validity constraints into
3092 * unconditional validity constraints.
3094 static int update_edges(isl_ctx *ctx, struct isl_sched_graph *graph)
3096 int i;
3097 int any = 0;
3098 isl_union_set *source, *sink;
3100 source = isl_union_set_empty(isl_space_params_alloc(ctx, 0));
3101 sink = isl_union_set_empty(isl_space_params_alloc(ctx, 0));
3102 for (i = 0; i < graph->n_edge; ++i) {
3103 int local;
3104 isl_union_set *uset;
3105 isl_union_map *umap;
3107 if (!is_condition(&graph->edge[i]))
3108 continue;
3109 if (is_local(&graph->edge[i]))
3110 continue;
3111 local = is_condition_false(&graph->edge[i]);
3112 if (local < 0)
3113 goto error;
3114 if (local)
3115 continue;
3117 any = 1;
3119 umap = isl_union_map_copy(graph->edge[i].tagged_condition);
3120 uset = isl_union_map_domain(umap);
3121 source = isl_union_set_union(source, uset);
3123 umap = isl_union_map_copy(graph->edge[i].tagged_condition);
3124 uset = isl_union_map_range(umap);
3125 sink = isl_union_set_union(sink, uset);
3128 for (i = graph->n_edge - 1; i >= 0; --i) {
3129 if (update_edge(graph, &graph->edge[i]) < 0)
3130 goto error;
3133 if (any)
3134 return unconditionalize_adjacent_validity(graph, source, sink);
3136 isl_union_set_free(source);
3137 isl_union_set_free(sink);
3138 return 0;
3139 error:
3140 isl_union_set_free(source);
3141 isl_union_set_free(sink);
3142 return -1;
3145 static void next_band(struct isl_sched_graph *graph)
3147 graph->band_start = graph->n_total_row;
3150 /* Return the union of the universe domains of the nodes in "graph"
3151 * that satisfy "pred".
3153 static __isl_give isl_union_set *isl_sched_graph_domain(isl_ctx *ctx,
3154 struct isl_sched_graph *graph,
3155 int (*pred)(struct isl_sched_node *node, int data), int data)
3157 int i;
3158 isl_set *set;
3159 isl_union_set *dom;
3161 for (i = 0; i < graph->n; ++i)
3162 if (pred(&graph->node[i], data))
3163 break;
3165 if (i >= graph->n)
3166 isl_die(ctx, isl_error_internal,
3167 "empty component", return NULL);
3169 set = isl_set_universe(isl_space_copy(graph->node[i].space));
3170 dom = isl_union_set_from_set(set);
3172 for (i = i + 1; i < graph->n; ++i) {
3173 if (!pred(&graph->node[i], data))
3174 continue;
3175 set = isl_set_universe(isl_space_copy(graph->node[i].space));
3176 dom = isl_union_set_union(dom, isl_union_set_from_set(set));
3179 return dom;
3182 /* Return a list of unions of universe domains, where each element
3183 * in the list corresponds to an SCC (or WCC) indexed by node->scc.
3185 static __isl_give isl_union_set_list *extract_sccs(isl_ctx *ctx,
3186 struct isl_sched_graph *graph)
3188 int i;
3189 isl_union_set_list *filters;
3191 filters = isl_union_set_list_alloc(ctx, graph->scc);
3192 for (i = 0; i < graph->scc; ++i) {
3193 isl_union_set *dom;
3195 dom = isl_sched_graph_domain(ctx, graph, &node_scc_exactly, i);
3196 filters = isl_union_set_list_add(filters, dom);
3199 return filters;
3202 /* Return a list of two unions of universe domains, one for the SCCs up
3203 * to and including graph->src_scc and another for the other SCCs.
3205 static __isl_give isl_union_set_list *extract_split(isl_ctx *ctx,
3206 struct isl_sched_graph *graph)
3208 isl_union_set *dom;
3209 isl_union_set_list *filters;
3211 filters = isl_union_set_list_alloc(ctx, 2);
3212 dom = isl_sched_graph_domain(ctx, graph,
3213 &node_scc_at_most, graph->src_scc);
3214 filters = isl_union_set_list_add(filters, dom);
3215 dom = isl_sched_graph_domain(ctx, graph,
3216 &node_scc_at_least, graph->src_scc + 1);
3217 filters = isl_union_set_list_add(filters, dom);
3219 return filters;
3222 /* Copy nodes that satisfy node_pred from the src dependence graph
3223 * to the dst dependence graph.
3225 static int copy_nodes(struct isl_sched_graph *dst, struct isl_sched_graph *src,
3226 int (*node_pred)(struct isl_sched_node *node, int data), int data)
3228 int i;
3230 dst->n = 0;
3231 for (i = 0; i < src->n; ++i) {
3232 int j;
3234 if (!node_pred(&src->node[i], data))
3235 continue;
3237 j = dst->n;
3238 dst->node[j].space = isl_space_copy(src->node[i].space);
3239 dst->node[j].compressed = src->node[i].compressed;
3240 dst->node[j].hull = isl_set_copy(src->node[i].hull);
3241 dst->node[j].compress =
3242 isl_multi_aff_copy(src->node[i].compress);
3243 dst->node[j].decompress =
3244 isl_multi_aff_copy(src->node[i].decompress);
3245 dst->node[j].nvar = src->node[i].nvar;
3246 dst->node[j].nparam = src->node[i].nparam;
3247 dst->node[j].sched = isl_mat_copy(src->node[i].sched);
3248 dst->node[j].sched_map = isl_map_copy(src->node[i].sched_map);
3249 dst->node[j].coincident = src->node[i].coincident;
3250 dst->n++;
3252 if (!dst->node[j].space || !dst->node[j].sched)
3253 return -1;
3254 if (dst->node[j].compressed &&
3255 (!dst->node[j].hull || !dst->node[j].compress ||
3256 !dst->node[j].decompress))
3257 return -1;
3260 return 0;
3263 /* Copy non-empty edges that satisfy edge_pred from the src dependence graph
3264 * to the dst dependence graph.
3265 * If the source or destination node of the edge is not in the destination
3266 * graph, then it must be a backward proximity edge and it should simply
3267 * be ignored.
3269 static int copy_edges(isl_ctx *ctx, struct isl_sched_graph *dst,
3270 struct isl_sched_graph *src,
3271 int (*edge_pred)(struct isl_sched_edge *edge, int data), int data)
3273 int i;
3274 enum isl_edge_type t;
3276 dst->n_edge = 0;
3277 for (i = 0; i < src->n_edge; ++i) {
3278 struct isl_sched_edge *edge = &src->edge[i];
3279 isl_map *map;
3280 isl_union_map *tagged_condition;
3281 isl_union_map *tagged_validity;
3282 struct isl_sched_node *dst_src, *dst_dst;
3284 if (!edge_pred(edge, data))
3285 continue;
3287 if (isl_map_plain_is_empty(edge->map))
3288 continue;
3290 dst_src = graph_find_node(ctx, dst, edge->src->space);
3291 dst_dst = graph_find_node(ctx, dst, edge->dst->space);
3292 if (!dst_src || !dst_dst) {
3293 if (is_validity(edge) || is_conditional_validity(edge))
3294 isl_die(ctx, isl_error_internal,
3295 "backward (conditional) validity edge",
3296 return -1);
3297 continue;
3300 map = isl_map_copy(edge->map);
3301 tagged_condition = isl_union_map_copy(edge->tagged_condition);
3302 tagged_validity = isl_union_map_copy(edge->tagged_validity);
3304 dst->edge[dst->n_edge].src = dst_src;
3305 dst->edge[dst->n_edge].dst = dst_dst;
3306 dst->edge[dst->n_edge].map = map;
3307 dst->edge[dst->n_edge].tagged_condition = tagged_condition;
3308 dst->edge[dst->n_edge].tagged_validity = tagged_validity;
3309 dst->edge[dst->n_edge].types = edge->types;
3310 dst->n_edge++;
3312 if (edge->tagged_condition && !tagged_condition)
3313 return -1;
3314 if (edge->tagged_validity && !tagged_validity)
3315 return -1;
3317 for (t = isl_edge_first; t <= isl_edge_last; ++t) {
3318 if (edge !=
3319 graph_find_edge(src, t, edge->src, edge->dst))
3320 continue;
3321 if (graph_edge_table_add(ctx, dst, t,
3322 &dst->edge[dst->n_edge - 1]) < 0)
3323 return -1;
3327 return 0;
3330 /* Compute the maximal number of variables over all nodes.
3331 * This is the maximal number of linearly independent schedule
3332 * rows that we need to compute.
3333 * Just in case we end up in a part of the dependence graph
3334 * with only lower-dimensional domains, we make sure we will
3335 * compute the required amount of extra linearly independent rows.
3337 static int compute_maxvar(struct isl_sched_graph *graph)
3339 int i;
3341 graph->maxvar = 0;
3342 for (i = 0; i < graph->n; ++i) {
3343 struct isl_sched_node *node = &graph->node[i];
3344 int nvar;
3346 if (node_update_cmap(node) < 0)
3347 return -1;
3348 nvar = node->nvar + graph->n_row - node->rank;
3349 if (nvar > graph->maxvar)
3350 graph->maxvar = nvar;
3353 return 0;
3356 /* Extract the subgraph of "graph" that consists of the node satisfying
3357 * "node_pred" and the edges satisfying "edge_pred" and store
3358 * the result in "sub".
3360 static int extract_sub_graph(isl_ctx *ctx, struct isl_sched_graph *graph,
3361 int (*node_pred)(struct isl_sched_node *node, int data),
3362 int (*edge_pred)(struct isl_sched_edge *edge, int data),
3363 int data, struct isl_sched_graph *sub)
3365 int i, n = 0, n_edge = 0;
3366 int t;
3368 for (i = 0; i < graph->n; ++i)
3369 if (node_pred(&graph->node[i], data))
3370 ++n;
3371 for (i = 0; i < graph->n_edge; ++i)
3372 if (edge_pred(&graph->edge[i], data))
3373 ++n_edge;
3374 if (graph_alloc(ctx, sub, n, n_edge) < 0)
3375 return -1;
3376 if (copy_nodes(sub, graph, node_pred, data) < 0)
3377 return -1;
3378 if (graph_init_table(ctx, sub) < 0)
3379 return -1;
3380 for (t = 0; t <= isl_edge_last; ++t)
3381 sub->max_edge[t] = graph->max_edge[t];
3382 if (graph_init_edge_tables(ctx, sub) < 0)
3383 return -1;
3384 if (copy_edges(ctx, sub, graph, edge_pred, data) < 0)
3385 return -1;
3386 sub->n_row = graph->n_row;
3387 sub->max_row = graph->max_row;
3388 sub->n_total_row = graph->n_total_row;
3389 sub->band_start = graph->band_start;
3391 return 0;
3394 static __isl_give isl_schedule_node *compute_schedule(isl_schedule_node *node,
3395 struct isl_sched_graph *graph);
3396 static __isl_give isl_schedule_node *compute_schedule_wcc(
3397 isl_schedule_node *node, struct isl_sched_graph *graph);
3399 /* Compute a schedule for a subgraph of "graph". In particular, for
3400 * the graph composed of nodes that satisfy node_pred and edges that
3401 * that satisfy edge_pred.
3402 * If the subgraph is known to consist of a single component, then wcc should
3403 * be set and then we call compute_schedule_wcc on the constructed subgraph.
3404 * Otherwise, we call compute_schedule, which will check whether the subgraph
3405 * is connected.
3407 * The schedule is inserted at "node" and the updated schedule node
3408 * is returned.
3410 static __isl_give isl_schedule_node *compute_sub_schedule(
3411 __isl_take isl_schedule_node *node, isl_ctx *ctx,
3412 struct isl_sched_graph *graph,
3413 int (*node_pred)(struct isl_sched_node *node, int data),
3414 int (*edge_pred)(struct isl_sched_edge *edge, int data),
3415 int data, int wcc)
3417 struct isl_sched_graph split = { 0 };
3419 if (extract_sub_graph(ctx, graph, node_pred, edge_pred, data,
3420 &split) < 0)
3421 goto error;
3423 if (wcc)
3424 node = compute_schedule_wcc(node, &split);
3425 else
3426 node = compute_schedule(node, &split);
3428 graph_free(ctx, &split);
3429 return node;
3430 error:
3431 graph_free(ctx, &split);
3432 return isl_schedule_node_free(node);
3435 static int edge_scc_exactly(struct isl_sched_edge *edge, int scc)
3437 return edge->src->scc == scc && edge->dst->scc == scc;
3440 static int edge_dst_scc_at_most(struct isl_sched_edge *edge, int scc)
3442 return edge->dst->scc <= scc;
3445 static int edge_src_scc_at_least(struct isl_sched_edge *edge, int scc)
3447 return edge->src->scc >= scc;
3450 /* Reset the current band by dropping all its schedule rows.
3452 static int reset_band(struct isl_sched_graph *graph)
3454 int i;
3455 int drop;
3457 drop = graph->n_total_row - graph->band_start;
3458 graph->n_total_row -= drop;
3459 graph->n_row -= drop;
3461 for (i = 0; i < graph->n; ++i) {
3462 struct isl_sched_node *node = &graph->node[i];
3464 isl_map_free(node->sched_map);
3465 node->sched_map = NULL;
3467 node->sched = isl_mat_drop_rows(node->sched,
3468 graph->band_start, drop);
3470 if (!node->sched)
3471 return -1;
3474 return 0;
3477 /* Split the current graph into two parts and compute a schedule for each
3478 * part individually. In particular, one part consists of all SCCs up
3479 * to and including graph->src_scc, while the other part contains the other
3480 * SCCs. The split is enforced by a sequence node inserted at position "node"
3481 * in the schedule tree. Return the updated schedule node.
3482 * If either of these two parts consists of a sequence, then it is spliced
3483 * into the sequence containing the two parts.
3485 * The current band is reset. It would be possible to reuse
3486 * the previously computed rows as the first rows in the next
3487 * band, but recomputing them may result in better rows as we are looking
3488 * at a smaller part of the dependence graph.
3490 static __isl_give isl_schedule_node *compute_split_schedule(
3491 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph)
3493 int is_seq;
3494 isl_ctx *ctx;
3495 isl_union_set_list *filters;
3497 if (!node)
3498 return NULL;
3500 if (reset_band(graph) < 0)
3501 return isl_schedule_node_free(node);
3503 next_band(graph);
3505 ctx = isl_schedule_node_get_ctx(node);
3506 filters = extract_split(ctx, graph);
3507 node = isl_schedule_node_insert_sequence(node, filters);
3508 node = isl_schedule_node_child(node, 1);
3509 node = isl_schedule_node_child(node, 0);
3511 node = compute_sub_schedule(node, ctx, graph,
3512 &node_scc_at_least, &edge_src_scc_at_least,
3513 graph->src_scc + 1, 0);
3514 is_seq = isl_schedule_node_get_type(node) == isl_schedule_node_sequence;
3515 node = isl_schedule_node_parent(node);
3516 node = isl_schedule_node_parent(node);
3517 if (is_seq)
3518 node = isl_schedule_node_sequence_splice_child(node, 1);
3519 node = isl_schedule_node_child(node, 0);
3520 node = isl_schedule_node_child(node, 0);
3521 node = compute_sub_schedule(node, ctx, graph,
3522 &node_scc_at_most, &edge_dst_scc_at_most,
3523 graph->src_scc, 0);
3524 is_seq = isl_schedule_node_get_type(node) == isl_schedule_node_sequence;
3525 node = isl_schedule_node_parent(node);
3526 node = isl_schedule_node_parent(node);
3527 if (is_seq)
3528 node = isl_schedule_node_sequence_splice_child(node, 0);
3530 return node;
3533 /* Insert a band node at position "node" in the schedule tree corresponding
3534 * to the current band in "graph". Mark the band node permutable
3535 * if "permutable" is set.
3536 * The partial schedules and the coincidence property are extracted
3537 * from the graph nodes.
3538 * Return the updated schedule node.
3540 static __isl_give isl_schedule_node *insert_current_band(
3541 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph,
3542 int permutable)
3544 int i;
3545 int start, end, n;
3546 isl_multi_aff *ma;
3547 isl_multi_pw_aff *mpa;
3548 isl_multi_union_pw_aff *mupa;
3550 if (!node)
3551 return NULL;
3553 if (graph->n < 1)
3554 isl_die(isl_schedule_node_get_ctx(node), isl_error_internal,
3555 "graph should have at least one node",
3556 return isl_schedule_node_free(node));
3558 start = graph->band_start;
3559 end = graph->n_total_row;
3560 n = end - start;
3562 ma = node_extract_partial_schedule_multi_aff(&graph->node[0], start, n);
3563 mpa = isl_multi_pw_aff_from_multi_aff(ma);
3564 mupa = isl_multi_union_pw_aff_from_multi_pw_aff(mpa);
3566 for (i = 1; i < graph->n; ++i) {
3567 isl_multi_union_pw_aff *mupa_i;
3569 ma = node_extract_partial_schedule_multi_aff(&graph->node[i],
3570 start, n);
3571 mpa = isl_multi_pw_aff_from_multi_aff(ma);
3572 mupa_i = isl_multi_union_pw_aff_from_multi_pw_aff(mpa);
3573 mupa = isl_multi_union_pw_aff_union_add(mupa, mupa_i);
3575 node = isl_schedule_node_insert_partial_schedule(node, mupa);
3577 for (i = 0; i < n; ++i)
3578 node = isl_schedule_node_band_member_set_coincident(node, i,
3579 graph->node[0].coincident[start + i]);
3580 node = isl_schedule_node_band_set_permutable(node, permutable);
3582 return node;
3585 /* Update the dependence relations based on the current schedule,
3586 * add the current band to "node" and then continue with the computation
3587 * of the next band.
3588 * Return the updated schedule node.
3590 static __isl_give isl_schedule_node *compute_next_band(
3591 __isl_take isl_schedule_node *node,
3592 struct isl_sched_graph *graph, int permutable)
3594 isl_ctx *ctx;
3596 if (!node)
3597 return NULL;
3599 ctx = isl_schedule_node_get_ctx(node);
3600 if (update_edges(ctx, graph) < 0)
3601 return isl_schedule_node_free(node);
3602 node = insert_current_band(node, graph, permutable);
3603 next_band(graph);
3605 node = isl_schedule_node_child(node, 0);
3606 node = compute_schedule(node, graph);
3607 node = isl_schedule_node_parent(node);
3609 return node;
3612 /* Add constraints to graph->lp that force the dependence "map" (which
3613 * is part of the dependence relation of "edge")
3614 * to be respected and attempt to carry it, where the edge is one from
3615 * a node j to itself. "pos" is the sequence number of the given map.
3616 * That is, add constraints that enforce
3618 * (c_j_0 + c_j_n n + c_j_x y) - (c_j_0 + c_j_n n + c_j_x x)
3619 * = c_j_x (y - x) >= e_i
3621 * for each (x,y) in R.
3622 * We obtain general constraints on coefficients (c_0, c_n, c_x)
3623 * of valid constraints for (y - x) and then plug in (-e_i, 0, c_j_x),
3624 * with each coefficient in c_j_x represented as a pair of non-negative
3625 * coefficients.
3627 static int add_intra_constraints(struct isl_sched_graph *graph,
3628 struct isl_sched_edge *edge, __isl_take isl_map *map, int pos)
3630 int offset;
3631 isl_ctx *ctx = isl_map_get_ctx(map);
3632 isl_space *space;
3633 isl_dim_map *dim_map;
3634 isl_basic_set *coef;
3635 struct isl_sched_node *node = edge->src;
3637 coef = intra_coefficients(graph, node, map);
3638 if (!coef)
3639 return -1;
3641 space = isl_space_unwrap(isl_basic_set_get_space(coef));
3642 space = isl_space_domain(space);
3643 offset = isl_space_dim(space, isl_dim_set);
3644 isl_space_free(space);
3646 dim_map = intra_dim_map(ctx, graph, node, offset, 1);
3647 isl_dim_map_range(dim_map, 3 + pos, 0, 0, 0, 1, -1);
3648 graph->lp = isl_basic_set_extend_constraints(graph->lp,
3649 coef->n_eq, coef->n_ineq);
3650 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
3651 coef, dim_map);
3653 return 0;
3656 /* Add constraints to graph->lp that force the dependence "map" (which
3657 * is part of the dependence relation of "edge")
3658 * to be respected and attempt to carry it, where the edge is one from
3659 * node j to node k. "pos" is the sequence number of the given map.
3660 * That is, add constraints that enforce
3662 * (c_k_0 + c_k_n n + c_k_x y) - (c_j_0 + c_j_n n + c_j_x x) >= e_i
3664 * for each (x,y) in R.
3665 * We obtain general constraints on coefficients (c_0, c_n, c_x)
3666 * of valid constraints for R and then plug in
3667 * (-e_i + c_k_0 - c_j_0, c_k_n - c_j_n, c_k_x - c_j_x)
3668 * with each coefficient (except e_i, c_k_0 and c_j_0)
3669 * represented as a pair of non-negative coefficients.
3671 static int add_inter_constraints(struct isl_sched_graph *graph,
3672 struct isl_sched_edge *edge, __isl_take isl_map *map, int pos)
3674 unsigned total;
3675 isl_ctx *ctx = isl_map_get_ctx(map);
3676 isl_space *space;
3677 isl_dim_map *dim_map;
3678 isl_basic_set *coef;
3679 struct isl_sched_node *src = edge->src;
3680 struct isl_sched_node *dst = edge->dst;
3682 coef = inter_coefficients(graph, edge, map);
3683 if (!coef)
3684 return -1;
3686 space = isl_space_unwrap(isl_basic_set_get_space(coef));
3687 space = isl_space_domain(space);
3689 total = isl_basic_set_total_dim(graph->lp);
3690 dim_map = isl_dim_map_alloc(ctx, total);
3692 isl_dim_map_range(dim_map, 3 + pos, 0, 0, 0, 1, -1);
3694 isl_dim_map_range(dim_map, dst->start, 0, 0, 0, 1, 1);
3695 isl_dim_map_range(dim_map, dst->start + 1, 2, 1, 1, dst->nparam, -1);
3696 isl_dim_map_range(dim_map, dst->start + 2, 2, 1, 1, dst->nparam, 1);
3697 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 1, 2,
3698 isl_space_dim(space, isl_dim_set) + src->nvar, 1,
3699 dst->nvar, -1);
3700 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 2, 2,
3701 isl_space_dim(space, isl_dim_set) + src->nvar, 1,
3702 dst->nvar, 1);
3704 isl_dim_map_range(dim_map, src->start, 0, 0, 0, 1, -1);
3705 isl_dim_map_range(dim_map, src->start + 1, 2, 1, 1, src->nparam, 1);
3706 isl_dim_map_range(dim_map, src->start + 2, 2, 1, 1, src->nparam, -1);
3707 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 1, 2,
3708 isl_space_dim(space, isl_dim_set), 1,
3709 src->nvar, 1);
3710 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 2, 2,
3711 isl_space_dim(space, isl_dim_set), 1,
3712 src->nvar, -1);
3714 graph->lp = isl_basic_set_extend_constraints(graph->lp,
3715 coef->n_eq, coef->n_ineq);
3716 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
3717 coef, dim_map);
3718 isl_space_free(space);
3720 return 0;
3723 /* Add constraints to graph->lp that force all (conditional) validity
3724 * dependences to be respected and attempt to carry them.
3726 static int add_all_constraints(struct isl_sched_graph *graph)
3728 int i, j;
3729 int pos;
3731 pos = 0;
3732 for (i = 0; i < graph->n_edge; ++i) {
3733 struct isl_sched_edge *edge= &graph->edge[i];
3735 if (!is_any_validity(edge))
3736 continue;
3738 for (j = 0; j < edge->map->n; ++j) {
3739 isl_basic_map *bmap;
3740 isl_map *map;
3742 bmap = isl_basic_map_copy(edge->map->p[j]);
3743 map = isl_map_from_basic_map(bmap);
3745 if (edge->src == edge->dst &&
3746 add_intra_constraints(graph, edge, map, pos) < 0)
3747 return -1;
3748 if (edge->src != edge->dst &&
3749 add_inter_constraints(graph, edge, map, pos) < 0)
3750 return -1;
3751 ++pos;
3755 return 0;
3758 /* Count the number of equality and inequality constraints
3759 * that will be added to the carry_lp problem.
3760 * We count each edge exactly once.
3762 static int count_all_constraints(struct isl_sched_graph *graph,
3763 int *n_eq, int *n_ineq)
3765 int i, j;
3767 *n_eq = *n_ineq = 0;
3768 for (i = 0; i < graph->n_edge; ++i) {
3769 struct isl_sched_edge *edge= &graph->edge[i];
3771 if (!is_any_validity(edge))
3772 continue;
3774 for (j = 0; j < edge->map->n; ++j) {
3775 isl_basic_map *bmap;
3776 isl_map *map;
3778 bmap = isl_basic_map_copy(edge->map->p[j]);
3779 map = isl_map_from_basic_map(bmap);
3781 if (count_map_constraints(graph, edge, map,
3782 n_eq, n_ineq, 1, 0) < 0)
3783 return -1;
3787 return 0;
3790 /* Construct an LP problem for finding schedule coefficients
3791 * such that the schedule carries as many dependences as possible.
3792 * In particular, for each dependence i, we bound the dependence distance
3793 * from below by e_i, with 0 <= e_i <= 1 and then maximize the sum
3794 * of all e_i's. Dependences with e_i = 0 in the solution are simply
3795 * respected, while those with e_i > 0 (in practice e_i = 1) are carried.
3796 * Note that if the dependence relation is a union of basic maps,
3797 * then we have to consider each basic map individually as it may only
3798 * be possible to carry the dependences expressed by some of those
3799 * basic maps and not all of them.
3800 * Below, we consider each of those basic maps as a separate "edge".
3802 * All variables of the LP are non-negative. The actual coefficients
3803 * may be negative, so each coefficient is represented as the difference
3804 * of two non-negative variables. The negative part always appears
3805 * immediately before the positive part.
3806 * Other than that, the variables have the following order
3808 * - sum of (1 - e_i) over all edges
3809 * - sum of positive and negative parts of all c_n coefficients
3810 * (unconstrained when computing non-parametric schedules)
3811 * - sum of positive and negative parts of all c_x coefficients
3812 * - for each edge
3813 * - e_i
3814 * - for each node
3815 * - c_i_0
3816 * - positive and negative parts of c_i_n (if parametric)
3817 * - positive and negative parts of c_i_x
3819 * The constraints are those from the (validity) edges plus three equalities
3820 * to express the sums and n_edge inequalities to express e_i <= 1.
3822 static isl_stat setup_carry_lp(isl_ctx *ctx, struct isl_sched_graph *graph)
3824 int i;
3825 int k;
3826 isl_space *dim;
3827 unsigned total;
3828 int n_eq, n_ineq;
3829 int n_edge;
3831 n_edge = 0;
3832 for (i = 0; i < graph->n_edge; ++i)
3833 n_edge += graph->edge[i].map->n;
3835 total = 3 + n_edge;
3836 for (i = 0; i < graph->n; ++i) {
3837 struct isl_sched_node *node = &graph->node[graph->sorted[i]];
3838 node->start = total;
3839 total += 1 + 2 * (node->nparam + node->nvar);
3842 if (count_all_constraints(graph, &n_eq, &n_ineq) < 0)
3843 return isl_stat_error;
3845 dim = isl_space_set_alloc(ctx, 0, total);
3846 isl_basic_set_free(graph->lp);
3847 n_eq += 3;
3848 n_ineq += n_edge;
3849 graph->lp = isl_basic_set_alloc_space(dim, 0, n_eq, n_ineq);
3850 graph->lp = isl_basic_set_set_rational(graph->lp);
3852 k = isl_basic_set_alloc_equality(graph->lp);
3853 if (k < 0)
3854 return isl_stat_error;
3855 isl_seq_clr(graph->lp->eq[k], 1 + total);
3856 isl_int_set_si(graph->lp->eq[k][0], -n_edge);
3857 isl_int_set_si(graph->lp->eq[k][1], 1);
3858 for (i = 0; i < n_edge; ++i)
3859 isl_int_set_si(graph->lp->eq[k][4 + i], 1);
3861 if (add_param_sum_constraint(graph, 1) < 0)
3862 return isl_stat_error;
3863 if (add_var_sum_constraint(graph, 2) < 0)
3864 return isl_stat_error;
3866 for (i = 0; i < n_edge; ++i) {
3867 k = isl_basic_set_alloc_inequality(graph->lp);
3868 if (k < 0)
3869 return isl_stat_error;
3870 isl_seq_clr(graph->lp->ineq[k], 1 + total);
3871 isl_int_set_si(graph->lp->ineq[k][4 + i], -1);
3872 isl_int_set_si(graph->lp->ineq[k][0], 1);
3875 if (add_all_constraints(graph) < 0)
3876 return isl_stat_error;
3878 return isl_stat_ok;
3881 static __isl_give isl_schedule_node *compute_component_schedule(
3882 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph,
3883 int wcc);
3885 /* Comparison function for sorting the statements based on
3886 * the corresponding value in "r".
3888 static int smaller_value(const void *a, const void *b, void *data)
3890 isl_vec *r = data;
3891 const int *i1 = a;
3892 const int *i2 = b;
3894 return isl_int_cmp(r->el[*i1], r->el[*i2]);
3897 /* If the schedule_split_scaled option is set and if the linear
3898 * parts of the scheduling rows for all nodes in the graphs have
3899 * a non-trivial common divisor, then split off the remainder of the
3900 * constant term modulo this common divisor from the linear part.
3901 * Otherwise, insert a band node directly and continue with
3902 * the construction of the schedule.
3904 * If a non-trivial common divisor is found, then
3905 * the linear part is reduced and the remainder is enforced
3906 * by a sequence node with the children placed in the order
3907 * of this remainder.
3908 * In particular, we assign an scc index based on the remainder and
3909 * then rely on compute_component_schedule to insert the sequence and
3910 * to continue the schedule construction on each part.
3912 static __isl_give isl_schedule_node *split_scaled(
3913 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph)
3915 int i;
3916 int row;
3917 int scc;
3918 isl_ctx *ctx;
3919 isl_int gcd, gcd_i;
3920 isl_vec *r;
3921 int *order;
3923 if (!node)
3924 return NULL;
3926 ctx = isl_schedule_node_get_ctx(node);
3927 if (!ctx->opt->schedule_split_scaled)
3928 return compute_next_band(node, graph, 0);
3929 if (graph->n <= 1)
3930 return compute_next_band(node, graph, 0);
3932 isl_int_init(gcd);
3933 isl_int_init(gcd_i);
3935 isl_int_set_si(gcd, 0);
3937 row = isl_mat_rows(graph->node[0].sched) - 1;
3939 for (i = 0; i < graph->n; ++i) {
3940 struct isl_sched_node *node = &graph->node[i];
3941 int cols = isl_mat_cols(node->sched);
3943 isl_seq_gcd(node->sched->row[row] + 1, cols - 1, &gcd_i);
3944 isl_int_gcd(gcd, gcd, gcd_i);
3947 isl_int_clear(gcd_i);
3949 if (isl_int_cmp_si(gcd, 1) <= 0) {
3950 isl_int_clear(gcd);
3951 return compute_next_band(node, graph, 0);
3954 r = isl_vec_alloc(ctx, graph->n);
3955 order = isl_calloc_array(ctx, int, graph->n);
3956 if (!r || !order)
3957 goto error;
3959 for (i = 0; i < graph->n; ++i) {
3960 struct isl_sched_node *node = &graph->node[i];
3962 order[i] = i;
3963 isl_int_fdiv_r(r->el[i], node->sched->row[row][0], gcd);
3964 isl_int_fdiv_q(node->sched->row[row][0],
3965 node->sched->row[row][0], gcd);
3966 isl_int_mul(node->sched->row[row][0],
3967 node->sched->row[row][0], gcd);
3968 node->sched = isl_mat_scale_down_row(node->sched, row, gcd);
3969 if (!node->sched)
3970 goto error;
3973 if (isl_sort(order, graph->n, sizeof(order[0]), &smaller_value, r) < 0)
3974 goto error;
3976 scc = 0;
3977 for (i = 0; i < graph->n; ++i) {
3978 if (i > 0 && isl_int_ne(r->el[order[i - 1]], r->el[order[i]]))
3979 ++scc;
3980 graph->node[order[i]].scc = scc;
3982 graph->scc = ++scc;
3983 graph->weak = 0;
3985 isl_int_clear(gcd);
3986 isl_vec_free(r);
3987 free(order);
3989 if (update_edges(ctx, graph) < 0)
3990 return isl_schedule_node_free(node);
3991 node = insert_current_band(node, graph, 0);
3992 next_band(graph);
3994 node = isl_schedule_node_child(node, 0);
3995 node = compute_component_schedule(node, graph, 0);
3996 node = isl_schedule_node_parent(node);
3998 return node;
3999 error:
4000 isl_vec_free(r);
4001 free(order);
4002 isl_int_clear(gcd);
4003 return isl_schedule_node_free(node);
4006 /* Is the schedule row "sol" trivial on node "node"?
4007 * That is, is the solution zero on the dimensions orthogonal to
4008 * the previously found solutions?
4009 * Return 1 if the solution is trivial, 0 if it is not and -1 on error.
4011 * Each coefficient is represented as the difference between
4012 * two non-negative values in "sol". "sol" has been computed
4013 * in terms of the original iterators (i.e., without use of cmap).
4014 * We construct the schedule row s and write it as a linear
4015 * combination of (linear combinations of) previously computed schedule rows.
4016 * s = Q c or c = U s.
4017 * If the final entries of c are all zero, then the solution is trivial.
4019 static int is_trivial(struct isl_sched_node *node, __isl_keep isl_vec *sol)
4021 int i;
4022 int pos;
4023 int trivial;
4024 isl_ctx *ctx;
4025 isl_vec *node_sol;
4027 if (!sol)
4028 return -1;
4029 if (node->nvar == node->rank)
4030 return 0;
4032 ctx = isl_vec_get_ctx(sol);
4033 node_sol = isl_vec_alloc(ctx, node->nvar);
4034 if (!node_sol)
4035 return -1;
4037 pos = 1 + node->start + 1 + 2 * node->nparam;
4039 for (i = 0; i < node->nvar; ++i)
4040 isl_int_sub(node_sol->el[i],
4041 sol->el[pos + 2 * i + 1], sol->el[pos + 2 * i]);
4043 node_sol = isl_mat_vec_product(isl_mat_copy(node->cinv), node_sol);
4045 if (!node_sol)
4046 return -1;
4048 trivial = isl_seq_first_non_zero(node_sol->el + node->rank,
4049 node->nvar - node->rank) == -1;
4051 isl_vec_free(node_sol);
4053 return trivial;
4056 /* Is the schedule row "sol" trivial on any node where it should
4057 * not be trivial?
4058 * "sol" has been computed in terms of the original iterators
4059 * (i.e., without use of cmap).
4060 * Return 1 if any solution is trivial, 0 if they are not and -1 on error.
4062 static int is_any_trivial(struct isl_sched_graph *graph,
4063 __isl_keep isl_vec *sol)
4065 int i;
4067 for (i = 0; i < graph->n; ++i) {
4068 struct isl_sched_node *node = &graph->node[i];
4069 int trivial;
4071 if (!needs_row(graph, node))
4072 continue;
4073 trivial = is_trivial(node, sol);
4074 if (trivial < 0 || trivial)
4075 return trivial;
4078 return 0;
4081 /* Construct a schedule row for each node such that as many dependences
4082 * as possible are carried and then continue with the next band.
4084 * Note that despite the fact that the problem is solved using a rational
4085 * solver, the solution is guaranteed to be integral.
4086 * Specifically, the dependence distance lower bounds e_i (and therefore
4087 * also their sum) are integers. See Lemma 5 of [1].
4089 * If the computed schedule row turns out to be trivial on one or
4090 * more nodes where it should not be trivial, then we throw it away
4091 * and try again on each component separately.
4093 * If there is only one component, then we accept the schedule row anyway,
4094 * but we do not consider it as a complete row and therefore do not
4095 * increment graph->n_row. Note that the ranks of the nodes that
4096 * do get a non-trivial schedule part will get updated regardless and
4097 * graph->maxvar is computed based on these ranks. The test for
4098 * whether more schedule rows are required in compute_schedule_wcc
4099 * is therefore not affected.
4101 * Insert a band corresponding to the schedule row at position "node"
4102 * of the schedule tree and continue with the construction of the schedule.
4103 * This insertion and the continued construction is performed by split_scaled
4104 * after optionally checking for non-trivial common divisors.
4106 * [1] P. Feautrier, Some Efficient Solutions to the Affine Scheduling
4107 * Problem, Part II: Multi-Dimensional Time.
4108 * In Intl. Journal of Parallel Programming, 1992.
4110 static __isl_give isl_schedule_node *carry_dependences(
4111 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph)
4113 int i;
4114 int n_edge;
4115 int trivial;
4116 isl_ctx *ctx;
4117 isl_vec *sol;
4118 isl_basic_set *lp;
4120 if (!node)
4121 return NULL;
4123 n_edge = 0;
4124 for (i = 0; i < graph->n_edge; ++i)
4125 n_edge += graph->edge[i].map->n;
4127 ctx = isl_schedule_node_get_ctx(node);
4128 if (setup_carry_lp(ctx, graph) < 0)
4129 return isl_schedule_node_free(node);
4131 lp = isl_basic_set_copy(graph->lp);
4132 sol = isl_tab_basic_set_non_neg_lexmin(lp);
4133 if (!sol)
4134 return isl_schedule_node_free(node);
4136 if (sol->size == 0) {
4137 isl_vec_free(sol);
4138 isl_die(ctx, isl_error_internal,
4139 "error in schedule construction",
4140 return isl_schedule_node_free(node));
4143 isl_int_divexact(sol->el[1], sol->el[1], sol->el[0]);
4144 if (isl_int_cmp_si(sol->el[1], n_edge) >= 0) {
4145 isl_vec_free(sol);
4146 isl_die(ctx, isl_error_unknown,
4147 "unable to carry dependences",
4148 return isl_schedule_node_free(node));
4151 trivial = is_any_trivial(graph, sol);
4152 if (trivial < 0) {
4153 sol = isl_vec_free(sol);
4154 } else if (trivial && graph->scc > 1) {
4155 isl_vec_free(sol);
4156 return compute_component_schedule(node, graph, 1);
4159 if (update_schedule(graph, sol, 0, 0) < 0)
4160 return isl_schedule_node_free(node);
4161 if (trivial)
4162 graph->n_row--;
4164 return split_scaled(node, graph);
4167 /* Topologically sort statements mapped to the same schedule iteration
4168 * and add insert a sequence node in front of "node"
4169 * corresponding to this order.
4170 * If "initialized" is set, then it may be assumed that compute_maxvar
4171 * has been called on the current band. Otherwise, call
4172 * compute_maxvar if and before carry_dependences gets called.
4174 * If it turns out to be impossible to sort the statements apart,
4175 * because different dependences impose different orderings
4176 * on the statements, then we extend the schedule such that
4177 * it carries at least one more dependence.
4179 static __isl_give isl_schedule_node *sort_statements(
4180 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph,
4181 int initialized)
4183 isl_ctx *ctx;
4184 isl_union_set_list *filters;
4186 if (!node)
4187 return NULL;
4189 ctx = isl_schedule_node_get_ctx(node);
4190 if (graph->n < 1)
4191 isl_die(ctx, isl_error_internal,
4192 "graph should have at least one node",
4193 return isl_schedule_node_free(node));
4195 if (graph->n == 1)
4196 return node;
4198 if (update_edges(ctx, graph) < 0)
4199 return isl_schedule_node_free(node);
4201 if (graph->n_edge == 0)
4202 return node;
4204 if (detect_sccs(ctx, graph) < 0)
4205 return isl_schedule_node_free(node);
4207 next_band(graph);
4208 if (graph->scc < graph->n) {
4209 if (!initialized && compute_maxvar(graph) < 0)
4210 return isl_schedule_node_free(node);
4211 return carry_dependences(node, graph);
4214 filters = extract_sccs(ctx, graph);
4215 node = isl_schedule_node_insert_sequence(node, filters);
4217 return node;
4220 /* Are there any (non-empty) (conditional) validity edges in the graph?
4222 static int has_validity_edges(struct isl_sched_graph *graph)
4224 int i;
4226 for (i = 0; i < graph->n_edge; ++i) {
4227 int empty;
4229 empty = isl_map_plain_is_empty(graph->edge[i].map);
4230 if (empty < 0)
4231 return -1;
4232 if (empty)
4233 continue;
4234 if (is_any_validity(&graph->edge[i]))
4235 return 1;
4238 return 0;
4241 /* Should we apply a Feautrier step?
4242 * That is, did the user request the Feautrier algorithm and are
4243 * there any validity dependences (left)?
4245 static int need_feautrier_step(isl_ctx *ctx, struct isl_sched_graph *graph)
4247 if (ctx->opt->schedule_algorithm != ISL_SCHEDULE_ALGORITHM_FEAUTRIER)
4248 return 0;
4250 return has_validity_edges(graph);
4253 /* Compute a schedule for a connected dependence graph using Feautrier's
4254 * multi-dimensional scheduling algorithm and return the updated schedule node.
4256 * The original algorithm is described in [1].
4257 * The main idea is to minimize the number of scheduling dimensions, by
4258 * trying to satisfy as many dependences as possible per scheduling dimension.
4260 * [1] P. Feautrier, Some Efficient Solutions to the Affine Scheduling
4261 * Problem, Part II: Multi-Dimensional Time.
4262 * In Intl. Journal of Parallel Programming, 1992.
4264 static __isl_give isl_schedule_node *compute_schedule_wcc_feautrier(
4265 isl_schedule_node *node, struct isl_sched_graph *graph)
4267 return carry_dependences(node, graph);
4270 /* Turn off the "local" bit on all (condition) edges.
4272 static void clear_local_edges(struct isl_sched_graph *graph)
4274 int i;
4276 for (i = 0; i < graph->n_edge; ++i)
4277 if (is_condition(&graph->edge[i]))
4278 clear_local(&graph->edge[i]);
4281 /* Does "graph" have both condition and conditional validity edges?
4283 static int need_condition_check(struct isl_sched_graph *graph)
4285 int i;
4286 int any_condition = 0;
4287 int any_conditional_validity = 0;
4289 for (i = 0; i < graph->n_edge; ++i) {
4290 if (is_condition(&graph->edge[i]))
4291 any_condition = 1;
4292 if (is_conditional_validity(&graph->edge[i]))
4293 any_conditional_validity = 1;
4296 return any_condition && any_conditional_validity;
4299 /* Does "graph" contain any coincidence edge?
4301 static int has_any_coincidence(struct isl_sched_graph *graph)
4303 int i;
4305 for (i = 0; i < graph->n_edge; ++i)
4306 if (is_coincidence(&graph->edge[i]))
4307 return 1;
4309 return 0;
4312 /* Extract the final schedule row as a map with the iteration domain
4313 * of "node" as domain.
4315 static __isl_give isl_map *final_row(struct isl_sched_node *node)
4317 isl_local_space *ls;
4318 isl_aff *aff;
4319 int row;
4321 row = isl_mat_rows(node->sched) - 1;
4322 ls = isl_local_space_from_space(isl_space_copy(node->space));
4323 aff = extract_schedule_row(ls, node, row);
4324 return isl_map_from_aff(aff);
4327 /* Is the conditional validity dependence in the edge with index "edge_index"
4328 * violated by the latest (i.e., final) row of the schedule?
4329 * That is, is i scheduled after j
4330 * for any conditional validity dependence i -> j?
4332 static int is_violated(struct isl_sched_graph *graph, int edge_index)
4334 isl_map *src_sched, *dst_sched, *map;
4335 struct isl_sched_edge *edge = &graph->edge[edge_index];
4336 int empty;
4338 src_sched = final_row(edge->src);
4339 dst_sched = final_row(edge->dst);
4340 map = isl_map_copy(edge->map);
4341 map = isl_map_apply_domain(map, src_sched);
4342 map = isl_map_apply_range(map, dst_sched);
4343 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_out, 0);
4344 empty = isl_map_is_empty(map);
4345 isl_map_free(map);
4347 if (empty < 0)
4348 return -1;
4350 return !empty;
4353 /* Does "graph" have any satisfied condition edges that
4354 * are adjacent to the conditional validity constraint with
4355 * domain "conditional_source" and range "conditional_sink"?
4357 * A satisfied condition is one that is not local.
4358 * If a condition was forced to be local already (i.e., marked as local)
4359 * then there is no need to check if it is in fact local.
4361 * Additionally, mark all adjacent condition edges found as local.
4363 static int has_adjacent_true_conditions(struct isl_sched_graph *graph,
4364 __isl_keep isl_union_set *conditional_source,
4365 __isl_keep isl_union_set *conditional_sink)
4367 int i;
4368 int any = 0;
4370 for (i = 0; i < graph->n_edge; ++i) {
4371 int adjacent, local;
4372 isl_union_map *condition;
4374 if (!is_condition(&graph->edge[i]))
4375 continue;
4376 if (is_local(&graph->edge[i]))
4377 continue;
4379 condition = graph->edge[i].tagged_condition;
4380 adjacent = domain_intersects(condition, conditional_sink);
4381 if (adjacent >= 0 && !adjacent)
4382 adjacent = range_intersects(condition,
4383 conditional_source);
4384 if (adjacent < 0)
4385 return -1;
4386 if (!adjacent)
4387 continue;
4389 set_local(&graph->edge[i]);
4391 local = is_condition_false(&graph->edge[i]);
4392 if (local < 0)
4393 return -1;
4394 if (!local)
4395 any = 1;
4398 return any;
4401 /* Are there any violated conditional validity dependences with
4402 * adjacent condition dependences that are not local with respect
4403 * to the current schedule?
4404 * That is, is the conditional validity constraint violated?
4406 * Additionally, mark all those adjacent condition dependences as local.
4407 * We also mark those adjacent condition dependences that were not marked
4408 * as local before, but just happened to be local already. This ensures
4409 * that they remain local if the schedule is recomputed.
4411 * We first collect domain and range of all violated conditional validity
4412 * dependences and then check if there are any adjacent non-local
4413 * condition dependences.
4415 static int has_violated_conditional_constraint(isl_ctx *ctx,
4416 struct isl_sched_graph *graph)
4418 int i;
4419 int any = 0;
4420 isl_union_set *source, *sink;
4422 source = isl_union_set_empty(isl_space_params_alloc(ctx, 0));
4423 sink = isl_union_set_empty(isl_space_params_alloc(ctx, 0));
4424 for (i = 0; i < graph->n_edge; ++i) {
4425 isl_union_set *uset;
4426 isl_union_map *umap;
4427 int violated;
4429 if (!is_conditional_validity(&graph->edge[i]))
4430 continue;
4432 violated = is_violated(graph, i);
4433 if (violated < 0)
4434 goto error;
4435 if (!violated)
4436 continue;
4438 any = 1;
4440 umap = isl_union_map_copy(graph->edge[i].tagged_validity);
4441 uset = isl_union_map_domain(umap);
4442 source = isl_union_set_union(source, uset);
4443 source = isl_union_set_coalesce(source);
4445 umap = isl_union_map_copy(graph->edge[i].tagged_validity);
4446 uset = isl_union_map_range(umap);
4447 sink = isl_union_set_union(sink, uset);
4448 sink = isl_union_set_coalesce(sink);
4451 if (any)
4452 any = has_adjacent_true_conditions(graph, source, sink);
4454 isl_union_set_free(source);
4455 isl_union_set_free(sink);
4456 return any;
4457 error:
4458 isl_union_set_free(source);
4459 isl_union_set_free(sink);
4460 return -1;
4463 /* Examine the current band (the rows between graph->band_start and
4464 * graph->n_total_row), deciding whether to drop it or add it to "node"
4465 * and then continue with the computation of the next band, if any.
4466 * If "initialized" is set, then it may be assumed that compute_maxvar
4467 * has been called on the current band. Otherwise, call
4468 * compute_maxvar if and before carry_dependences gets called.
4470 * The caller keeps looking for a new row as long as
4471 * graph->n_row < graph->maxvar. If the latest attempt to find
4472 * such a row failed (i.e., we still have graph->n_row < graph->maxvar),
4473 * then we either
4474 * - split between SCCs and start over (assuming we found an interesting
4475 * pair of SCCs between which to split)
4476 * - continue with the next band (assuming the current band has at least
4477 * one row)
4478 * - try to carry as many dependences as possible and continue with the next
4479 * band
4480 * In each case, we first insert a band node in the schedule tree
4481 * if any rows have been computed.
4483 * If the caller managed to complete the schedule, we insert a band node
4484 * (if any schedule rows were computed) and we finish off by topologically
4485 * sorting the statements based on the remaining dependences.
4487 static __isl_give isl_schedule_node *compute_schedule_finish_band(
4488 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph,
4489 int initialized)
4491 int insert;
4493 if (!node)
4494 return NULL;
4496 if (graph->n_row < graph->maxvar) {
4497 isl_ctx *ctx;
4498 int empty = graph->n_total_row == graph->band_start;
4500 ctx = isl_schedule_node_get_ctx(node);
4501 if (!ctx->opt->schedule_maximize_band_depth && !empty)
4502 return compute_next_band(node, graph, 1);
4503 if (graph->src_scc >= 0)
4504 return compute_split_schedule(node, graph);
4505 if (!empty)
4506 return compute_next_band(node, graph, 1);
4507 if (!initialized && compute_maxvar(graph) < 0)
4508 return isl_schedule_node_free(node);
4509 return carry_dependences(node, graph);
4512 insert = graph->n_total_row > graph->band_start;
4513 if (insert) {
4514 node = insert_current_band(node, graph, 1);
4515 node = isl_schedule_node_child(node, 0);
4517 node = sort_statements(node, graph, initialized);
4518 if (insert)
4519 node = isl_schedule_node_parent(node);
4521 return node;
4524 /* Construct a band of schedule rows for a connected dependence graph.
4525 * The caller is responsible for determining the strongly connected
4526 * components and calling compute_maxvar first.
4528 * We try to find a sequence of as many schedule rows as possible that result
4529 * in non-negative dependence distances (independent of the previous rows
4530 * in the sequence, i.e., such that the sequence is tilable), with as
4531 * many of the initial rows as possible satisfying the coincidence constraints.
4532 * The computation stops if we can't find any more rows or if we have found
4533 * all the rows we wanted to find.
4535 * If ctx->opt->schedule_outer_coincidence is set, then we force the
4536 * outermost dimension to satisfy the coincidence constraints. If this
4537 * turns out to be impossible, we fall back on the general scheme above
4538 * and try to carry as many dependences as possible.
4540 * If "graph" contains both condition and conditional validity dependences,
4541 * then we need to check that that the conditional schedule constraint
4542 * is satisfied, i.e., there are no violated conditional validity dependences
4543 * that are adjacent to any non-local condition dependences.
4544 * If there are, then we mark all those adjacent condition dependences
4545 * as local and recompute the current band. Those dependences that
4546 * are marked local will then be forced to be local.
4547 * The initial computation is performed with no dependences marked as local.
4548 * If we are lucky, then there will be no violated conditional validity
4549 * dependences adjacent to any non-local condition dependences.
4550 * Otherwise, we mark some additional condition dependences as local and
4551 * recompute. We continue this process until there are no violations left or
4552 * until we are no longer able to compute a schedule.
4553 * Since there are only a finite number of dependences,
4554 * there will only be a finite number of iterations.
4556 static isl_stat compute_schedule_wcc_band(isl_ctx *ctx,
4557 struct isl_sched_graph *graph)
4559 int has_coincidence;
4560 int use_coincidence;
4561 int force_coincidence = 0;
4562 int check_conditional;
4564 if (sort_sccs(graph) < 0)
4565 return isl_stat_error;
4567 clear_local_edges(graph);
4568 check_conditional = need_condition_check(graph);
4569 has_coincidence = has_any_coincidence(graph);
4571 if (ctx->opt->schedule_outer_coincidence)
4572 force_coincidence = 1;
4574 use_coincidence = has_coincidence;
4575 while (graph->n_row < graph->maxvar) {
4576 isl_vec *sol;
4577 int violated;
4578 int coincident;
4580 graph->src_scc = -1;
4581 graph->dst_scc = -1;
4583 if (setup_lp(ctx, graph, use_coincidence) < 0)
4584 return isl_stat_error;
4585 sol = solve_lp(graph);
4586 if (!sol)
4587 return isl_stat_error;
4588 if (sol->size == 0) {
4589 int empty = graph->n_total_row == graph->band_start;
4591 isl_vec_free(sol);
4592 if (use_coincidence && (!force_coincidence || !empty)) {
4593 use_coincidence = 0;
4594 continue;
4596 return isl_stat_ok;
4598 coincident = !has_coincidence || use_coincidence;
4599 if (update_schedule(graph, sol, 1, coincident) < 0)
4600 return isl_stat_error;
4602 if (!check_conditional)
4603 continue;
4604 violated = has_violated_conditional_constraint(ctx, graph);
4605 if (violated < 0)
4606 return isl_stat_error;
4607 if (!violated)
4608 continue;
4609 if (reset_band(graph) < 0)
4610 return isl_stat_error;
4611 use_coincidence = has_coincidence;
4614 return isl_stat_ok;
4617 /* Compute a schedule for a connected dependence graph by considering
4618 * the graph as a whole and return the updated schedule node.
4620 * The actual schedule rows of the current band are computed by
4621 * compute_schedule_wcc_band. compute_schedule_finish_band takes
4622 * care of integrating the band into "node" and continuing
4623 * the computation.
4625 static __isl_give isl_schedule_node *compute_schedule_wcc_whole(
4626 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph)
4628 isl_ctx *ctx;
4630 if (!node)
4631 return NULL;
4633 ctx = isl_schedule_node_get_ctx(node);
4634 if (compute_schedule_wcc_band(ctx, graph) < 0)
4635 return isl_schedule_node_free(node);
4637 return compute_schedule_finish_band(node, graph, 1);
4640 /* Clustering information used by compute_schedule_wcc_clustering.
4642 * "n" is the number of SCCs in the original dependence graph
4643 * "scc" is an array of "n" elements, each representing an SCC
4644 * of the original dependence graph. All entries in the same cluster
4645 * have the same number of schedule rows.
4646 * "scc_cluster" maps each SCC index to the cluster to which it belongs,
4647 * where each cluster is represented by the index of the first SCC
4648 * in the cluster. Initially, each SCC belongs to a cluster containing
4649 * only that SCC.
4651 * "scc_in_merge" is used by merge_clusters_along_edge to keep
4652 * track of which SCCs need to be merged.
4654 * "cluster" contains the merged clusters of SCCs after the clustering
4655 * has completed.
4657 * "scc_node" is a temporary data structure used inside copy_partial.
4658 * For each SCC, it keeps track of the number of nodes in the SCC
4659 * that have already been copied.
4661 struct isl_clustering {
4662 int n;
4663 struct isl_sched_graph *scc;
4664 struct isl_sched_graph *cluster;
4665 int *scc_cluster;
4666 int *scc_node;
4667 int *scc_in_merge;
4670 /* Initialize the clustering data structure "c" from "graph".
4672 * In particular, allocate memory, extract the SCCs from "graph"
4673 * into c->scc, initialize scc_cluster and construct
4674 * a band of schedule rows for each SCC.
4675 * Within each SCC, there is only one SCC by definition.
4676 * Each SCC initially belongs to a cluster containing only that SCC.
4678 static isl_stat clustering_init(isl_ctx *ctx, struct isl_clustering *c,
4679 struct isl_sched_graph *graph)
4681 int i;
4683 c->n = graph->scc;
4684 c->scc = isl_calloc_array(ctx, struct isl_sched_graph, c->n);
4685 c->cluster = isl_calloc_array(ctx, struct isl_sched_graph, c->n);
4686 c->scc_cluster = isl_calloc_array(ctx, int, c->n);
4687 c->scc_node = isl_calloc_array(ctx, int, c->n);
4688 c->scc_in_merge = isl_calloc_array(ctx, int, c->n);
4689 if (!c->scc || !c->cluster ||
4690 !c->scc_cluster || !c->scc_node || !c->scc_in_merge)
4691 return isl_stat_error;
4693 for (i = 0; i < c->n; ++i) {
4694 if (extract_sub_graph(ctx, graph, &node_scc_exactly,
4695 &edge_scc_exactly, i, &c->scc[i]) < 0)
4696 return isl_stat_error;
4697 c->scc[i].scc = 1;
4698 if (compute_maxvar(&c->scc[i]) < 0)
4699 return isl_stat_error;
4700 if (compute_schedule_wcc_band(ctx, &c->scc[i]) < 0)
4701 return isl_stat_error;
4702 c->scc_cluster[i] = i;
4705 return isl_stat_ok;
4708 /* Free all memory allocated for "c".
4710 static void clustering_free(isl_ctx *ctx, struct isl_clustering *c)
4712 int i;
4714 if (c->scc)
4715 for (i = 0; i < c->n; ++i)
4716 graph_free(ctx, &c->scc[i]);
4717 free(c->scc);
4718 if (c->cluster)
4719 for (i = 0; i < c->n; ++i)
4720 graph_free(ctx, &c->cluster[i]);
4721 free(c->cluster);
4722 free(c->scc_cluster);
4723 free(c->scc_node);
4724 free(c->scc_in_merge);
4727 /* Should we refrain from merging the cluster in "graph" with
4728 * any other cluster?
4729 * In particular, is its current schedule band empty and incomplete.
4731 static int bad_cluster(struct isl_sched_graph *graph)
4733 return graph->n_row < graph->maxvar &&
4734 graph->n_total_row == graph->band_start;
4737 /* Return the index of an edge in "graph" that can be used to merge
4738 * two clusters in "c".
4739 * Return graph->n_edge if no such edge can be found.
4740 * Return -1 on error.
4742 * In particular, return a proximity edge between two clusters
4743 * that is not marked "no_merge" and such that neither of the
4744 * two clusters has an incomplete, empty band.
4746 * If there are multiple such edges, then try and find the most
4747 * appropriate edge to use for merging. In particular, pick the edge
4748 * with the greatest weight. If there are multiple of those,
4749 * then pick one with the shortest distance between
4750 * the two cluster representatives.
4752 static int find_proximity(struct isl_sched_graph *graph,
4753 struct isl_clustering *c)
4755 int i, best = graph->n_edge, best_dist, best_weight;
4757 for (i = 0; i < graph->n_edge; ++i) {
4758 struct isl_sched_edge *edge = &graph->edge[i];
4759 int dist, weight;
4761 if (!is_proximity(edge))
4762 continue;
4763 if (edge->no_merge)
4764 continue;
4765 if (bad_cluster(&c->scc[edge->src->scc]) ||
4766 bad_cluster(&c->scc[edge->dst->scc]))
4767 continue;
4768 dist = c->scc_cluster[edge->dst->scc] -
4769 c->scc_cluster[edge->src->scc];
4770 if (dist == 0)
4771 continue;
4772 weight = edge->weight;
4773 if (best < graph->n_edge) {
4774 if (best_weight > weight)
4775 continue;
4776 if (best_weight == weight && best_dist <= dist)
4777 continue;
4779 best = i;
4780 best_dist = dist;
4781 best_weight = weight;
4784 return best;
4787 /* Internal data structure used in mark_merge_sccs.
4789 * "graph" is the dependence graph in which a strongly connected
4790 * component is constructed.
4791 * "scc_cluster" maps each SCC index to the cluster to which it belongs.
4792 * "src" and "dst" are the indices of the nodes that are being merged.
4794 struct isl_mark_merge_sccs_data {
4795 struct isl_sched_graph *graph;
4796 int *scc_cluster;
4797 int src;
4798 int dst;
4801 /* Check whether the cluster containing node "i" depends on the cluster
4802 * containing node "j". If "i" and "j" belong to the same cluster,
4803 * then they are taken to depend on each other to ensure that
4804 * the resulting strongly connected component consists of complete
4805 * clusters. Furthermore, if "i" and "j" are the two nodes that
4806 * are being merged, then they are taken to depend on each other as well.
4807 * Otherwise, check if there is a (conditional) validity dependence
4808 * from node[j] to node[i], forcing node[i] to follow node[j].
4810 static isl_bool cluster_follows(int i, int j, void *user)
4812 struct isl_mark_merge_sccs_data *data = user;
4813 struct isl_sched_graph *graph = data->graph;
4814 int *scc_cluster = data->scc_cluster;
4816 if (data->src == i && data->dst == j)
4817 return isl_bool_true;
4818 if (data->src == j && data->dst == i)
4819 return isl_bool_true;
4820 if (scc_cluster[graph->node[i].scc] == scc_cluster[graph->node[j].scc])
4821 return isl_bool_true;
4823 return graph_has_validity_edge(graph, &graph->node[j], &graph->node[i]);
4826 /* Mark all SCCs that belong to either of the two clusters in "c"
4827 * connected by the edge in "graph" with index "edge", or to any
4828 * of the intermediate clusters.
4829 * The marking is recorded in c->scc_in_merge.
4831 * The given edge has been selected for merging two clusters,
4832 * meaning that there is at least a proximity edge between the two nodes.
4833 * However, there may also be (indirect) validity dependences
4834 * between the two nodes. When merging the two clusters, all clusters
4835 * containing one or more of the intermediate nodes along the
4836 * indirect validity dependences need to be merged in as well.
4838 * First collect all such nodes by computing the strongly connected
4839 * component (SCC) containing the two nodes connected by the edge, where
4840 * the two nodes are considered to depend on each other to make
4841 * sure they end up in the same SCC. Similarly, each node is considered
4842 * to depend on every other node in the same cluster to ensure
4843 * that the SCC consists of complete clusters.
4845 * Then the original SCCs that contain any of these nodes are marked
4846 * in c->scc_in_merge.
4848 static isl_stat mark_merge_sccs(isl_ctx *ctx, struct isl_sched_graph *graph,
4849 int edge, struct isl_clustering *c)
4851 struct isl_mark_merge_sccs_data data;
4852 struct isl_tarjan_graph *g;
4853 int i;
4855 for (i = 0; i < c->n; ++i)
4856 c->scc_in_merge[i] = 0;
4858 data.graph = graph;
4859 data.scc_cluster = c->scc_cluster;
4860 data.src = graph->edge[edge].src - graph->node;
4861 data.dst = graph->edge[edge].dst - graph->node;
4863 g = isl_tarjan_graph_component(ctx, graph->n, data.dst,
4864 &cluster_follows, &data);
4865 if (!g)
4866 goto error;
4868 i = g->op;
4869 if (i < 3)
4870 isl_die(ctx, isl_error_internal,
4871 "expecting at least two nodes in component",
4872 goto error);
4873 if (g->order[--i] != -1)
4874 isl_die(ctx, isl_error_internal,
4875 "expecting end of component marker", goto error);
4877 for (--i; i >= 0 && g->order[i] != -1; --i) {
4878 int scc = graph->node[g->order[i]].scc;
4879 c->scc_in_merge[scc] = 1;
4882 isl_tarjan_graph_free(g);
4883 return isl_stat_ok;
4884 error:
4885 isl_tarjan_graph_free(g);
4886 return isl_stat_error;
4889 /* Construct the identifier "cluster_i".
4891 static __isl_give isl_id *cluster_id(isl_ctx *ctx, int i)
4893 char name[40];
4895 snprintf(name, sizeof(name), "cluster_%d", i);
4896 return isl_id_alloc(ctx, name, NULL);
4899 /* Construct the space of the cluster with index "i" containing
4900 * the strongly connected component "scc".
4902 * In particular, construct a space called cluster_i with dimension equal
4903 * to the number of schedule rows in the current band of "scc".
4905 static __isl_give isl_space *cluster_space(struct isl_sched_graph *scc, int i)
4907 int nvar;
4908 isl_space *space;
4909 isl_id *id;
4911 nvar = scc->n_total_row - scc->band_start;
4912 space = isl_space_copy(scc->node[0].space);
4913 space = isl_space_params(space);
4914 space = isl_space_set_from_params(space);
4915 space = isl_space_add_dims(space, isl_dim_set, nvar);
4916 id = cluster_id(isl_space_get_ctx(space), i);
4917 space = isl_space_set_tuple_id(space, isl_dim_set, id);
4919 return space;
4922 /* Collect the domain of the graph for merging clusters.
4924 * In particular, for each cluster with first SCC "i", construct
4925 * a set in the space called cluster_i with dimension equal
4926 * to the number of schedule rows in the current band of the cluster.
4928 static __isl_give isl_union_set *collect_domain(isl_ctx *ctx,
4929 struct isl_sched_graph *graph, struct isl_clustering *c)
4931 int i;
4932 isl_space *space;
4933 isl_union_set *domain;
4935 space = isl_space_params_alloc(ctx, 0);
4936 domain = isl_union_set_empty(space);
4938 for (i = 0; i < graph->scc; ++i) {
4939 isl_space *space;
4941 if (!c->scc_in_merge[i])
4942 continue;
4943 if (c->scc_cluster[i] != i)
4944 continue;
4945 space = cluster_space(&c->scc[i], i);
4946 domain = isl_union_set_add_set(domain, isl_set_universe(space));
4949 return domain;
4952 /* Construct a map from the original instances to the corresponding
4953 * cluster instance in the current bands of the clusters in "c".
4955 static __isl_give isl_union_map *collect_cluster_map(isl_ctx *ctx,
4956 struct isl_sched_graph *graph, struct isl_clustering *c)
4958 int i, j;
4959 isl_space *space;
4960 isl_union_map *cluster_map;
4962 space = isl_space_params_alloc(ctx, 0);
4963 cluster_map = isl_union_map_empty(space);
4964 for (i = 0; i < graph->scc; ++i) {
4965 int start, n;
4966 isl_id *id;
4968 if (!c->scc_in_merge[i])
4969 continue;
4971 id = cluster_id(ctx, c->scc_cluster[i]);
4972 start = c->scc[i].band_start;
4973 n = c->scc[i].n_total_row - start;
4974 for (j = 0; j < c->scc[i].n; ++j) {
4975 isl_multi_aff *ma;
4976 isl_map *map;
4977 struct isl_sched_node *node = &c->scc[i].node[j];
4979 ma = node_extract_partial_schedule_multi_aff(node,
4980 start, n);
4981 ma = isl_multi_aff_set_tuple_id(ma, isl_dim_out,
4982 isl_id_copy(id));
4983 map = isl_map_from_multi_aff(ma);
4984 cluster_map = isl_union_map_add_map(cluster_map, map);
4986 isl_id_free(id);
4989 return cluster_map;
4992 /* Add "umap" to the schedule constraints "sc" of all types of "edge"
4993 * that are not isl_edge_condition or isl_edge_conditional_validity.
4995 static __isl_give isl_schedule_constraints *add_non_conditional_constraints(
4996 struct isl_sched_edge *edge, __isl_keep isl_union_map *umap,
4997 __isl_take isl_schedule_constraints *sc)
4999 enum isl_edge_type t;
5001 if (!sc)
5002 return NULL;
5004 for (t = isl_edge_first; t <= isl_edge_last; ++t) {
5005 if (t == isl_edge_condition ||
5006 t == isl_edge_conditional_validity)
5007 continue;
5008 if (!is_type(edge, t))
5009 continue;
5010 sc->constraint[t] = isl_union_map_union(sc->constraint[t],
5011 isl_union_map_copy(umap));
5012 if (!sc->constraint[t])
5013 return isl_schedule_constraints_free(sc);
5016 return sc;
5019 /* Add schedule constraints of types isl_edge_condition and
5020 * isl_edge_conditional_validity to "sc" by applying "umap" to
5021 * the domains of the wrapped relations in domain and range
5022 * of the corresponding tagged constraints of "edge".
5024 static __isl_give isl_schedule_constraints *add_conditional_constraints(
5025 struct isl_sched_edge *edge, __isl_keep isl_union_map *umap,
5026 __isl_take isl_schedule_constraints *sc)
5028 enum isl_edge_type t;
5029 isl_union_map *tagged;
5031 for (t = isl_edge_condition; t <= isl_edge_conditional_validity; ++t) {
5032 if (!is_type(edge, t))
5033 continue;
5034 if (t == isl_edge_condition)
5035 tagged = isl_union_map_copy(edge->tagged_condition);
5036 else
5037 tagged = isl_union_map_copy(edge->tagged_validity);
5038 tagged = isl_union_map_zip(tagged);
5039 tagged = isl_union_map_apply_domain(tagged,
5040 isl_union_map_copy(umap));
5041 tagged = isl_union_map_zip(tagged);
5042 sc->constraint[t] = isl_union_map_union(sc->constraint[t],
5043 tagged);
5044 if (!sc->constraint[t])
5045 return isl_schedule_constraints_free(sc);
5048 return sc;
5051 /* Given a mapping "cluster_map" from the original instances to
5052 * the cluster instances, add schedule constraints on the clusters
5053 * to "sc" corresponding to the original constraints represented by "edge".
5055 * For non-tagged dependence constraints, the cluster constraints
5056 * are obtained by applying "cluster_map" to the edge->map.
5058 * For tagged dependence constraints, "cluster_map" needs to be applied
5059 * to the domains of the wrapped relations in domain and range
5060 * of the tagged dependence constraints. Pick out the mappings
5061 * from these domains from "cluster_map" and construct their product.
5062 * This mapping can then be applied to the pair of domains.
5064 static __isl_give isl_schedule_constraints *collect_edge_constraints(
5065 struct isl_sched_edge *edge, __isl_keep isl_union_map *cluster_map,
5066 __isl_take isl_schedule_constraints *sc)
5068 isl_union_map *umap;
5069 isl_space *space;
5070 isl_union_set *uset;
5071 isl_union_map *umap1, *umap2;
5073 if (!sc)
5074 return NULL;
5076 umap = isl_union_map_from_map(isl_map_copy(edge->map));
5077 umap = isl_union_map_apply_domain(umap,
5078 isl_union_map_copy(cluster_map));
5079 umap = isl_union_map_apply_range(umap,
5080 isl_union_map_copy(cluster_map));
5081 sc = add_non_conditional_constraints(edge, umap, sc);
5082 isl_union_map_free(umap);
5084 if (!sc || (!is_condition(edge) && !is_conditional_validity(edge)))
5085 return sc;
5087 space = isl_space_domain(isl_map_get_space(edge->map));
5088 uset = isl_union_set_from_set(isl_set_universe(space));
5089 umap1 = isl_union_map_copy(cluster_map);
5090 umap1 = isl_union_map_intersect_domain(umap1, uset);
5091 space = isl_space_range(isl_map_get_space(edge->map));
5092 uset = isl_union_set_from_set(isl_set_universe(space));
5093 umap2 = isl_union_map_copy(cluster_map);
5094 umap2 = isl_union_map_intersect_domain(umap2, uset);
5095 umap = isl_union_map_product(umap1, umap2);
5097 sc = add_conditional_constraints(edge, umap, sc);
5099 isl_union_map_free(umap);
5100 return sc;
5103 /* Given a mapping "cluster_map" from the original instances to
5104 * the cluster instances, add schedule constraints on the clusters
5105 * to "sc" corresponding to all edges in "graph" between nodes that
5106 * belong to SCCs that are marked for merging in "scc_in_merge".
5108 static __isl_give isl_schedule_constraints *collect_constraints(
5109 struct isl_sched_graph *graph, int *scc_in_merge,
5110 __isl_keep isl_union_map *cluster_map,
5111 __isl_take isl_schedule_constraints *sc)
5113 int i;
5115 for (i = 0; i < graph->n_edge; ++i) {
5116 struct isl_sched_edge *edge = &graph->edge[i];
5118 if (!scc_in_merge[edge->src->scc])
5119 continue;
5120 if (!scc_in_merge[edge->dst->scc])
5121 continue;
5122 sc = collect_edge_constraints(edge, cluster_map, sc);
5125 return sc;
5128 /* Construct a dependence graph for scheduling clusters with respect
5129 * to each other and store the result in "merge_graph".
5130 * In particular, the nodes of the graph correspond to the schedule
5131 * dimensions of the current bands of those clusters that have been
5132 * marked for merging in "c".
5134 * First construct an isl_schedule_constraints object for this domain
5135 * by transforming the edges in "graph" to the domain.
5136 * Then initialize a dependence graph for scheduling from these
5137 * constraints.
5139 static isl_stat init_merge_graph(isl_ctx *ctx, struct isl_sched_graph *graph,
5140 struct isl_clustering *c, struct isl_sched_graph *merge_graph)
5142 isl_union_set *domain;
5143 isl_union_map *cluster_map;
5144 isl_schedule_constraints *sc;
5145 isl_stat r;
5147 domain = collect_domain(ctx, graph, c);
5148 sc = isl_schedule_constraints_on_domain(domain);
5149 if (!sc)
5150 return isl_stat_error;
5151 cluster_map = collect_cluster_map(ctx, graph, c);
5152 sc = collect_constraints(graph, c->scc_in_merge, cluster_map, sc);
5153 isl_union_map_free(cluster_map);
5155 r = graph_init(merge_graph, sc);
5157 isl_schedule_constraints_free(sc);
5159 return r;
5162 /* Compute the maximal number of remaining schedule rows that still need
5163 * to be computed for the nodes that belong to clusters with the maximal
5164 * dimension for the current band (i.e., the band that is to be merged).
5165 * Only clusters that are about to be merged are considered.
5166 * "maxvar" is the maximal dimension for the current band.
5167 * "c" contains information about the clusters.
5169 * Return the maximal number of remaining schedule rows or -1 on error.
5171 static int compute_maxvar_max_slack(int maxvar, struct isl_clustering *c)
5173 int i, j;
5174 int max_slack;
5176 max_slack = 0;
5177 for (i = 0; i < c->n; ++i) {
5178 int nvar;
5179 struct isl_sched_graph *scc;
5181 if (!c->scc_in_merge[i])
5182 continue;
5183 scc = &c->scc[i];
5184 nvar = scc->n_total_row - scc->band_start;
5185 if (nvar != maxvar)
5186 continue;
5187 for (j = 0; j < scc->n; ++j) {
5188 struct isl_sched_node *node = &scc->node[j];
5189 int slack;
5191 if (node_update_cmap(node) < 0)
5192 return -1;
5193 slack = node->nvar - node->rank;
5194 if (slack > max_slack)
5195 max_slack = slack;
5199 return max_slack;
5202 /* If there are any clusters where the dimension of the current band
5203 * (i.e., the band that is to be merged) is smaller than "maxvar" and
5204 * if there are any nodes in such a cluster where the number
5205 * of remaining schedule rows that still need to be computed
5206 * is greater than "max_slack", then return the smallest current band
5207 * dimension of all these clusters. Otherwise return the original value
5208 * of "maxvar". Return -1 in case of any error.
5209 * Only clusters that are about to be merged are considered.
5210 * "c" contains information about the clusters.
5212 static int limit_maxvar_to_slack(int maxvar, int max_slack,
5213 struct isl_clustering *c)
5215 int i, j;
5217 for (i = 0; i < c->n; ++i) {
5218 int nvar;
5219 struct isl_sched_graph *scc;
5221 if (!c->scc_in_merge[i])
5222 continue;
5223 scc = &c->scc[i];
5224 nvar = scc->n_total_row - scc->band_start;
5225 if (nvar >= maxvar)
5226 continue;
5227 for (j = 0; j < scc->n; ++j) {
5228 struct isl_sched_node *node = &scc->node[j];
5229 int slack;
5231 if (node_update_cmap(node) < 0)
5232 return -1;
5233 slack = node->nvar - node->rank;
5234 if (slack > max_slack) {
5235 maxvar = nvar;
5236 break;
5241 return maxvar;
5244 /* Adjust merge_graph->maxvar based on the number of remaining schedule rows
5245 * that still need to be computed. In particular, if there is a node
5246 * in a cluster where the dimension of the current band is smaller
5247 * than merge_graph->maxvar, but the number of remaining schedule rows
5248 * is greater than that of any node in a cluster with the maximal
5249 * dimension for the current band (i.e., merge_graph->maxvar),
5250 * then adjust merge_graph->maxvar to the (smallest) current band dimension
5251 * of those clusters. Without this adjustment, the total number of
5252 * schedule dimensions would be increased, resulting in a skewed view
5253 * of the number of coincident dimensions.
5254 * "c" contains information about the clusters.
5256 * If the maximize_band_depth option is set and merge_graph->maxvar is reduced,
5257 * then there is no point in attempting any merge since it will be rejected
5258 * anyway. Set merge_graph->maxvar to zero in such cases.
5260 static isl_stat adjust_maxvar_to_slack(isl_ctx *ctx,
5261 struct isl_sched_graph *merge_graph, struct isl_clustering *c)
5263 int max_slack, maxvar;
5265 max_slack = compute_maxvar_max_slack(merge_graph->maxvar, c);
5266 if (max_slack < 0)
5267 return isl_stat_error;
5268 maxvar = limit_maxvar_to_slack(merge_graph->maxvar, max_slack, c);
5269 if (maxvar < 0)
5270 return isl_stat_error;
5272 if (maxvar < merge_graph->maxvar) {
5273 if (isl_options_get_schedule_maximize_band_depth(ctx))
5274 merge_graph->maxvar = 0;
5275 else
5276 merge_graph->maxvar = maxvar;
5279 return isl_stat_ok;
5282 /* Return the number of coincident dimensions in the current band of "graph",
5283 * where the nodes of "graph" are assumed to be scheduled by a single band.
5285 static int get_n_coincident(struct isl_sched_graph *graph)
5287 int i;
5289 for (i = graph->band_start; i < graph->n_total_row; ++i)
5290 if (!graph->node[0].coincident[i])
5291 break;
5293 return i - graph->band_start;
5296 /* Should the clusters be merged based on the cluster schedule
5297 * in the current (and only) band of "merge_graph", given that
5298 * coincidence should be maximized?
5300 * If the number of coincident schedule dimensions in the merged band
5301 * would be less than the maximal number of coincident schedule dimensions
5302 * in any of the merged clusters, then the clusters should not be merged.
5304 static isl_bool ok_to_merge_coincident(struct isl_clustering *c,
5305 struct isl_sched_graph *merge_graph)
5307 int i;
5308 int n_coincident;
5309 int max_coincident;
5311 max_coincident = 0;
5312 for (i = 0; i < c->n; ++i) {
5313 if (!c->scc_in_merge[i])
5314 continue;
5315 n_coincident = get_n_coincident(&c->scc[i]);
5316 if (n_coincident > max_coincident)
5317 max_coincident = n_coincident;
5320 n_coincident = get_n_coincident(merge_graph);
5322 return n_coincident >= max_coincident;
5325 /* Return the transformation on "node" expressed by the current (and only)
5326 * band of "merge_graph" applied to the clusters in "c".
5328 * First find the representation of "node" in its SCC in "c" and
5329 * extract the transformation expressed by the current band.
5330 * Then extract the transformation applied by "merge_graph"
5331 * to the cluster to which this SCC belongs.
5332 * Combine the two to obtain the complete transformation on the node.
5334 * Note that the range of the first transformation is an anonymous space,
5335 * while the domain of the second is named "cluster_X". The range
5336 * of the former therefore needs to be adjusted before the two
5337 * can be combined.
5339 static __isl_give isl_map *extract_node_transformation(isl_ctx *ctx,
5340 struct isl_sched_node *node, struct isl_clustering *c,
5341 struct isl_sched_graph *merge_graph)
5343 struct isl_sched_node *scc_node, *cluster_node;
5344 int start, n;
5345 isl_id *id;
5346 isl_space *space;
5347 isl_multi_aff *ma, *ma2;
5349 scc_node = graph_find_node(ctx, &c->scc[node->scc], node->space);
5350 start = c->scc[node->scc].band_start;
5351 n = c->scc[node->scc].n_total_row - start;
5352 ma = node_extract_partial_schedule_multi_aff(scc_node, start, n);
5353 space = cluster_space(&c->scc[node->scc], c->scc_cluster[node->scc]);
5354 cluster_node = graph_find_node(ctx, merge_graph, space);
5355 if (space && !cluster_node)
5356 isl_die(ctx, isl_error_internal, "unable to find cluster",
5357 space = isl_space_free(space));
5358 id = isl_space_get_tuple_id(space, isl_dim_set);
5359 ma = isl_multi_aff_set_tuple_id(ma, isl_dim_out, id);
5360 isl_space_free(space);
5361 n = merge_graph->n_total_row;
5362 ma2 = node_extract_partial_schedule_multi_aff(cluster_node, 0, n);
5363 ma = isl_multi_aff_pullback_multi_aff(ma2, ma);
5365 return isl_map_from_multi_aff(ma);
5368 /* Give a set of distances "set", are they bounded by a small constant
5369 * in direction "pos"?
5370 * In practice, check if they are bounded by 2 by checking that there
5371 * are no elements with a value greater than or equal to 3 or
5372 * smaller than or equal to -3.
5374 static isl_bool distance_is_bounded(__isl_keep isl_set *set, int pos)
5376 isl_bool bounded;
5377 isl_set *test;
5379 if (!set)
5380 return isl_bool_error;
5382 test = isl_set_copy(set);
5383 test = isl_set_lower_bound_si(test, isl_dim_set, pos, 3);
5384 bounded = isl_set_is_empty(test);
5385 isl_set_free(test);
5387 if (bounded < 0 || !bounded)
5388 return bounded;
5390 test = isl_set_copy(set);
5391 test = isl_set_upper_bound_si(test, isl_dim_set, pos, -3);
5392 bounded = isl_set_is_empty(test);
5393 isl_set_free(test);
5395 return bounded;
5398 /* Does the set "set" have a fixed (but possible parametric) value
5399 * at dimension "pos"?
5401 static isl_bool has_single_value(__isl_keep isl_set *set, int pos)
5403 int n;
5404 isl_bool single;
5406 if (!set)
5407 return isl_bool_error;
5408 set = isl_set_copy(set);
5409 n = isl_set_dim(set, isl_dim_set);
5410 set = isl_set_project_out(set, isl_dim_set, pos + 1, n - (pos + 1));
5411 set = isl_set_project_out(set, isl_dim_set, 0, pos);
5412 single = isl_set_is_singleton(set);
5413 isl_set_free(set);
5415 return single;
5418 /* Does "map" have a fixed (but possible parametric) value
5419 * at dimension "pos" of either its domain or its range?
5421 static isl_bool has_singular_src_or_dst(__isl_keep isl_map *map, int pos)
5423 isl_set *set;
5424 isl_bool single;
5426 set = isl_map_domain(isl_map_copy(map));
5427 single = has_single_value(set, pos);
5428 isl_set_free(set);
5430 if (single < 0 || single)
5431 return single;
5433 set = isl_map_range(isl_map_copy(map));
5434 single = has_single_value(set, pos);
5435 isl_set_free(set);
5437 return single;
5440 /* Does the edge "edge" from "graph" have bounded dependence distances
5441 * in the merged graph "merge_graph" of a selection of clusters in "c"?
5443 * Extract the complete transformations of the source and destination
5444 * nodes of the edge, apply them to the edge constraints and
5445 * compute the differences. Finally, check if these differences are bounded
5446 * in each direction.
5448 * If the dimension of the band is greater than the number of
5449 * dimensions that can be expected to be optimized by the edge
5450 * (based on its weight), then also allow the differences to be unbounded
5451 * in the remaining dimensions, but only if either the source or
5452 * the destination has a fixed value in that direction.
5453 * This allows a statement that produces values that are used by
5454 * several instance of another statement to be merged with that
5455 * other statement.
5456 * However, merging such clusters will introduce an inherently
5457 * large proximity distance inside the merged cluster, meaning
5458 * that proximity distances will no longer be optimized in
5459 * subsequent merges. These merges are therefore only allowed
5460 * after all other possible merges have been tried.
5461 * The first time such a merge is encountered, the weight of the edge
5462 * is replaced by a negative weight. The second time (i.e., after
5463 * all merges over edges with a non-negative weight have been tried),
5464 * the merge is allowed.
5466 static isl_bool has_bounded_distances(isl_ctx *ctx, struct isl_sched_edge *edge,
5467 struct isl_sched_graph *graph, struct isl_clustering *c,
5468 struct isl_sched_graph *merge_graph)
5470 int i, n, n_slack;
5471 isl_bool bounded;
5472 isl_map *map, *t;
5473 isl_set *dist;
5475 map = isl_map_copy(edge->map);
5476 t = extract_node_transformation(ctx, edge->src, c, merge_graph);
5477 map = isl_map_apply_domain(map, t);
5478 t = extract_node_transformation(ctx, edge->dst, c, merge_graph);
5479 map = isl_map_apply_range(map, t);
5480 dist = isl_map_deltas(isl_map_copy(map));
5482 bounded = isl_bool_true;
5483 n = isl_set_dim(dist, isl_dim_set);
5484 n_slack = n - edge->weight;
5485 if (edge->weight < 0)
5486 n_slack -= graph->max_weight + 1;
5487 for (i = 0; i < n; ++i) {
5488 isl_bool bounded_i, singular_i;
5490 bounded_i = distance_is_bounded(dist, i);
5491 if (bounded_i < 0)
5492 goto error;
5493 if (bounded_i)
5494 continue;
5495 if (edge->weight >= 0)
5496 bounded = isl_bool_false;
5497 n_slack--;
5498 if (n_slack < 0)
5499 break;
5500 singular_i = has_singular_src_or_dst(map, i);
5501 if (singular_i < 0)
5502 goto error;
5503 if (singular_i)
5504 continue;
5505 bounded = isl_bool_false;
5506 break;
5508 if (!bounded && i >= n && edge->weight >= 0)
5509 edge->weight -= graph->max_weight + 1;
5510 isl_map_free(map);
5511 isl_set_free(dist);
5513 return bounded;
5514 error:
5515 isl_map_free(map);
5516 isl_set_free(dist);
5517 return isl_bool_error;
5520 /* Should the clusters be merged based on the cluster schedule
5521 * in the current (and only) band of "merge_graph"?
5522 * "graph" is the original dependence graph, while "c" records
5523 * which SCCs are involved in the latest merge.
5525 * In particular, is there at least one proximity constraint
5526 * that is optimized by the merge?
5528 * A proximity constraint is considered to be optimized
5529 * if the dependence distances are small.
5531 static isl_bool ok_to_merge_proximity(isl_ctx *ctx,
5532 struct isl_sched_graph *graph, struct isl_clustering *c,
5533 struct isl_sched_graph *merge_graph)
5535 int i;
5537 for (i = 0; i < graph->n_edge; ++i) {
5538 struct isl_sched_edge *edge = &graph->edge[i];
5539 isl_bool bounded;
5541 if (!is_proximity(edge))
5542 continue;
5543 if (!c->scc_in_merge[edge->src->scc])
5544 continue;
5545 if (!c->scc_in_merge[edge->dst->scc])
5546 continue;
5547 if (c->scc_cluster[edge->dst->scc] ==
5548 c->scc_cluster[edge->src->scc])
5549 continue;
5550 bounded = has_bounded_distances(ctx, edge, graph, c,
5551 merge_graph);
5552 if (bounded < 0 || bounded)
5553 return bounded;
5556 return isl_bool_false;
5559 /* Should the clusters be merged based on the cluster schedule
5560 * in the current (and only) band of "merge_graph"?
5561 * "graph" is the original dependence graph, while "c" records
5562 * which SCCs are involved in the latest merge.
5564 * If the current band is empty, then the clusters should not be merged.
5566 * If the band depth should be maximized and the merge schedule
5567 * is incomplete (meaning that the dimension of some of the schedule
5568 * bands in the original schedule will be reduced), then the clusters
5569 * should not be merged.
5571 * If the schedule_maximize_coincidence option is set, then check that
5572 * the number of coincident schedule dimensions is not reduced.
5574 * Finally, only allow the merge if at least one proximity
5575 * constraint is optimized.
5577 static isl_bool ok_to_merge(isl_ctx *ctx, struct isl_sched_graph *graph,
5578 struct isl_clustering *c, struct isl_sched_graph *merge_graph)
5580 if (merge_graph->n_total_row == merge_graph->band_start)
5581 return isl_bool_false;
5583 if (isl_options_get_schedule_maximize_band_depth(ctx) &&
5584 merge_graph->n_total_row < merge_graph->maxvar)
5585 return isl_bool_false;
5587 if (isl_options_get_schedule_maximize_coincidence(ctx)) {
5588 isl_bool ok;
5590 ok = ok_to_merge_coincident(c, merge_graph);
5591 if (ok < 0 || !ok)
5592 return ok;
5595 return ok_to_merge_proximity(ctx, graph, c, merge_graph);
5598 /* Apply the schedule in "t_node" to the "n" rows starting at "first"
5599 * of the schedule in "node" and return the result.
5601 * That is, essentially compute
5603 * T * N(first:first+n-1)
5605 * taking into account the constant term and the parameter coefficients
5606 * in "t_node".
5608 static __isl_give isl_mat *node_transformation(isl_ctx *ctx,
5609 struct isl_sched_node *t_node, struct isl_sched_node *node,
5610 int first, int n)
5612 int i, j;
5613 isl_mat *t;
5614 int n_row, n_col, n_param, n_var;
5616 n_param = node->nparam;
5617 n_var = node->nvar;
5618 n_row = isl_mat_rows(t_node->sched);
5619 n_col = isl_mat_cols(node->sched);
5620 t = isl_mat_alloc(ctx, n_row, n_col);
5621 if (!t)
5622 return NULL;
5623 for (i = 0; i < n_row; ++i) {
5624 isl_seq_cpy(t->row[i], t_node->sched->row[i], 1 + n_param);
5625 isl_seq_clr(t->row[i] + 1 + n_param, n_var);
5626 for (j = 0; j < n; ++j)
5627 isl_seq_addmul(t->row[i],
5628 t_node->sched->row[i][1 + n_param + j],
5629 node->sched->row[first + j],
5630 1 + n_param + n_var);
5632 return t;
5635 /* Apply the cluster schedule in "t_node" to the current band
5636 * schedule of the nodes in "graph".
5638 * In particular, replace the rows starting at band_start
5639 * by the result of applying the cluster schedule in "t_node"
5640 * to the original rows.
5642 * The coincidence of the schedule is determined by the coincidence
5643 * of the cluster schedule.
5645 static isl_stat transform(isl_ctx *ctx, struct isl_sched_graph *graph,
5646 struct isl_sched_node *t_node)
5648 int i, j;
5649 int n_new;
5650 int start, n;
5652 start = graph->band_start;
5653 n = graph->n_total_row - start;
5655 n_new = isl_mat_rows(t_node->sched);
5656 for (i = 0; i < graph->n; ++i) {
5657 struct isl_sched_node *node = &graph->node[i];
5658 isl_mat *t;
5660 t = node_transformation(ctx, t_node, node, start, n);
5661 node->sched = isl_mat_drop_rows(node->sched, start, n);
5662 node->sched = isl_mat_concat(node->sched, t);
5663 node->sched_map = isl_map_free(node->sched_map);
5664 if (!node->sched)
5665 return isl_stat_error;
5666 for (j = 0; j < n_new; ++j)
5667 node->coincident[start + j] = t_node->coincident[j];
5669 graph->n_total_row -= n;
5670 graph->n_row -= n;
5671 graph->n_total_row += n_new;
5672 graph->n_row += n_new;
5674 return isl_stat_ok;
5677 /* Merge the clusters marked for merging in "c" into a single
5678 * cluster using the cluster schedule in the current band of "merge_graph".
5679 * The representative SCC for the new cluster is the SCC with
5680 * the smallest index.
5682 * The current band schedule of each SCC in the new cluster is obtained
5683 * by applying the schedule of the corresponding original cluster
5684 * to the original band schedule.
5685 * All SCCs in the new cluster have the same number of schedule rows.
5687 static isl_stat merge(isl_ctx *ctx, struct isl_clustering *c,
5688 struct isl_sched_graph *merge_graph)
5690 int i;
5691 int cluster = -1;
5692 isl_space *space;
5694 for (i = 0; i < c->n; ++i) {
5695 struct isl_sched_node *node;
5697 if (!c->scc_in_merge[i])
5698 continue;
5699 if (cluster < 0)
5700 cluster = i;
5701 space = cluster_space(&c->scc[i], c->scc_cluster[i]);
5702 if (!space)
5703 return isl_stat_error;
5704 node = graph_find_node(ctx, merge_graph, space);
5705 isl_space_free(space);
5706 if (!node)
5707 isl_die(ctx, isl_error_internal,
5708 "unable to find cluster",
5709 return isl_stat_error);
5710 if (transform(ctx, &c->scc[i], node) < 0)
5711 return isl_stat_error;
5712 c->scc_cluster[i] = cluster;
5715 return isl_stat_ok;
5718 /* Try and merge the clusters of SCCs marked in c->scc_in_merge
5719 * by scheduling the current cluster bands with respect to each other.
5721 * Construct a dependence graph with a space for each cluster and
5722 * with the coordinates of each space corresponding to the schedule
5723 * dimensions of the current band of that cluster.
5724 * Construct a cluster schedule in this cluster dependence graph and
5725 * apply it to the current cluster bands if it is applicable
5726 * according to ok_to_merge.
5728 * If the number of remaining schedule dimensions in a cluster
5729 * with a non-maximal current schedule dimension is greater than
5730 * the number of remaining schedule dimensions in clusters
5731 * with a maximal current schedule dimension, then restrict
5732 * the number of rows to be computed in the cluster schedule
5733 * to the minimal such non-maximal current schedule dimension.
5734 * Do this by adjusting merge_graph.maxvar.
5736 * Return isl_bool_true if the clusters have effectively been merged
5737 * into a single cluster.
5739 * Note that since the standard scheduling algorithm minimizes the maximal
5740 * distance over proximity constraints, the proximity constraints between
5741 * the merged clusters may not be optimized any further than what is
5742 * sufficient to bring the distances within the limits of the internal
5743 * proximity constraints inside the individual clusters.
5744 * It may therefore make sense to perform an additional translation step
5745 * to bring the clusters closer to each other, while maintaining
5746 * the linear part of the merging schedule found using the standard
5747 * scheduling algorithm.
5749 static isl_bool try_merge(isl_ctx *ctx, struct isl_sched_graph *graph,
5750 struct isl_clustering *c)
5752 struct isl_sched_graph merge_graph = { 0 };
5753 isl_bool merged;
5755 if (init_merge_graph(ctx, graph, c, &merge_graph) < 0)
5756 goto error;
5758 if (compute_maxvar(&merge_graph) < 0)
5759 goto error;
5760 if (adjust_maxvar_to_slack(ctx, &merge_graph,c) < 0)
5761 goto error;
5762 if (compute_schedule_wcc_band(ctx, &merge_graph) < 0)
5763 goto error;
5764 merged = ok_to_merge(ctx, graph, c, &merge_graph);
5765 if (merged && merge(ctx, c, &merge_graph) < 0)
5766 goto error;
5768 graph_free(ctx, &merge_graph);
5769 return merged;
5770 error:
5771 graph_free(ctx, &merge_graph);
5772 return isl_bool_error;
5775 /* Is there any edge marked "no_merge" between two SCCs that are
5776 * about to be merged (i.e., that are set in "scc_in_merge")?
5777 * "merge_edge" is the proximity edge along which the clusters of SCCs
5778 * are going to be merged.
5780 * If there is any edge between two SCCs with a negative weight,
5781 * while the weight of "merge_edge" is non-negative, then this
5782 * means that the edge was postponed. "merge_edge" should then
5783 * also be postponed since merging along the edge with negative weight should
5784 * be postponed until all edges with non-negative weight have been tried.
5785 * Replace the weight of "merge_edge" by a negative weight as well and
5786 * tell the caller not to attempt a merge.
5788 static int any_no_merge(struct isl_sched_graph *graph, int *scc_in_merge,
5789 struct isl_sched_edge *merge_edge)
5791 int i;
5793 for (i = 0; i < graph->n_edge; ++i) {
5794 struct isl_sched_edge *edge = &graph->edge[i];
5796 if (!scc_in_merge[edge->src->scc])
5797 continue;
5798 if (!scc_in_merge[edge->dst->scc])
5799 continue;
5800 if (edge->no_merge)
5801 return 1;
5802 if (merge_edge->weight >= 0 && edge->weight < 0) {
5803 merge_edge->weight -= graph->max_weight + 1;
5804 return 1;
5808 return 0;
5811 /* Merge the two clusters in "c" connected by the edge in "graph"
5812 * with index "edge" into a single cluster.
5813 * If it turns out to be impossible to merge these two clusters,
5814 * then mark the edge as "no_merge" such that it will not be
5815 * considered again.
5817 * First mark all SCCs that need to be merged. This includes the SCCs
5818 * in the two clusters, but it may also include the SCCs
5819 * of intermediate clusters.
5820 * If there is already a no_merge edge between any pair of such SCCs,
5821 * then simply mark the current edge as no_merge as well.
5822 * Likewise, if any of those edges was postponed by has_bounded_distances,
5823 * then postpone the current edge as well.
5824 * Otherwise, try and merge the clusters and mark "edge" as "no_merge"
5825 * if the clusters did not end up getting merged, unless the non-merge
5826 * is due to the fact that the edge was postponed. This postponement
5827 * can be recognized by a change in weight (from non-negative to negative).
5829 static isl_stat merge_clusters_along_edge(isl_ctx *ctx,
5830 struct isl_sched_graph *graph, int edge, struct isl_clustering *c)
5832 isl_bool merged;
5833 int edge_weight = graph->edge[edge].weight;
5835 if (mark_merge_sccs(ctx, graph, edge, c) < 0)
5836 return isl_stat_error;
5838 if (any_no_merge(graph, c->scc_in_merge, &graph->edge[edge]))
5839 merged = isl_bool_false;
5840 else
5841 merged = try_merge(ctx, graph, c);
5842 if (merged < 0)
5843 return isl_stat_error;
5844 if (!merged && edge_weight == graph->edge[edge].weight)
5845 graph->edge[edge].no_merge = 1;
5847 return isl_stat_ok;
5850 /* Does "node" belong to the cluster identified by "cluster"?
5852 static int node_cluster_exactly(struct isl_sched_node *node, int cluster)
5854 return node->cluster == cluster;
5857 /* Does "edge" connect two nodes belonging to the cluster
5858 * identified by "cluster"?
5860 static int edge_cluster_exactly(struct isl_sched_edge *edge, int cluster)
5862 return edge->src->cluster == cluster && edge->dst->cluster == cluster;
5865 /* Swap the schedule of "node1" and "node2".
5866 * Both nodes have been derived from the same node in a common parent graph.
5867 * Since the "coincident" field is shared with that node
5868 * in the parent graph, there is no need to also swap this field.
5870 static void swap_sched(struct isl_sched_node *node1,
5871 struct isl_sched_node *node2)
5873 isl_mat *sched;
5874 isl_map *sched_map;
5876 sched = node1->sched;
5877 node1->sched = node2->sched;
5878 node2->sched = sched;
5880 sched_map = node1->sched_map;
5881 node1->sched_map = node2->sched_map;
5882 node2->sched_map = sched_map;
5885 /* Copy the current band schedule from the SCCs that form the cluster
5886 * with index "pos" to the actual cluster at position "pos".
5887 * By construction, the index of the first SCC that belongs to the cluster
5888 * is also "pos".
5890 * The order of the nodes inside both the SCCs and the cluster
5891 * is assumed to be same as the order in the original "graph".
5893 * Since the SCC graphs will no longer be used after this function,
5894 * the schedules are actually swapped rather than copied.
5896 static isl_stat copy_partial(struct isl_sched_graph *graph,
5897 struct isl_clustering *c, int pos)
5899 int i, j;
5901 c->cluster[pos].n_total_row = c->scc[pos].n_total_row;
5902 c->cluster[pos].n_row = c->scc[pos].n_row;
5903 c->cluster[pos].maxvar = c->scc[pos].maxvar;
5904 j = 0;
5905 for (i = 0; i < graph->n; ++i) {
5906 int k;
5907 int s;
5909 if (graph->node[i].cluster != pos)
5910 continue;
5911 s = graph->node[i].scc;
5912 k = c->scc_node[s]++;
5913 swap_sched(&c->cluster[pos].node[j], &c->scc[s].node[k]);
5914 if (c->scc[s].maxvar > c->cluster[pos].maxvar)
5915 c->cluster[pos].maxvar = c->scc[s].maxvar;
5916 ++j;
5919 return isl_stat_ok;
5922 /* Is there a (conditional) validity dependence from node[j] to node[i],
5923 * forcing node[i] to follow node[j] or do the nodes belong to the same
5924 * cluster?
5926 static isl_bool node_follows_strong_or_same_cluster(int i, int j, void *user)
5928 struct isl_sched_graph *graph = user;
5930 if (graph->node[i].cluster == graph->node[j].cluster)
5931 return isl_bool_true;
5932 return graph_has_validity_edge(graph, &graph->node[j], &graph->node[i]);
5935 /* Extract the merged clusters of SCCs in "graph", sort them, and
5936 * store them in c->clusters. Update c->scc_cluster accordingly.
5938 * First keep track of the cluster containing the SCC to which a node
5939 * belongs in the node itself.
5940 * Then extract the clusters into c->clusters, copying the current
5941 * band schedule from the SCCs that belong to the cluster.
5942 * Do this only once per cluster.
5944 * Finally, topologically sort the clusters and update c->scc_cluster
5945 * to match the new scc numbering. While the SCCs were originally
5946 * sorted already, some SCCs that depend on some other SCCs may
5947 * have been merged with SCCs that appear before these other SCCs.
5948 * A reordering may therefore be required.
5950 static isl_stat extract_clusters(isl_ctx *ctx, struct isl_sched_graph *graph,
5951 struct isl_clustering *c)
5953 int i;
5955 for (i = 0; i < graph->n; ++i)
5956 graph->node[i].cluster = c->scc_cluster[graph->node[i].scc];
5958 for (i = 0; i < graph->scc; ++i) {
5959 if (c->scc_cluster[i] != i)
5960 continue;
5961 if (extract_sub_graph(ctx, graph, &node_cluster_exactly,
5962 &edge_cluster_exactly, i, &c->cluster[i]) < 0)
5963 return isl_stat_error;
5964 c->cluster[i].src_scc = -1;
5965 c->cluster[i].dst_scc = -1;
5966 if (copy_partial(graph, c, i) < 0)
5967 return isl_stat_error;
5970 if (detect_ccs(ctx, graph, &node_follows_strong_or_same_cluster) < 0)
5971 return isl_stat_error;
5972 for (i = 0; i < graph->n; ++i)
5973 c->scc_cluster[graph->node[i].scc] = graph->node[i].cluster;
5975 return isl_stat_ok;
5978 /* Compute weights on the proximity edges of "graph" that can
5979 * be used by find_proximity to find the most appropriate
5980 * proximity edge to use to merge two clusters in "c".
5981 * The weights are also used by has_bounded_distances to determine
5982 * whether the merge should be allowed.
5983 * Store the maximum of the computed weights in graph->max_weight.
5985 * The computed weight is a measure for the number of remaining schedule
5986 * dimensions that can still be completely aligned.
5987 * In particular, compute the number of equalities between
5988 * input dimensions and output dimensions in the proximity constraints.
5989 * The directions that are already handled by outer schedule bands
5990 * are projected out prior to determining this number.
5992 * Edges that will never be considered by find_proximity are ignored.
5994 static isl_stat compute_weights(struct isl_sched_graph *graph,
5995 struct isl_clustering *c)
5997 int i;
5999 graph->max_weight = 0;
6001 for (i = 0; i < graph->n_edge; ++i) {
6002 struct isl_sched_edge *edge = &graph->edge[i];
6003 struct isl_sched_node *src = edge->src;
6004 struct isl_sched_node *dst = edge->dst;
6005 isl_basic_map *hull;
6006 int n_in, n_out;
6008 if (!is_proximity(edge))
6009 continue;
6010 if (bad_cluster(&c->scc[edge->src->scc]) ||
6011 bad_cluster(&c->scc[edge->dst->scc]))
6012 continue;
6013 if (c->scc_cluster[edge->dst->scc] ==
6014 c->scc_cluster[edge->src->scc])
6015 continue;
6017 hull = isl_map_affine_hull(isl_map_copy(edge->map));
6018 hull = isl_basic_map_transform_dims(hull, isl_dim_in, 0,
6019 isl_mat_copy(src->ctrans));
6020 hull = isl_basic_map_transform_dims(hull, isl_dim_out, 0,
6021 isl_mat_copy(dst->ctrans));
6022 hull = isl_basic_map_project_out(hull,
6023 isl_dim_in, 0, src->rank);
6024 hull = isl_basic_map_project_out(hull,
6025 isl_dim_out, 0, dst->rank);
6026 hull = isl_basic_map_remove_divs(hull);
6027 n_in = isl_basic_map_dim(hull, isl_dim_in);
6028 n_out = isl_basic_map_dim(hull, isl_dim_out);
6029 hull = isl_basic_map_drop_constraints_not_involving_dims(hull,
6030 isl_dim_in, 0, n_in);
6031 hull = isl_basic_map_drop_constraints_not_involving_dims(hull,
6032 isl_dim_out, 0, n_out);
6033 if (!hull)
6034 return isl_stat_error;
6035 edge->weight = hull->n_eq;
6036 isl_basic_map_free(hull);
6038 if (edge->weight > graph->max_weight)
6039 graph->max_weight = edge->weight;
6042 return isl_stat_ok;
6045 /* Call compute_schedule_finish_band on each of the clusters in "c"
6046 * in their topological order. This order is determined by the scc
6047 * fields of the nodes in "graph".
6048 * Combine the results in a sequence expressing the topological order.
6050 * If there is only one cluster left, then there is no need to introduce
6051 * a sequence node. Also, in this case, the cluster necessarily contains
6052 * the SCC at position 0 in the original graph and is therefore also
6053 * stored in the first cluster of "c".
6055 static __isl_give isl_schedule_node *finish_bands_clustering(
6056 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph,
6057 struct isl_clustering *c)
6059 int i;
6060 isl_ctx *ctx;
6061 isl_union_set_list *filters;
6063 if (graph->scc == 1)
6064 return compute_schedule_finish_band(node, &c->cluster[0], 0);
6066 ctx = isl_schedule_node_get_ctx(node);
6068 filters = extract_sccs(ctx, graph);
6069 node = isl_schedule_node_insert_sequence(node, filters);
6071 for (i = 0; i < graph->scc; ++i) {
6072 int j = c->scc_cluster[i];
6073 node = isl_schedule_node_child(node, i);
6074 node = isl_schedule_node_child(node, 0);
6075 node = compute_schedule_finish_band(node, &c->cluster[j], 0);
6076 node = isl_schedule_node_parent(node);
6077 node = isl_schedule_node_parent(node);
6080 return node;
6083 /* Compute a schedule for a connected dependence graph by first considering
6084 * each strongly connected component (SCC) in the graph separately and then
6085 * incrementally combining them into clusters.
6086 * Return the updated schedule node.
6088 * Initially, each cluster consists of a single SCC, each with its
6089 * own band schedule. The algorithm then tries to merge pairs
6090 * of clusters along a proximity edge until no more suitable
6091 * proximity edges can be found. During this merging, the schedule
6092 * is maintained in the individual SCCs.
6093 * After the merging is completed, the full resulting clusters
6094 * are extracted and in finish_bands_clustering,
6095 * compute_schedule_finish_band is called on each of them to integrate
6096 * the band into "node" and to continue the computation.
6098 * compute_weights initializes the weights that are used by find_proximity.
6100 static __isl_give isl_schedule_node *compute_schedule_wcc_clustering(
6101 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph)
6103 isl_ctx *ctx;
6104 struct isl_clustering c;
6105 int i;
6107 ctx = isl_schedule_node_get_ctx(node);
6109 if (clustering_init(ctx, &c, graph) < 0)
6110 goto error;
6112 if (compute_weights(graph, &c) < 0)
6113 goto error;
6115 for (;;) {
6116 i = find_proximity(graph, &c);
6117 if (i < 0)
6118 goto error;
6119 if (i >= graph->n_edge)
6120 break;
6121 if (merge_clusters_along_edge(ctx, graph, i, &c) < 0)
6122 goto error;
6125 if (extract_clusters(ctx, graph, &c) < 0)
6126 goto error;
6128 node = finish_bands_clustering(node, graph, &c);
6130 clustering_free(ctx, &c);
6131 return node;
6132 error:
6133 clustering_free(ctx, &c);
6134 return isl_schedule_node_free(node);
6137 /* Compute a schedule for a connected dependence graph and return
6138 * the updated schedule node.
6140 * If Feautrier's algorithm is selected, we first recursively try to satisfy
6141 * as many validity dependences as possible. When all validity dependences
6142 * are satisfied we extend the schedule to a full-dimensional schedule.
6144 * Call compute_schedule_wcc_whole or compute_schedule_wcc_clustering
6145 * depending on whether the user has selected the option to try and
6146 * compute a schedule for the entire (weakly connected) component first.
6147 * If there is only a single strongly connected component (SCC), then
6148 * there is no point in trying to combine SCCs
6149 * in compute_schedule_wcc_clustering, so compute_schedule_wcc_whole
6150 * is called instead.
6152 static __isl_give isl_schedule_node *compute_schedule_wcc(
6153 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph)
6155 isl_ctx *ctx;
6157 if (!node)
6158 return NULL;
6160 ctx = isl_schedule_node_get_ctx(node);
6161 if (detect_sccs(ctx, graph) < 0)
6162 return isl_schedule_node_free(node);
6164 if (compute_maxvar(graph) < 0)
6165 return isl_schedule_node_free(node);
6167 if (need_feautrier_step(ctx, graph))
6168 return compute_schedule_wcc_feautrier(node, graph);
6170 if (graph->scc <= 1 || isl_options_get_schedule_whole_component(ctx))
6171 return compute_schedule_wcc_whole(node, graph);
6172 else
6173 return compute_schedule_wcc_clustering(node, graph);
6176 /* Compute a schedule for each group of nodes identified by node->scc
6177 * separately and then combine them in a sequence node (or as set node
6178 * if graph->weak is set) inserted at position "node" of the schedule tree.
6179 * Return the updated schedule node.
6181 * If "wcc" is set then each of the groups belongs to a single
6182 * weakly connected component in the dependence graph so that
6183 * there is no need for compute_sub_schedule to look for weakly
6184 * connected components.
6186 static __isl_give isl_schedule_node *compute_component_schedule(
6187 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph,
6188 int wcc)
6190 int component;
6191 isl_ctx *ctx;
6192 isl_union_set_list *filters;
6194 if (!node)
6195 return NULL;
6196 ctx = isl_schedule_node_get_ctx(node);
6198 filters = extract_sccs(ctx, graph);
6199 if (graph->weak)
6200 node = isl_schedule_node_insert_set(node, filters);
6201 else
6202 node = isl_schedule_node_insert_sequence(node, filters);
6204 for (component = 0; component < graph->scc; ++component) {
6205 node = isl_schedule_node_child(node, component);
6206 node = isl_schedule_node_child(node, 0);
6207 node = compute_sub_schedule(node, ctx, graph,
6208 &node_scc_exactly,
6209 &edge_scc_exactly, component, wcc);
6210 node = isl_schedule_node_parent(node);
6211 node = isl_schedule_node_parent(node);
6214 return node;
6217 /* Compute a schedule for the given dependence graph and insert it at "node".
6218 * Return the updated schedule node.
6220 * We first check if the graph is connected (through validity and conditional
6221 * validity dependences) and, if not, compute a schedule
6222 * for each component separately.
6223 * If the schedule_serialize_sccs option is set, then we check for strongly
6224 * connected components instead and compute a separate schedule for
6225 * each such strongly connected component.
6227 static __isl_give isl_schedule_node *compute_schedule(isl_schedule_node *node,
6228 struct isl_sched_graph *graph)
6230 isl_ctx *ctx;
6232 if (!node)
6233 return NULL;
6235 ctx = isl_schedule_node_get_ctx(node);
6236 if (isl_options_get_schedule_serialize_sccs(ctx)) {
6237 if (detect_sccs(ctx, graph) < 0)
6238 return isl_schedule_node_free(node);
6239 } else {
6240 if (detect_wccs(ctx, graph) < 0)
6241 return isl_schedule_node_free(node);
6244 if (graph->scc > 1)
6245 return compute_component_schedule(node, graph, 1);
6247 return compute_schedule_wcc(node, graph);
6250 /* Compute a schedule on sc->domain that respects the given schedule
6251 * constraints.
6253 * In particular, the schedule respects all the validity dependences.
6254 * If the default isl scheduling algorithm is used, it tries to minimize
6255 * the dependence distances over the proximity dependences.
6256 * If Feautrier's scheduling algorithm is used, the proximity dependence
6257 * distances are only minimized during the extension to a full-dimensional
6258 * schedule.
6260 * If there are any condition and conditional validity dependences,
6261 * then the conditional validity dependences may be violated inside
6262 * a tilable band, provided they have no adjacent non-local
6263 * condition dependences.
6265 __isl_give isl_schedule *isl_schedule_constraints_compute_schedule(
6266 __isl_take isl_schedule_constraints *sc)
6268 isl_ctx *ctx = isl_schedule_constraints_get_ctx(sc);
6269 struct isl_sched_graph graph = { 0 };
6270 isl_schedule *sched;
6271 isl_schedule_node *node;
6272 isl_union_set *domain;
6274 sc = isl_schedule_constraints_align_params(sc);
6276 domain = isl_schedule_constraints_get_domain(sc);
6277 if (isl_union_set_n_set(domain) == 0) {
6278 isl_schedule_constraints_free(sc);
6279 return isl_schedule_from_domain(domain);
6282 if (graph_init(&graph, sc) < 0)
6283 domain = isl_union_set_free(domain);
6285 node = isl_schedule_node_from_domain(domain);
6286 node = isl_schedule_node_child(node, 0);
6287 if (graph.n > 0)
6288 node = compute_schedule(node, &graph);
6289 sched = isl_schedule_node_get_schedule(node);
6290 isl_schedule_node_free(node);
6292 graph_free(ctx, &graph);
6293 isl_schedule_constraints_free(sc);
6295 return sched;
6298 /* Compute a schedule for the given union of domains that respects
6299 * all the validity dependences and minimizes
6300 * the dependence distances over the proximity dependences.
6302 * This function is kept for backward compatibility.
6304 __isl_give isl_schedule *isl_union_set_compute_schedule(
6305 __isl_take isl_union_set *domain,
6306 __isl_take isl_union_map *validity,
6307 __isl_take isl_union_map *proximity)
6309 isl_schedule_constraints *sc;
6311 sc = isl_schedule_constraints_on_domain(domain);
6312 sc = isl_schedule_constraints_set_validity(sc, validity);
6313 sc = isl_schedule_constraints_set_proximity(sc, proximity);
6315 return isl_schedule_constraints_compute_schedule(sc);