isl_scheduler.c: move up node_scc_* functions
[isl.git] / isl_scheduler.c
blobad6fd695c0e31036cfc9a49a5c36465081664df6
1 /*
2 * Copyright 2011 INRIA Saclay
3 * Copyright 2012-2014 Ecole Normale Superieure
5 * Use of this software is governed by the MIT license
7 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
8 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
9 * 91893 Orsay, France
10 * and Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
13 #include <isl_ctx_private.h>
14 #include <isl_map_private.h>
15 #include <isl_space_private.h>
16 #include <isl_aff_private.h>
17 #include <isl/hash.h>
18 #include <isl/constraint.h>
19 #include <isl/schedule.h>
20 #include <isl_mat_private.h>
21 #include <isl_vec_private.h>
22 #include <isl/set.h>
23 #include <isl_seq.h>
24 #include <isl_tab.h>
25 #include <isl_dim_map.h>
26 #include <isl/map_to_basic_set.h>
27 #include <isl_sort.h>
28 #include <isl_schedule_private.h>
29 #include <isl_options_private.h>
30 #include <isl_tarjan.h>
31 #include <isl_morph.h>
34 * The scheduling algorithm implemented in this file was inspired by
35 * Bondhugula et al., "Automatic Transformations for Communication-Minimized
36 * Parallelization and Locality Optimization in the Polyhedral Model".
39 enum isl_edge_type {
40 isl_edge_validity = 0,
41 isl_edge_first = isl_edge_validity,
42 isl_edge_coincidence,
43 isl_edge_condition,
44 isl_edge_conditional_validity,
45 isl_edge_proximity,
46 isl_edge_last = isl_edge_proximity
49 /* The constraints that need to be satisfied by a schedule on "domain".
51 * "validity" constraints map domain elements i to domain elements
52 * that should be scheduled after i. (Hard constraint)
53 * "proximity" constraints map domain elements i to domains elements
54 * that should be scheduled as early as possible after i (or before i).
55 * (Soft constraint)
57 * "condition" and "conditional_validity" constraints map possibly "tagged"
58 * domain elements i -> s to "tagged" domain elements j -> t.
59 * The elements of the "conditional_validity" constraints, but without the
60 * tags (i.e., the elements i -> j) are treated as validity constraints,
61 * except that during the construction of a tilable band,
62 * the elements of the "conditional_validity" constraints may be violated
63 * provided that all adjacent elements of the "condition" constraints
64 * are local within the band.
65 * A dependence is local within a band if domain and range are mapped
66 * to the same schedule point by the band.
68 struct isl_schedule_constraints {
69 isl_union_set *domain;
71 isl_union_map *constraint[isl_edge_last + 1];
74 __isl_give isl_schedule_constraints *isl_schedule_constraints_copy(
75 __isl_keep isl_schedule_constraints *sc)
77 isl_ctx *ctx;
78 isl_schedule_constraints *sc_copy;
79 enum isl_edge_type i;
81 ctx = isl_union_set_get_ctx(sc->domain);
82 sc_copy = isl_calloc_type(ctx, struct isl_schedule_constraints);
83 if (!sc_copy)
84 return NULL;
86 sc_copy->domain = isl_union_set_copy(sc->domain);
87 if (!sc_copy->domain)
88 return isl_schedule_constraints_free(sc_copy);
90 for (i = isl_edge_first; i <= isl_edge_last; ++i) {
91 sc_copy->constraint[i] = isl_union_map_copy(sc->constraint[i]);
92 if (!sc_copy->constraint[i])
93 return isl_schedule_constraints_free(sc_copy);
96 return sc_copy;
100 /* Construct an isl_schedule_constraints object for computing a schedule
101 * on "domain". The initial object does not impose any constraints.
103 __isl_give isl_schedule_constraints *isl_schedule_constraints_on_domain(
104 __isl_take isl_union_set *domain)
106 isl_ctx *ctx;
107 isl_space *space;
108 isl_schedule_constraints *sc;
109 isl_union_map *empty;
110 enum isl_edge_type i;
112 if (!domain)
113 return NULL;
115 ctx = isl_union_set_get_ctx(domain);
116 sc = isl_calloc_type(ctx, struct isl_schedule_constraints);
117 if (!sc)
118 goto error;
120 space = isl_union_set_get_space(domain);
121 sc->domain = domain;
122 empty = isl_union_map_empty(space);
123 for (i = isl_edge_first; i <= isl_edge_last; ++i) {
124 sc->constraint[i] = isl_union_map_copy(empty);
125 if (!sc->constraint[i])
126 sc->domain = isl_union_set_free(sc->domain);
128 isl_union_map_free(empty);
130 if (!sc->domain)
131 return isl_schedule_constraints_free(sc);
133 return sc;
134 error:
135 isl_union_set_free(domain);
136 return NULL;
139 /* Replace the validity constraints of "sc" by "validity".
141 __isl_give isl_schedule_constraints *isl_schedule_constraints_set_validity(
142 __isl_take isl_schedule_constraints *sc,
143 __isl_take isl_union_map *validity)
145 if (!sc || !validity)
146 goto error;
148 isl_union_map_free(sc->constraint[isl_edge_validity]);
149 sc->constraint[isl_edge_validity] = validity;
151 return sc;
152 error:
153 isl_schedule_constraints_free(sc);
154 isl_union_map_free(validity);
155 return NULL;
158 /* Replace the coincidence constraints of "sc" by "coincidence".
160 __isl_give isl_schedule_constraints *isl_schedule_constraints_set_coincidence(
161 __isl_take isl_schedule_constraints *sc,
162 __isl_take isl_union_map *coincidence)
164 if (!sc || !coincidence)
165 goto error;
167 isl_union_map_free(sc->constraint[isl_edge_coincidence]);
168 sc->constraint[isl_edge_coincidence] = coincidence;
170 return sc;
171 error:
172 isl_schedule_constraints_free(sc);
173 isl_union_map_free(coincidence);
174 return NULL;
177 /* Replace the proximity constraints of "sc" by "proximity".
179 __isl_give isl_schedule_constraints *isl_schedule_constraints_set_proximity(
180 __isl_take isl_schedule_constraints *sc,
181 __isl_take isl_union_map *proximity)
183 if (!sc || !proximity)
184 goto error;
186 isl_union_map_free(sc->constraint[isl_edge_proximity]);
187 sc->constraint[isl_edge_proximity] = proximity;
189 return sc;
190 error:
191 isl_schedule_constraints_free(sc);
192 isl_union_map_free(proximity);
193 return NULL;
196 /* Replace the conditional validity constraints of "sc" by "condition"
197 * and "validity".
199 __isl_give isl_schedule_constraints *
200 isl_schedule_constraints_set_conditional_validity(
201 __isl_take isl_schedule_constraints *sc,
202 __isl_take isl_union_map *condition,
203 __isl_take isl_union_map *validity)
205 if (!sc || !condition || !validity)
206 goto error;
208 isl_union_map_free(sc->constraint[isl_edge_condition]);
209 sc->constraint[isl_edge_condition] = condition;
210 isl_union_map_free(sc->constraint[isl_edge_conditional_validity]);
211 sc->constraint[isl_edge_conditional_validity] = validity;
213 return sc;
214 error:
215 isl_schedule_constraints_free(sc);
216 isl_union_map_free(condition);
217 isl_union_map_free(validity);
218 return NULL;
221 __isl_null isl_schedule_constraints *isl_schedule_constraints_free(
222 __isl_take isl_schedule_constraints *sc)
224 enum isl_edge_type i;
226 if (!sc)
227 return NULL;
229 isl_union_set_free(sc->domain);
230 for (i = isl_edge_first; i <= isl_edge_last; ++i)
231 isl_union_map_free(sc->constraint[i]);
233 free(sc);
235 return NULL;
238 isl_ctx *isl_schedule_constraints_get_ctx(
239 __isl_keep isl_schedule_constraints *sc)
241 return sc ? isl_union_set_get_ctx(sc->domain) : NULL;
244 void isl_schedule_constraints_dump(__isl_keep isl_schedule_constraints *sc)
246 if (!sc)
247 return;
249 fprintf(stderr, "domain: ");
250 isl_union_set_dump(sc->domain);
251 fprintf(stderr, "validity: ");
252 isl_union_map_dump(sc->constraint[isl_edge_validity]);
253 fprintf(stderr, "proximity: ");
254 isl_union_map_dump(sc->constraint[isl_edge_proximity]);
255 fprintf(stderr, "coincidence: ");
256 isl_union_map_dump(sc->constraint[isl_edge_coincidence]);
257 fprintf(stderr, "condition: ");
258 isl_union_map_dump(sc->constraint[isl_edge_condition]);
259 fprintf(stderr, "conditional_validity: ");
260 isl_union_map_dump(sc->constraint[isl_edge_conditional_validity]);
263 /* Align the parameters of the fields of "sc".
265 static __isl_give isl_schedule_constraints *
266 isl_schedule_constraints_align_params(__isl_take isl_schedule_constraints *sc)
268 isl_space *space;
269 enum isl_edge_type i;
271 if (!sc)
272 return NULL;
274 space = isl_union_set_get_space(sc->domain);
275 for (i = isl_edge_first; i <= isl_edge_last; ++i)
276 space = isl_space_align_params(space,
277 isl_union_map_get_space(sc->constraint[i]));
279 for (i = isl_edge_first; i <= isl_edge_last; ++i) {
280 sc->constraint[i] = isl_union_map_align_params(
281 sc->constraint[i], isl_space_copy(space));
282 if (!sc->constraint[i])
283 space = isl_space_free(space);
285 sc->domain = isl_union_set_align_params(sc->domain, space);
286 if (!sc->domain)
287 return isl_schedule_constraints_free(sc);
289 return sc;
292 /* Return the total number of isl_maps in the constraints of "sc".
294 static __isl_give int isl_schedule_constraints_n_map(
295 __isl_keep isl_schedule_constraints *sc)
297 enum isl_edge_type i;
298 int n = 0;
300 for (i = isl_edge_first; i <= isl_edge_last; ++i)
301 n += isl_union_map_n_map(sc->constraint[i]);
303 return n;
306 /* Internal information about a node that is used during the construction
307 * of a schedule.
308 * space represents the space in which the domain lives
309 * sched is a matrix representation of the schedule being constructed
310 * for this node; if compressed is set, then this schedule is
311 * defined over the compressed domain space
312 * sched_map is an isl_map representation of the same (partial) schedule
313 * sched_map may be NULL; if compressed is set, then this map
314 * is defined over the uncompressed domain space
315 * rank is the number of linearly independent rows in the linear part
316 * of sched
317 * the columns of cmap represent a change of basis for the schedule
318 * coefficients; the first rank columns span the linear part of
319 * the schedule rows
320 * cinv is the inverse of cmap.
321 * start is the first variable in the LP problem in the sequences that
322 * represents the schedule coefficients of this node
323 * nvar is the dimension of the domain
324 * nparam is the number of parameters or 0 if we are not constructing
325 * a parametric schedule
327 * If compressed is set, then hull represents the constraints
328 * that were used to derive the compression, while compress and
329 * decompress map the original space to the compressed space and
330 * vice versa.
332 * scc is the index of SCC (or WCC) this node belongs to
334 * band contains the band index for each of the rows of the schedule.
335 * band_id is used to differentiate between separate bands at the same
336 * level within the same parent band, i.e., bands that are separated
337 * by the parent band or bands that are independent of each other.
338 * coincident contains a boolean for each of the rows of the schedule,
339 * indicating whether the corresponding scheduling dimension satisfies
340 * the coincidence constraints in the sense that the corresponding
341 * dependence distances are zero.
343 struct isl_sched_node {
344 isl_space *space;
345 int compressed;
346 isl_set *hull;
347 isl_multi_aff *compress;
348 isl_multi_aff *decompress;
349 isl_mat *sched;
350 isl_map *sched_map;
351 int rank;
352 isl_mat *cmap;
353 isl_mat *cinv;
354 int start;
355 int nvar;
356 int nparam;
358 int scc;
360 int *band;
361 int *band_id;
362 int *coincident;
365 static int node_has_space(const void *entry, const void *val)
367 struct isl_sched_node *node = (struct isl_sched_node *)entry;
368 isl_space *dim = (isl_space *)val;
370 return isl_space_is_equal(node->space, dim);
373 static int node_scc_exactly(struct isl_sched_node *node, int scc)
375 return node->scc == scc;
378 static int node_scc_at_most(struct isl_sched_node *node, int scc)
380 return node->scc <= scc;
383 static int node_scc_at_least(struct isl_sched_node *node, int scc)
385 return node->scc >= scc;
388 /* An edge in the dependence graph. An edge may be used to
389 * ensure validity of the generated schedule, to minimize the dependence
390 * distance or both
392 * map is the dependence relation, with i -> j in the map if j depends on i
393 * tagged_condition and tagged_validity contain the union of all tagged
394 * condition or conditional validity dependence relations that
395 * specialize the dependence relation "map"; that is,
396 * if (i -> a) -> (j -> b) is an element of "tagged_condition"
397 * or "tagged_validity", then i -> j is an element of "map".
398 * If these fields are NULL, then they represent the empty relation.
399 * src is the source node
400 * dst is the sink node
401 * validity is set if the edge is used to ensure correctness
402 * coincidence is used to enforce zero dependence distances
403 * proximity is set if the edge is used to minimize dependence distances
404 * condition is set if the edge represents a condition
405 * for a conditional validity schedule constraint
406 * local can only be set for condition edges and indicates that
407 * the dependence distance over the edge should be zero
408 * conditional_validity is set if the edge is used to conditionally
409 * ensure correctness
411 * For validity edges, start and end mark the sequence of inequality
412 * constraints in the LP problem that encode the validity constraint
413 * corresponding to this edge.
415 struct isl_sched_edge {
416 isl_map *map;
417 isl_union_map *tagged_condition;
418 isl_union_map *tagged_validity;
420 struct isl_sched_node *src;
421 struct isl_sched_node *dst;
423 unsigned validity : 1;
424 unsigned coincidence : 1;
425 unsigned proximity : 1;
426 unsigned local : 1;
427 unsigned condition : 1;
428 unsigned conditional_validity : 1;
430 int start;
431 int end;
434 /* Internal information about the dependence graph used during
435 * the construction of the schedule.
437 * intra_hmap is a cache, mapping dependence relations to their dual,
438 * for dependences from a node to itself
439 * inter_hmap is a cache, mapping dependence relations to their dual,
440 * for dependences between distinct nodes
441 * if compression is involved then the key for these maps
442 * it the original, uncompressed dependence relation, while
443 * the value is the dual of the compressed dependence relation.
445 * n is the number of nodes
446 * node is the list of nodes
447 * maxvar is the maximal number of variables over all nodes
448 * max_row is the allocated number of rows in the schedule
449 * n_row is the current (maximal) number of linearly independent
450 * rows in the node schedules
451 * n_total_row is the current number of rows in the node schedules
452 * n_band is the current number of completed bands
453 * band_start is the starting row in the node schedules of the current band
454 * root is set if this graph is the original dependence graph,
455 * without any splitting
457 * sorted contains a list of node indices sorted according to the
458 * SCC to which a node belongs
460 * n_edge is the number of edges
461 * edge is the list of edges
462 * max_edge contains the maximal number of edges of each type;
463 * in particular, it contains the number of edges in the inital graph.
464 * edge_table contains pointers into the edge array, hashed on the source
465 * and sink spaces; there is one such table for each type;
466 * a given edge may be referenced from more than one table
467 * if the corresponding relation appears in more than of the
468 * sets of dependences
470 * node_table contains pointers into the node array, hashed on the space
472 * region contains a list of variable sequences that should be non-trivial
474 * lp contains the (I)LP problem used to obtain new schedule rows
476 * src_scc and dst_scc are the source and sink SCCs of an edge with
477 * conflicting constraints
479 * scc represents the number of components
480 * weak is set if the components are weakly connected
482 struct isl_sched_graph {
483 isl_map_to_basic_set *intra_hmap;
484 isl_map_to_basic_set *inter_hmap;
486 struct isl_sched_node *node;
487 int n;
488 int maxvar;
489 int max_row;
490 int n_row;
492 int *sorted;
494 int n_band;
495 int n_total_row;
496 int band_start;
498 int root;
500 struct isl_sched_edge *edge;
501 int n_edge;
502 int max_edge[isl_edge_last + 1];
503 struct isl_hash_table *edge_table[isl_edge_last + 1];
505 struct isl_hash_table *node_table;
506 struct isl_region *region;
508 isl_basic_set *lp;
510 int src_scc;
511 int dst_scc;
513 int scc;
514 int weak;
517 /* Initialize node_table based on the list of nodes.
519 static int graph_init_table(isl_ctx *ctx, struct isl_sched_graph *graph)
521 int i;
523 graph->node_table = isl_hash_table_alloc(ctx, graph->n);
524 if (!graph->node_table)
525 return -1;
527 for (i = 0; i < graph->n; ++i) {
528 struct isl_hash_table_entry *entry;
529 uint32_t hash;
531 hash = isl_space_get_hash(graph->node[i].space);
532 entry = isl_hash_table_find(ctx, graph->node_table, hash,
533 &node_has_space,
534 graph->node[i].space, 1);
535 if (!entry)
536 return -1;
537 entry->data = &graph->node[i];
540 return 0;
543 /* Return a pointer to the node that lives within the given space,
544 * or NULL if there is no such node.
546 static struct isl_sched_node *graph_find_node(isl_ctx *ctx,
547 struct isl_sched_graph *graph, __isl_keep isl_space *dim)
549 struct isl_hash_table_entry *entry;
550 uint32_t hash;
552 hash = isl_space_get_hash(dim);
553 entry = isl_hash_table_find(ctx, graph->node_table, hash,
554 &node_has_space, dim, 0);
556 return entry ? entry->data : NULL;
559 static int edge_has_src_and_dst(const void *entry, const void *val)
561 const struct isl_sched_edge *edge = entry;
562 const struct isl_sched_edge *temp = val;
564 return edge->src == temp->src && edge->dst == temp->dst;
567 /* Add the given edge to graph->edge_table[type].
569 static int graph_edge_table_add(isl_ctx *ctx, struct isl_sched_graph *graph,
570 enum isl_edge_type type, struct isl_sched_edge *edge)
572 struct isl_hash_table_entry *entry;
573 uint32_t hash;
575 hash = isl_hash_init();
576 hash = isl_hash_builtin(hash, edge->src);
577 hash = isl_hash_builtin(hash, edge->dst);
578 entry = isl_hash_table_find(ctx, graph->edge_table[type], hash,
579 &edge_has_src_and_dst, edge, 1);
580 if (!entry)
581 return -1;
582 entry->data = edge;
584 return 0;
587 /* Allocate the edge_tables based on the maximal number of edges of
588 * each type.
590 static int graph_init_edge_tables(isl_ctx *ctx, struct isl_sched_graph *graph)
592 int i;
594 for (i = 0; i <= isl_edge_last; ++i) {
595 graph->edge_table[i] = isl_hash_table_alloc(ctx,
596 graph->max_edge[i]);
597 if (!graph->edge_table[i])
598 return -1;
601 return 0;
604 /* If graph->edge_table[type] contains an edge from the given source
605 * to the given destination, then return the hash table entry of this edge.
606 * Otherwise, return NULL.
608 static struct isl_hash_table_entry *graph_find_edge_entry(
609 struct isl_sched_graph *graph,
610 enum isl_edge_type type,
611 struct isl_sched_node *src, struct isl_sched_node *dst)
613 isl_ctx *ctx = isl_space_get_ctx(src->space);
614 uint32_t hash;
615 struct isl_sched_edge temp = { .src = src, .dst = dst };
617 hash = isl_hash_init();
618 hash = isl_hash_builtin(hash, temp.src);
619 hash = isl_hash_builtin(hash, temp.dst);
620 return isl_hash_table_find(ctx, graph->edge_table[type], hash,
621 &edge_has_src_and_dst, &temp, 0);
625 /* If graph->edge_table[type] contains an edge from the given source
626 * to the given destination, then return this edge.
627 * Otherwise, return NULL.
629 static struct isl_sched_edge *graph_find_edge(struct isl_sched_graph *graph,
630 enum isl_edge_type type,
631 struct isl_sched_node *src, struct isl_sched_node *dst)
633 struct isl_hash_table_entry *entry;
635 entry = graph_find_edge_entry(graph, type, src, dst);
636 if (!entry)
637 return NULL;
639 return entry->data;
642 /* Check whether the dependence graph has an edge of the given type
643 * between the given two nodes.
645 static int graph_has_edge(struct isl_sched_graph *graph,
646 enum isl_edge_type type,
647 struct isl_sched_node *src, struct isl_sched_node *dst)
649 struct isl_sched_edge *edge;
650 int empty;
652 edge = graph_find_edge(graph, type, src, dst);
653 if (!edge)
654 return 0;
656 empty = isl_map_plain_is_empty(edge->map);
657 if (empty < 0)
658 return -1;
660 return !empty;
663 /* Look for any edge with the same src, dst and map fields as "model".
665 * Return the matching edge if one can be found.
666 * Return "model" if no matching edge is found.
667 * Return NULL on error.
669 static struct isl_sched_edge *graph_find_matching_edge(
670 struct isl_sched_graph *graph, struct isl_sched_edge *model)
672 enum isl_edge_type i;
673 struct isl_sched_edge *edge;
675 for (i = isl_edge_first; i <= isl_edge_last; ++i) {
676 int is_equal;
678 edge = graph_find_edge(graph, i, model->src, model->dst);
679 if (!edge)
680 continue;
681 is_equal = isl_map_plain_is_equal(model->map, edge->map);
682 if (is_equal < 0)
683 return NULL;
684 if (is_equal)
685 return edge;
688 return model;
691 /* Remove the given edge from all the edge_tables that refer to it.
693 static void graph_remove_edge(struct isl_sched_graph *graph,
694 struct isl_sched_edge *edge)
696 isl_ctx *ctx = isl_map_get_ctx(edge->map);
697 enum isl_edge_type i;
699 for (i = isl_edge_first; i <= isl_edge_last; ++i) {
700 struct isl_hash_table_entry *entry;
702 entry = graph_find_edge_entry(graph, i, edge->src, edge->dst);
703 if (!entry)
704 continue;
705 if (entry->data != edge)
706 continue;
707 isl_hash_table_remove(ctx, graph->edge_table[i], entry);
711 /* Check whether the dependence graph has any edge
712 * between the given two nodes.
714 static int graph_has_any_edge(struct isl_sched_graph *graph,
715 struct isl_sched_node *src, struct isl_sched_node *dst)
717 enum isl_edge_type i;
718 int r;
720 for (i = isl_edge_first; i <= isl_edge_last; ++i) {
721 r = graph_has_edge(graph, i, src, dst);
722 if (r < 0 || r)
723 return r;
726 return r;
729 /* Check whether the dependence graph has a validity edge
730 * between the given two nodes.
732 * Conditional validity edges are essentially validity edges that
733 * can be ignored if the corresponding condition edges are iteration private.
734 * Here, we are only checking for the presence of validity
735 * edges, so we need to consider the conditional validity edges too.
736 * In particular, this function is used during the detection
737 * of strongly connected components and we cannot ignore
738 * conditional validity edges during this detection.
740 static int graph_has_validity_edge(struct isl_sched_graph *graph,
741 struct isl_sched_node *src, struct isl_sched_node *dst)
743 int r;
745 r = graph_has_edge(graph, isl_edge_validity, src, dst);
746 if (r < 0 || r)
747 return r;
749 return graph_has_edge(graph, isl_edge_conditional_validity, src, dst);
752 static int graph_alloc(isl_ctx *ctx, struct isl_sched_graph *graph,
753 int n_node, int n_edge)
755 int i;
757 graph->n = n_node;
758 graph->n_edge = n_edge;
759 graph->node = isl_calloc_array(ctx, struct isl_sched_node, graph->n);
760 graph->sorted = isl_calloc_array(ctx, int, graph->n);
761 graph->region = isl_alloc_array(ctx, struct isl_region, graph->n);
762 graph->edge = isl_calloc_array(ctx,
763 struct isl_sched_edge, graph->n_edge);
765 graph->intra_hmap = isl_map_to_basic_set_alloc(ctx, 2 * n_edge);
766 graph->inter_hmap = isl_map_to_basic_set_alloc(ctx, 2 * n_edge);
768 if (!graph->node || !graph->region || (graph->n_edge && !graph->edge) ||
769 !graph->sorted)
770 return -1;
772 for(i = 0; i < graph->n; ++i)
773 graph->sorted[i] = i;
775 return 0;
778 static void graph_free(isl_ctx *ctx, struct isl_sched_graph *graph)
780 int i;
782 isl_map_to_basic_set_free(graph->intra_hmap);
783 isl_map_to_basic_set_free(graph->inter_hmap);
785 if (graph->node)
786 for (i = 0; i < graph->n; ++i) {
787 isl_space_free(graph->node[i].space);
788 isl_set_free(graph->node[i].hull);
789 isl_multi_aff_free(graph->node[i].compress);
790 isl_multi_aff_free(graph->node[i].decompress);
791 isl_mat_free(graph->node[i].sched);
792 isl_map_free(graph->node[i].sched_map);
793 isl_mat_free(graph->node[i].cmap);
794 isl_mat_free(graph->node[i].cinv);
795 if (graph->root) {
796 free(graph->node[i].band);
797 free(graph->node[i].band_id);
798 free(graph->node[i].coincident);
801 free(graph->node);
802 free(graph->sorted);
803 if (graph->edge)
804 for (i = 0; i < graph->n_edge; ++i) {
805 isl_map_free(graph->edge[i].map);
806 isl_union_map_free(graph->edge[i].tagged_condition);
807 isl_union_map_free(graph->edge[i].tagged_validity);
809 free(graph->edge);
810 free(graph->region);
811 for (i = 0; i <= isl_edge_last; ++i)
812 isl_hash_table_free(ctx, graph->edge_table[i]);
813 isl_hash_table_free(ctx, graph->node_table);
814 isl_basic_set_free(graph->lp);
817 /* For each "set" on which this function is called, increment
818 * graph->n by one and update graph->maxvar.
820 static int init_n_maxvar(__isl_take isl_set *set, void *user)
822 struct isl_sched_graph *graph = user;
823 int nvar = isl_set_dim(set, isl_dim_set);
825 graph->n++;
826 if (nvar > graph->maxvar)
827 graph->maxvar = nvar;
829 isl_set_free(set);
831 return 0;
834 /* Add the number of basic maps in "map" to *n.
836 static int add_n_basic_map(__isl_take isl_map *map, void *user)
838 int *n = user;
840 *n += isl_map_n_basic_map(map);
841 isl_map_free(map);
843 return 0;
846 /* Compute the number of rows that should be allocated for the schedule.
847 * The graph can be split at most "n - 1" times, there can be at most
848 * one row for each dimension in the iteration domains plus two rows
849 * for each basic map in the dependences (in particular,
850 * we usually have one row, but it may be split by split_scaled),
851 * and there can be one extra row for ordering the statements.
852 * Note that if we have actually split "n - 1" times, then no ordering
853 * is needed, so in principle we could use "graph->n + 2 * graph->maxvar - 1".
854 * It is also practically impossible to exhaust both the number of dependences
855 * and the number of variables.
857 static int compute_max_row(struct isl_sched_graph *graph,
858 __isl_keep isl_schedule_constraints *sc)
860 enum isl_edge_type i;
861 int n_edge;
863 graph->n = 0;
864 graph->maxvar = 0;
865 if (isl_union_set_foreach_set(sc->domain, &init_n_maxvar, graph) < 0)
866 return -1;
867 n_edge = 0;
868 for (i = isl_edge_first; i <= isl_edge_last; ++i)
869 if (isl_union_map_foreach_map(sc->constraint[i],
870 &add_n_basic_map, &n_edge) < 0)
871 return -1;
872 graph->max_row = graph->n + 2 * n_edge + graph->maxvar;
874 return 0;
877 /* Does "bset" have any defining equalities for its set variables?
879 static int has_any_defining_equality(__isl_keep isl_basic_set *bset)
881 int i, n;
883 if (!bset)
884 return -1;
886 n = isl_basic_set_dim(bset, isl_dim_set);
887 for (i = 0; i < n; ++i) {
888 int has;
890 has = isl_basic_set_has_defining_equality(bset, isl_dim_set, i,
891 NULL);
892 if (has < 0 || has)
893 return has;
896 return 0;
899 /* Add a new node to the graph representing the given space.
900 * "nvar" is the (possibly compressed) number of variables and
901 * may be smaller than then number of set variables in "space"
902 * if "compressed" is set.
903 * If "compressed" is set, then "hull" represents the constraints
904 * that were used to derive the compression, while "compress" and
905 * "decompress" map the original space to the compressed space and
906 * vice versa.
907 * If "compressed" is not set, then "hull", "compress" and "decompress"
908 * should be NULL.
910 static int add_node(struct isl_sched_graph *graph, __isl_take isl_space *space,
911 int nvar, int compressed, __isl_take isl_set *hull,
912 __isl_take isl_multi_aff *compress,
913 __isl_take isl_multi_aff *decompress)
915 int nparam;
916 isl_ctx *ctx;
917 isl_mat *sched;
918 int *band, *band_id, *coincident;
920 if (!space)
921 return -1;
923 ctx = isl_space_get_ctx(space);
924 nparam = isl_space_dim(space, isl_dim_param);
925 if (!ctx->opt->schedule_parametric)
926 nparam = 0;
927 sched = isl_mat_alloc(ctx, 0, 1 + nparam + nvar);
928 graph->node[graph->n].space = space;
929 graph->node[graph->n].nvar = nvar;
930 graph->node[graph->n].nparam = nparam;
931 graph->node[graph->n].sched = sched;
932 graph->node[graph->n].sched_map = NULL;
933 band = isl_alloc_array(ctx, int, graph->max_row);
934 graph->node[graph->n].band = band;
935 band_id = isl_calloc_array(ctx, int, graph->max_row);
936 graph->node[graph->n].band_id = band_id;
937 coincident = isl_calloc_array(ctx, int, graph->max_row);
938 graph->node[graph->n].coincident = coincident;
939 graph->node[graph->n].compressed = compressed;
940 graph->node[graph->n].hull = hull;
941 graph->node[graph->n].compress = compress;
942 graph->node[graph->n].decompress = decompress;
943 graph->n++;
945 if (!space || !sched ||
946 (graph->max_row && (!band || !band_id || !coincident)))
947 return -1;
948 if (compressed && (!hull || !compress || !decompress))
949 return -1;
951 return 0;
954 /* Add a new node to the graph representing the given set.
956 * If any of the set variables is defined by an equality, then
957 * we perform variable compression such that we can perform
958 * the scheduling on the compressed domain.
960 static int extract_node(__isl_take isl_set *set, void *user)
962 int nvar;
963 int has_equality;
964 isl_space *space;
965 isl_basic_set *hull;
966 isl_set *hull_set;
967 isl_morph *morph;
968 isl_multi_aff *compress, *decompress;
969 struct isl_sched_graph *graph = user;
971 space = isl_set_get_space(set);
972 hull = isl_set_affine_hull(set);
973 hull = isl_basic_set_remove_divs(hull);
974 nvar = isl_space_dim(space, isl_dim_set);
975 has_equality = has_any_defining_equality(hull);
977 if (has_equality < 0)
978 goto error;
979 if (!has_equality) {
980 isl_basic_set_free(hull);
981 return add_node(graph, space, nvar, 0, NULL, NULL, NULL);
984 morph = isl_basic_set_variable_compression(hull, isl_dim_set);
985 nvar = isl_morph_ran_dim(morph, isl_dim_set);
986 compress = isl_morph_get_var_multi_aff(morph);
987 morph = isl_morph_inverse(morph);
988 decompress = isl_morph_get_var_multi_aff(morph);
989 isl_morph_free(morph);
991 hull_set = isl_set_from_basic_set(hull);
992 return add_node(graph, space, nvar, 1, hull_set, compress, decompress);
993 error:
994 isl_basic_set_free(hull);
995 isl_space_free(space);
996 return -1;
999 struct isl_extract_edge_data {
1000 enum isl_edge_type type;
1001 struct isl_sched_graph *graph;
1004 /* Merge edge2 into edge1, freeing the contents of edge2.
1005 * "type" is the type of the schedule constraint from which edge2 was
1006 * extracted.
1007 * Return 0 on success and -1 on failure.
1009 * edge1 and edge2 are assumed to have the same value for the map field.
1011 static int merge_edge(enum isl_edge_type type, struct isl_sched_edge *edge1,
1012 struct isl_sched_edge *edge2)
1014 edge1->validity |= edge2->validity;
1015 edge1->coincidence |= edge2->coincidence;
1016 edge1->proximity |= edge2->proximity;
1017 edge1->condition |= edge2->condition;
1018 edge1->conditional_validity |= edge2->conditional_validity;
1019 isl_map_free(edge2->map);
1021 if (type == isl_edge_condition) {
1022 if (!edge1->tagged_condition)
1023 edge1->tagged_condition = edge2->tagged_condition;
1024 else
1025 edge1->tagged_condition =
1026 isl_union_map_union(edge1->tagged_condition,
1027 edge2->tagged_condition);
1030 if (type == isl_edge_conditional_validity) {
1031 if (!edge1->tagged_validity)
1032 edge1->tagged_validity = edge2->tagged_validity;
1033 else
1034 edge1->tagged_validity =
1035 isl_union_map_union(edge1->tagged_validity,
1036 edge2->tagged_validity);
1039 if (type == isl_edge_condition && !edge1->tagged_condition)
1040 return -1;
1041 if (type == isl_edge_conditional_validity && !edge1->tagged_validity)
1042 return -1;
1044 return 0;
1047 /* Insert dummy tags in domain and range of "map".
1049 * In particular, if "map" is of the form
1051 * A -> B
1053 * then return
1055 * [A -> dummy_tag] -> [B -> dummy_tag]
1057 * where the dummy_tags are identical and equal to any dummy tags
1058 * introduced by any other call to this function.
1060 static __isl_give isl_map *insert_dummy_tags(__isl_take isl_map *map)
1062 static char dummy;
1063 isl_ctx *ctx;
1064 isl_id *id;
1065 isl_space *space;
1066 isl_set *domain, *range;
1068 ctx = isl_map_get_ctx(map);
1070 id = isl_id_alloc(ctx, NULL, &dummy);
1071 space = isl_space_params(isl_map_get_space(map));
1072 space = isl_space_set_from_params(space);
1073 space = isl_space_set_tuple_id(space, isl_dim_set, id);
1074 space = isl_space_map_from_set(space);
1076 domain = isl_map_wrap(map);
1077 range = isl_map_wrap(isl_map_universe(space));
1078 map = isl_map_from_domain_and_range(domain, range);
1079 map = isl_map_zip(map);
1081 return map;
1084 /* Given that at least one of "src" or "dst" is compressed, return
1085 * a map between the spaces of these nodes restricted to the affine
1086 * hull that was used in the compression.
1088 static __isl_give isl_map *extract_hull(struct isl_sched_node *src,
1089 struct isl_sched_node *dst)
1091 isl_set *dom, *ran;
1093 if (src->compressed)
1094 dom = isl_set_copy(src->hull);
1095 else
1096 dom = isl_set_universe(isl_space_copy(src->space));
1097 if (dst->compressed)
1098 ran = isl_set_copy(dst->hull);
1099 else
1100 ran = isl_set_universe(isl_space_copy(dst->space));
1102 return isl_map_from_domain_and_range(dom, ran);
1105 /* Intersect the domains of the nested relations in domain and range
1106 * of "tagged" with "map".
1108 static __isl_give isl_map *map_intersect_domains(__isl_take isl_map *tagged,
1109 __isl_keep isl_map *map)
1111 isl_set *set;
1113 tagged = isl_map_zip(tagged);
1114 set = isl_map_wrap(isl_map_copy(map));
1115 tagged = isl_map_intersect_domain(tagged, set);
1116 tagged = isl_map_zip(tagged);
1117 return tagged;
1120 /* Add a new edge to the graph based on the given map
1121 * and add it to data->graph->edge_table[data->type].
1122 * If a dependence relation of a given type happens to be identical
1123 * to one of the dependence relations of a type that was added before,
1124 * then we don't create a new edge, but instead mark the original edge
1125 * as also representing a dependence of the current type.
1127 * Edges of type isl_edge_condition or isl_edge_conditional_validity
1128 * may be specified as "tagged" dependence relations. That is, "map"
1129 * may contain elements (i -> a) -> (j -> b), where i -> j denotes
1130 * the dependence on iterations and a and b are tags.
1131 * edge->map is set to the relation containing the elements i -> j,
1132 * while edge->tagged_condition and edge->tagged_validity contain
1133 * the union of all the "map" relations
1134 * for which extract_edge is called that result in the same edge->map.
1136 * If the source or the destination node is compressed, then
1137 * intersect both "map" and "tagged" with the constraints that
1138 * were used to construct the compression.
1139 * This ensures that there are no schedule constraints defined
1140 * outside of these domains, while the scheduler no longer has
1141 * any control over those outside parts.
1143 static int extract_edge(__isl_take isl_map *map, void *user)
1145 isl_ctx *ctx = isl_map_get_ctx(map);
1146 struct isl_extract_edge_data *data = user;
1147 struct isl_sched_graph *graph = data->graph;
1148 struct isl_sched_node *src, *dst;
1149 isl_space *dim;
1150 struct isl_sched_edge *edge;
1151 isl_map *tagged = NULL;
1153 if (data->type == isl_edge_condition ||
1154 data->type == isl_edge_conditional_validity) {
1155 if (isl_map_can_zip(map)) {
1156 tagged = isl_map_copy(map);
1157 map = isl_set_unwrap(isl_map_domain(isl_map_zip(map)));
1158 } else {
1159 tagged = insert_dummy_tags(isl_map_copy(map));
1163 dim = isl_space_domain(isl_map_get_space(map));
1164 src = graph_find_node(ctx, graph, dim);
1165 isl_space_free(dim);
1166 dim = isl_space_range(isl_map_get_space(map));
1167 dst = graph_find_node(ctx, graph, dim);
1168 isl_space_free(dim);
1170 if (!src || !dst) {
1171 isl_map_free(map);
1172 isl_map_free(tagged);
1173 return 0;
1176 if (src->compressed || dst->compressed) {
1177 isl_map *hull;
1178 hull = extract_hull(src, dst);
1179 if (tagged)
1180 tagged = map_intersect_domains(tagged, hull);
1181 map = isl_map_intersect(map, hull);
1184 graph->edge[graph->n_edge].src = src;
1185 graph->edge[graph->n_edge].dst = dst;
1186 graph->edge[graph->n_edge].map = map;
1187 graph->edge[graph->n_edge].validity = 0;
1188 graph->edge[graph->n_edge].coincidence = 0;
1189 graph->edge[graph->n_edge].proximity = 0;
1190 graph->edge[graph->n_edge].condition = 0;
1191 graph->edge[graph->n_edge].local = 0;
1192 graph->edge[graph->n_edge].conditional_validity = 0;
1193 graph->edge[graph->n_edge].tagged_condition = NULL;
1194 graph->edge[graph->n_edge].tagged_validity = NULL;
1195 if (data->type == isl_edge_validity)
1196 graph->edge[graph->n_edge].validity = 1;
1197 if (data->type == isl_edge_coincidence)
1198 graph->edge[graph->n_edge].coincidence = 1;
1199 if (data->type == isl_edge_proximity)
1200 graph->edge[graph->n_edge].proximity = 1;
1201 if (data->type == isl_edge_condition) {
1202 graph->edge[graph->n_edge].condition = 1;
1203 graph->edge[graph->n_edge].tagged_condition =
1204 isl_union_map_from_map(tagged);
1206 if (data->type == isl_edge_conditional_validity) {
1207 graph->edge[graph->n_edge].conditional_validity = 1;
1208 graph->edge[graph->n_edge].tagged_validity =
1209 isl_union_map_from_map(tagged);
1212 edge = graph_find_matching_edge(graph, &graph->edge[graph->n_edge]);
1213 if (!edge) {
1214 graph->n_edge++;
1215 return -1;
1217 if (edge == &graph->edge[graph->n_edge])
1218 return graph_edge_table_add(ctx, graph, data->type,
1219 &graph->edge[graph->n_edge++]);
1221 if (merge_edge(data->type, edge, &graph->edge[graph->n_edge]) < 0)
1222 return -1;
1224 return graph_edge_table_add(ctx, graph, data->type, edge);
1227 /* Check whether there is any dependence from node[j] to node[i]
1228 * or from node[i] to node[j].
1230 static int node_follows_weak(int i, int j, void *user)
1232 int f;
1233 struct isl_sched_graph *graph = user;
1235 f = graph_has_any_edge(graph, &graph->node[j], &graph->node[i]);
1236 if (f < 0 || f)
1237 return f;
1238 return graph_has_any_edge(graph, &graph->node[i], &graph->node[j]);
1241 /* Check whether there is a (conditional) validity dependence from node[j]
1242 * to node[i], forcing node[i] to follow node[j].
1244 static int node_follows_strong(int i, int j, void *user)
1246 struct isl_sched_graph *graph = user;
1248 return graph_has_validity_edge(graph, &graph->node[j], &graph->node[i]);
1251 /* Use Tarjan's algorithm for computing the strongly connected components
1252 * in the dependence graph (only validity edges).
1253 * If weak is set, we consider the graph to be undirected and
1254 * we effectively compute the (weakly) connected components.
1255 * Additionally, we also consider other edges when weak is set.
1257 static int detect_ccs(isl_ctx *ctx, struct isl_sched_graph *graph, int weak)
1259 int i, n;
1260 struct isl_tarjan_graph *g = NULL;
1262 g = isl_tarjan_graph_init(ctx, graph->n,
1263 weak ? &node_follows_weak : &node_follows_strong, graph);
1264 if (!g)
1265 return -1;
1267 graph->weak = weak;
1268 graph->scc = 0;
1269 i = 0;
1270 n = graph->n;
1271 while (n) {
1272 while (g->order[i] != -1) {
1273 graph->node[g->order[i]].scc = graph->scc;
1274 --n;
1275 ++i;
1277 ++i;
1278 graph->scc++;
1281 isl_tarjan_graph_free(g);
1283 return 0;
1286 /* Apply Tarjan's algorithm to detect the strongly connected components
1287 * in the dependence graph.
1289 static int detect_sccs(isl_ctx *ctx, struct isl_sched_graph *graph)
1291 return detect_ccs(ctx, graph, 0);
1294 /* Apply Tarjan's algorithm to detect the (weakly) connected components
1295 * in the dependence graph.
1297 static int detect_wccs(isl_ctx *ctx, struct isl_sched_graph *graph)
1299 return detect_ccs(ctx, graph, 1);
1302 static int cmp_scc(const void *a, const void *b, void *data)
1304 struct isl_sched_graph *graph = data;
1305 const int *i1 = a;
1306 const int *i2 = b;
1308 return graph->node[*i1].scc - graph->node[*i2].scc;
1311 /* Sort the elements of graph->sorted according to the corresponding SCCs.
1313 static int sort_sccs(struct isl_sched_graph *graph)
1315 return isl_sort(graph->sorted, graph->n, sizeof(int), &cmp_scc, graph);
1318 /* Given a dependence relation R from "node" to itself,
1319 * construct the set of coefficients of valid constraints for elements
1320 * in that dependence relation.
1321 * In particular, the result contains tuples of coefficients
1322 * c_0, c_n, c_x such that
1324 * c_0 + c_n n + c_x y - c_x x >= 0 for each (x,y) in R
1326 * or, equivalently,
1328 * c_0 + c_n n + c_x d >= 0 for each d in delta R = { y - x | (x,y) in R }
1330 * We choose here to compute the dual of delta R.
1331 * Alternatively, we could have computed the dual of R, resulting
1332 * in a set of tuples c_0, c_n, c_x, c_y, and then
1333 * plugged in (c_0, c_n, c_x, -c_x).
1335 * If "node" has been compressed, then the dependence relation
1336 * is also compressed before the set of coefficients is computed.
1338 static __isl_give isl_basic_set *intra_coefficients(
1339 struct isl_sched_graph *graph, struct isl_sched_node *node,
1340 __isl_take isl_map *map)
1342 isl_set *delta;
1343 isl_map *key;
1344 isl_basic_set *coef;
1346 if (isl_map_to_basic_set_has(graph->intra_hmap, map))
1347 return isl_map_to_basic_set_get(graph->intra_hmap, map);
1349 key = isl_map_copy(map);
1350 if (node->compressed) {
1351 map = isl_map_preimage_domain_multi_aff(map,
1352 isl_multi_aff_copy(node->decompress));
1353 map = isl_map_preimage_range_multi_aff(map,
1354 isl_multi_aff_copy(node->decompress));
1356 delta = isl_set_remove_divs(isl_map_deltas(map));
1357 coef = isl_set_coefficients(delta);
1358 graph->intra_hmap = isl_map_to_basic_set_set(graph->intra_hmap, key,
1359 isl_basic_set_copy(coef));
1361 return coef;
1364 /* Given a dependence relation R, construct the set of coefficients
1365 * of valid constraints for elements in that dependence relation.
1366 * In particular, the result contains tuples of coefficients
1367 * c_0, c_n, c_x, c_y such that
1369 * c_0 + c_n n + c_x x + c_y y >= 0 for each (x,y) in R
1371 * If the source or destination nodes of "edge" have been compressed,
1372 * then the dependence relation is also compressed before
1373 * the set of coefficients is computed.
1375 static __isl_give isl_basic_set *inter_coefficients(
1376 struct isl_sched_graph *graph, struct isl_sched_edge *edge,
1377 __isl_take isl_map *map)
1379 isl_set *set;
1380 isl_map *key;
1381 isl_basic_set *coef;
1383 if (isl_map_to_basic_set_has(graph->inter_hmap, map))
1384 return isl_map_to_basic_set_get(graph->inter_hmap, map);
1386 key = isl_map_copy(map);
1387 if (edge->src->compressed)
1388 map = isl_map_preimage_domain_multi_aff(map,
1389 isl_multi_aff_copy(edge->src->decompress));
1390 if (edge->dst->compressed)
1391 map = isl_map_preimage_range_multi_aff(map,
1392 isl_multi_aff_copy(edge->dst->decompress));
1393 set = isl_map_wrap(isl_map_remove_divs(map));
1394 coef = isl_set_coefficients(set);
1395 graph->inter_hmap = isl_map_to_basic_set_set(graph->inter_hmap, key,
1396 isl_basic_set_copy(coef));
1398 return coef;
1401 /* Add constraints to graph->lp that force validity for the given
1402 * dependence from a node i to itself.
1403 * That is, add constraints that enforce
1405 * (c_i_0 + c_i_n n + c_i_x y) - (c_i_0 + c_i_n n + c_i_x x)
1406 * = c_i_x (y - x) >= 0
1408 * for each (x,y) in R.
1409 * We obtain general constraints on coefficients (c_0, c_n, c_x)
1410 * of valid constraints for (y - x) and then plug in (0, 0, c_i_x^+ - c_i_x^-),
1411 * where c_i_x = c_i_x^+ - c_i_x^-, with c_i_x^+ and c_i_x^- non-negative.
1412 * In graph->lp, the c_i_x^- appear before their c_i_x^+ counterpart.
1414 * Actually, we do not construct constraints for the c_i_x themselves,
1415 * but for the coefficients of c_i_x written as a linear combination
1416 * of the columns in node->cmap.
1418 static int add_intra_validity_constraints(struct isl_sched_graph *graph,
1419 struct isl_sched_edge *edge)
1421 unsigned total;
1422 isl_map *map = isl_map_copy(edge->map);
1423 isl_ctx *ctx = isl_map_get_ctx(map);
1424 isl_space *dim;
1425 isl_dim_map *dim_map;
1426 isl_basic_set *coef;
1427 struct isl_sched_node *node = edge->src;
1429 coef = intra_coefficients(graph, node, map);
1431 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
1433 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
1434 isl_space_dim(dim, isl_dim_set), isl_mat_copy(node->cmap));
1435 if (!coef)
1436 goto error;
1438 total = isl_basic_set_total_dim(graph->lp);
1439 dim_map = isl_dim_map_alloc(ctx, total);
1440 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 1, 2,
1441 isl_space_dim(dim, isl_dim_set), 1,
1442 node->nvar, -1);
1443 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 2, 2,
1444 isl_space_dim(dim, isl_dim_set), 1,
1445 node->nvar, 1);
1446 graph->lp = isl_basic_set_extend_constraints(graph->lp,
1447 coef->n_eq, coef->n_ineq);
1448 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
1449 coef, dim_map);
1450 isl_space_free(dim);
1452 return 0;
1453 error:
1454 isl_space_free(dim);
1455 return -1;
1458 /* Add constraints to graph->lp that force validity for the given
1459 * dependence from node i to node j.
1460 * That is, add constraints that enforce
1462 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x) >= 0
1464 * for each (x,y) in R.
1465 * We obtain general constraints on coefficients (c_0, c_n, c_x, c_y)
1466 * of valid constraints for R and then plug in
1467 * (c_j_0 - c_i_0, c_j_n^+ - c_j_n^- - (c_i_n^+ - c_i_n^-),
1468 * c_j_x^+ - c_j_x^- - (c_i_x^+ - c_i_x^-)),
1469 * where c_* = c_*^+ - c_*^-, with c_*^+ and c_*^- non-negative.
1470 * In graph->lp, the c_*^- appear before their c_*^+ counterpart.
1472 * Actually, we do not construct constraints for the c_*_x themselves,
1473 * but for the coefficients of c_*_x written as a linear combination
1474 * of the columns in node->cmap.
1476 static int add_inter_validity_constraints(struct isl_sched_graph *graph,
1477 struct isl_sched_edge *edge)
1479 unsigned total;
1480 isl_map *map = isl_map_copy(edge->map);
1481 isl_ctx *ctx = isl_map_get_ctx(map);
1482 isl_space *dim;
1483 isl_dim_map *dim_map;
1484 isl_basic_set *coef;
1485 struct isl_sched_node *src = edge->src;
1486 struct isl_sched_node *dst = edge->dst;
1488 coef = inter_coefficients(graph, edge, map);
1490 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
1492 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
1493 isl_space_dim(dim, isl_dim_set), isl_mat_copy(src->cmap));
1494 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
1495 isl_space_dim(dim, isl_dim_set) + src->nvar,
1496 isl_mat_copy(dst->cmap));
1497 if (!coef)
1498 goto error;
1500 total = isl_basic_set_total_dim(graph->lp);
1501 dim_map = isl_dim_map_alloc(ctx, total);
1503 isl_dim_map_range(dim_map, dst->start, 0, 0, 0, 1, 1);
1504 isl_dim_map_range(dim_map, dst->start + 1, 2, 1, 1, dst->nparam, -1);
1505 isl_dim_map_range(dim_map, dst->start + 2, 2, 1, 1, dst->nparam, 1);
1506 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 1, 2,
1507 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
1508 dst->nvar, -1);
1509 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 2, 2,
1510 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
1511 dst->nvar, 1);
1513 isl_dim_map_range(dim_map, src->start, 0, 0, 0, 1, -1);
1514 isl_dim_map_range(dim_map, src->start + 1, 2, 1, 1, src->nparam, 1);
1515 isl_dim_map_range(dim_map, src->start + 2, 2, 1, 1, src->nparam, -1);
1516 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 1, 2,
1517 isl_space_dim(dim, isl_dim_set), 1,
1518 src->nvar, 1);
1519 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 2, 2,
1520 isl_space_dim(dim, isl_dim_set), 1,
1521 src->nvar, -1);
1523 edge->start = graph->lp->n_ineq;
1524 graph->lp = isl_basic_set_extend_constraints(graph->lp,
1525 coef->n_eq, coef->n_ineq);
1526 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
1527 coef, dim_map);
1528 if (!graph->lp)
1529 goto error;
1530 isl_space_free(dim);
1531 edge->end = graph->lp->n_ineq;
1533 return 0;
1534 error:
1535 isl_space_free(dim);
1536 return -1;
1539 /* Add constraints to graph->lp that bound the dependence distance for the given
1540 * dependence from a node i to itself.
1541 * If s = 1, we add the constraint
1543 * c_i_x (y - x) <= m_0 + m_n n
1545 * or
1547 * -c_i_x (y - x) + m_0 + m_n n >= 0
1549 * for each (x,y) in R.
1550 * If s = -1, we add the constraint
1552 * -c_i_x (y - x) <= m_0 + m_n n
1554 * or
1556 * c_i_x (y - x) + m_0 + m_n n >= 0
1558 * for each (x,y) in R.
1559 * We obtain general constraints on coefficients (c_0, c_n, c_x)
1560 * of valid constraints for (y - x) and then plug in (m_0, m_n, -s * c_i_x),
1561 * with each coefficient (except m_0) represented as a pair of non-negative
1562 * coefficients.
1564 * Actually, we do not construct constraints for the c_i_x themselves,
1565 * but for the coefficients of c_i_x written as a linear combination
1566 * of the columns in node->cmap.
1569 * If "local" is set, then we add constraints
1571 * c_i_x (y - x) <= 0
1573 * or
1575 * -c_i_x (y - x) <= 0
1577 * instead, forcing the dependence distance to be (less than or) equal to 0.
1578 * That is, we plug in (0, 0, -s * c_i_x),
1579 * Note that dependences marked local are treated as validity constraints
1580 * by add_all_validity_constraints and therefore also have
1581 * their distances bounded by 0 from below.
1583 static int add_intra_proximity_constraints(struct isl_sched_graph *graph,
1584 struct isl_sched_edge *edge, int s, int local)
1586 unsigned total;
1587 unsigned nparam;
1588 isl_map *map = isl_map_copy(edge->map);
1589 isl_ctx *ctx = isl_map_get_ctx(map);
1590 isl_space *dim;
1591 isl_dim_map *dim_map;
1592 isl_basic_set *coef;
1593 struct isl_sched_node *node = edge->src;
1595 coef = intra_coefficients(graph, node, map);
1597 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
1599 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
1600 isl_space_dim(dim, isl_dim_set), isl_mat_copy(node->cmap));
1601 if (!coef)
1602 goto error;
1604 nparam = isl_space_dim(node->space, isl_dim_param);
1605 total = isl_basic_set_total_dim(graph->lp);
1606 dim_map = isl_dim_map_alloc(ctx, total);
1608 if (!local) {
1609 isl_dim_map_range(dim_map, 1, 0, 0, 0, 1, 1);
1610 isl_dim_map_range(dim_map, 4, 2, 1, 1, nparam, -1);
1611 isl_dim_map_range(dim_map, 5, 2, 1, 1, nparam, 1);
1613 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 1, 2,
1614 isl_space_dim(dim, isl_dim_set), 1,
1615 node->nvar, s);
1616 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 2, 2,
1617 isl_space_dim(dim, isl_dim_set), 1,
1618 node->nvar, -s);
1619 graph->lp = isl_basic_set_extend_constraints(graph->lp,
1620 coef->n_eq, coef->n_ineq);
1621 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
1622 coef, dim_map);
1623 isl_space_free(dim);
1625 return 0;
1626 error:
1627 isl_space_free(dim);
1628 return -1;
1631 /* Add constraints to graph->lp that bound the dependence distance for the given
1632 * dependence from node i to node j.
1633 * If s = 1, we add the constraint
1635 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x)
1636 * <= m_0 + m_n n
1638 * or
1640 * -(c_j_0 + c_j_n n + c_j_x y) + (c_i_0 + c_i_n n + c_i_x x) +
1641 * m_0 + m_n n >= 0
1643 * for each (x,y) in R.
1644 * If s = -1, we add the constraint
1646 * -((c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x))
1647 * <= m_0 + m_n n
1649 * or
1651 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x) +
1652 * m_0 + m_n n >= 0
1654 * for each (x,y) in R.
1655 * We obtain general constraints on coefficients (c_0, c_n, c_x, c_y)
1656 * of valid constraints for R and then plug in
1657 * (m_0 - s*c_j_0 + s*c_i_0, m_n - s*c_j_n + s*c_i_n,
1658 * -s*c_j_x+s*c_i_x)
1659 * with each coefficient (except m_0, c_j_0 and c_i_0)
1660 * represented as a pair of non-negative coefficients.
1662 * Actually, we do not construct constraints for the c_*_x themselves,
1663 * but for the coefficients of c_*_x written as a linear combination
1664 * of the columns in node->cmap.
1667 * If "local" is set, then we add constraints
1669 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x) <= 0
1671 * or
1673 * -((c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x)) <= 0
1675 * instead, forcing the dependence distance to be (less than or) equal to 0.
1676 * That is, we plug in
1677 * (-s*c_j_0 + s*c_i_0, -s*c_j_n + s*c_i_n, -s*c_j_x+s*c_i_x).
1678 * Note that dependences marked local are treated as validity constraints
1679 * by add_all_validity_constraints and therefore also have
1680 * their distances bounded by 0 from below.
1682 static int add_inter_proximity_constraints(struct isl_sched_graph *graph,
1683 struct isl_sched_edge *edge, int s, int local)
1685 unsigned total;
1686 unsigned nparam;
1687 isl_map *map = isl_map_copy(edge->map);
1688 isl_ctx *ctx = isl_map_get_ctx(map);
1689 isl_space *dim;
1690 isl_dim_map *dim_map;
1691 isl_basic_set *coef;
1692 struct isl_sched_node *src = edge->src;
1693 struct isl_sched_node *dst = edge->dst;
1695 coef = inter_coefficients(graph, edge, map);
1697 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
1699 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
1700 isl_space_dim(dim, isl_dim_set), isl_mat_copy(src->cmap));
1701 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
1702 isl_space_dim(dim, isl_dim_set) + src->nvar,
1703 isl_mat_copy(dst->cmap));
1704 if (!coef)
1705 goto error;
1707 nparam = isl_space_dim(src->space, isl_dim_param);
1708 total = isl_basic_set_total_dim(graph->lp);
1709 dim_map = isl_dim_map_alloc(ctx, total);
1711 if (!local) {
1712 isl_dim_map_range(dim_map, 1, 0, 0, 0, 1, 1);
1713 isl_dim_map_range(dim_map, 4, 2, 1, 1, nparam, -1);
1714 isl_dim_map_range(dim_map, 5, 2, 1, 1, nparam, 1);
1717 isl_dim_map_range(dim_map, dst->start, 0, 0, 0, 1, -s);
1718 isl_dim_map_range(dim_map, dst->start + 1, 2, 1, 1, dst->nparam, s);
1719 isl_dim_map_range(dim_map, dst->start + 2, 2, 1, 1, dst->nparam, -s);
1720 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 1, 2,
1721 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
1722 dst->nvar, s);
1723 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 2, 2,
1724 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
1725 dst->nvar, -s);
1727 isl_dim_map_range(dim_map, src->start, 0, 0, 0, 1, s);
1728 isl_dim_map_range(dim_map, src->start + 1, 2, 1, 1, src->nparam, -s);
1729 isl_dim_map_range(dim_map, src->start + 2, 2, 1, 1, src->nparam, s);
1730 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 1, 2,
1731 isl_space_dim(dim, isl_dim_set), 1,
1732 src->nvar, -s);
1733 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 2, 2,
1734 isl_space_dim(dim, isl_dim_set), 1,
1735 src->nvar, s);
1737 graph->lp = isl_basic_set_extend_constraints(graph->lp,
1738 coef->n_eq, coef->n_ineq);
1739 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
1740 coef, dim_map);
1741 isl_space_free(dim);
1743 return 0;
1744 error:
1745 isl_space_free(dim);
1746 return -1;
1749 /* Add all validity constraints to graph->lp.
1751 * An edge that is forced to be local needs to have its dependence
1752 * distances equal to zero. We take care of bounding them by 0 from below
1753 * here. add_all_proximity_constraints takes care of bounding them by 0
1754 * from above.
1756 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
1757 * Otherwise, we ignore them.
1759 static int add_all_validity_constraints(struct isl_sched_graph *graph,
1760 int use_coincidence)
1762 int i;
1764 for (i = 0; i < graph->n_edge; ++i) {
1765 struct isl_sched_edge *edge= &graph->edge[i];
1766 int local;
1768 local = edge->local || (edge->coincidence && use_coincidence);
1769 if (!edge->validity && !local)
1770 continue;
1771 if (edge->src != edge->dst)
1772 continue;
1773 if (add_intra_validity_constraints(graph, edge) < 0)
1774 return -1;
1777 for (i = 0; i < graph->n_edge; ++i) {
1778 struct isl_sched_edge *edge = &graph->edge[i];
1779 int local;
1781 local = edge->local || (edge->coincidence && use_coincidence);
1782 if (!edge->validity && !local)
1783 continue;
1784 if (edge->src == edge->dst)
1785 continue;
1786 if (add_inter_validity_constraints(graph, edge) < 0)
1787 return -1;
1790 return 0;
1793 /* Add constraints to graph->lp that bound the dependence distance
1794 * for all dependence relations.
1795 * If a given proximity dependence is identical to a validity
1796 * dependence, then the dependence distance is already bounded
1797 * from below (by zero), so we only need to bound the distance
1798 * from above. (This includes the case of "local" dependences
1799 * which are treated as validity dependence by add_all_validity_constraints.)
1800 * Otherwise, we need to bound the distance both from above and from below.
1802 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
1803 * Otherwise, we ignore them.
1805 static int add_all_proximity_constraints(struct isl_sched_graph *graph,
1806 int use_coincidence)
1808 int i;
1810 for (i = 0; i < graph->n_edge; ++i) {
1811 struct isl_sched_edge *edge= &graph->edge[i];
1812 int local;
1814 local = edge->local || (edge->coincidence && use_coincidence);
1815 if (!edge->proximity && !local)
1816 continue;
1817 if (edge->src == edge->dst &&
1818 add_intra_proximity_constraints(graph, edge, 1, local) < 0)
1819 return -1;
1820 if (edge->src != edge->dst &&
1821 add_inter_proximity_constraints(graph, edge, 1, local) < 0)
1822 return -1;
1823 if (edge->validity || local)
1824 continue;
1825 if (edge->src == edge->dst &&
1826 add_intra_proximity_constraints(graph, edge, -1, 0) < 0)
1827 return -1;
1828 if (edge->src != edge->dst &&
1829 add_inter_proximity_constraints(graph, edge, -1, 0) < 0)
1830 return -1;
1833 return 0;
1836 /* Compute a basis for the rows in the linear part of the schedule
1837 * and extend this basis to a full basis. The remaining rows
1838 * can then be used to force linear independence from the rows
1839 * in the schedule.
1841 * In particular, given the schedule rows S, we compute
1843 * S = H Q
1844 * S U = H
1846 * with H the Hermite normal form of S. That is, all but the
1847 * first rank columns of H are zero and so each row in S is
1848 * a linear combination of the first rank rows of Q.
1849 * The matrix Q is then transposed because we will write the
1850 * coefficients of the next schedule row as a column vector s
1851 * and express this s as a linear combination s = Q c of the
1852 * computed basis.
1853 * Similarly, the matrix U is transposed such that we can
1854 * compute the coefficients c = U s from a schedule row s.
1856 static int node_update_cmap(struct isl_sched_node *node)
1858 isl_mat *H, *U, *Q;
1859 int n_row = isl_mat_rows(node->sched);
1861 H = isl_mat_sub_alloc(node->sched, 0, n_row,
1862 1 + node->nparam, node->nvar);
1864 H = isl_mat_left_hermite(H, 0, &U, &Q);
1865 isl_mat_free(node->cmap);
1866 isl_mat_free(node->cinv);
1867 node->cmap = isl_mat_transpose(Q);
1868 node->cinv = isl_mat_transpose(U);
1869 node->rank = isl_mat_initial_non_zero_cols(H);
1870 isl_mat_free(H);
1872 if (!node->cmap || !node->cinv || node->rank < 0)
1873 return -1;
1874 return 0;
1877 /* How many times should we count the constraints in "edge"?
1879 * If carry is set, then we are counting the number of
1880 * (validity or conditional validity) constraints that will be added
1881 * in setup_carry_lp and we count each edge exactly once.
1883 * Otherwise, we count as follows
1884 * validity -> 1 (>= 0)
1885 * validity+proximity -> 2 (>= 0 and upper bound)
1886 * proximity -> 2 (lower and upper bound)
1887 * local(+any) -> 2 (>= 0 and <= 0)
1889 * If an edge is only marked conditional_validity then it counts
1890 * as zero since it is only checked afterwards.
1892 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
1893 * Otherwise, we ignore them.
1895 static int edge_multiplicity(struct isl_sched_edge *edge, int carry,
1896 int use_coincidence)
1898 if (carry && !edge->validity && !edge->conditional_validity)
1899 return 0;
1900 if (carry)
1901 return 1;
1902 if (edge->proximity || edge->local)
1903 return 2;
1904 if (use_coincidence && edge->coincidence)
1905 return 2;
1906 if (edge->validity)
1907 return 1;
1908 return 0;
1911 /* Count the number of equality and inequality constraints
1912 * that will be added for the given map.
1914 * "use_coincidence" is set if we should take into account coincidence edges.
1916 static int count_map_constraints(struct isl_sched_graph *graph,
1917 struct isl_sched_edge *edge, __isl_take isl_map *map,
1918 int *n_eq, int *n_ineq, int carry, int use_coincidence)
1920 isl_basic_set *coef;
1921 int f = edge_multiplicity(edge, carry, use_coincidence);
1923 if (f == 0) {
1924 isl_map_free(map);
1925 return 0;
1928 if (edge->src == edge->dst)
1929 coef = intra_coefficients(graph, edge->src, map);
1930 else
1931 coef = inter_coefficients(graph, edge, map);
1932 if (!coef)
1933 return -1;
1934 *n_eq += f * coef->n_eq;
1935 *n_ineq += f * coef->n_ineq;
1936 isl_basic_set_free(coef);
1938 return 0;
1941 /* Count the number of equality and inequality constraints
1942 * that will be added to the main lp problem.
1943 * We count as follows
1944 * validity -> 1 (>= 0)
1945 * validity+proximity -> 2 (>= 0 and upper bound)
1946 * proximity -> 2 (lower and upper bound)
1947 * local(+any) -> 2 (>= 0 and <= 0)
1949 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
1950 * Otherwise, we ignore them.
1952 static int count_constraints(struct isl_sched_graph *graph,
1953 int *n_eq, int *n_ineq, int use_coincidence)
1955 int i;
1957 *n_eq = *n_ineq = 0;
1958 for (i = 0; i < graph->n_edge; ++i) {
1959 struct isl_sched_edge *edge= &graph->edge[i];
1960 isl_map *map = isl_map_copy(edge->map);
1962 if (count_map_constraints(graph, edge, map, n_eq, n_ineq,
1963 0, use_coincidence) < 0)
1964 return -1;
1967 return 0;
1970 /* Count the number of constraints that will be added by
1971 * add_bound_coefficient_constraints and increment *n_eq and *n_ineq
1972 * accordingly.
1974 * In practice, add_bound_coefficient_constraints only adds inequalities.
1976 static int count_bound_coefficient_constraints(isl_ctx *ctx,
1977 struct isl_sched_graph *graph, int *n_eq, int *n_ineq)
1979 int i;
1981 if (ctx->opt->schedule_max_coefficient == -1)
1982 return 0;
1984 for (i = 0; i < graph->n; ++i)
1985 *n_ineq += 2 * graph->node[i].nparam + 2 * graph->node[i].nvar;
1987 return 0;
1990 /* Add constraints that bound the values of the variable and parameter
1991 * coefficients of the schedule.
1993 * The maximal value of the coefficients is defined by the option
1994 * 'schedule_max_coefficient'.
1996 static int add_bound_coefficient_constraints(isl_ctx *ctx,
1997 struct isl_sched_graph *graph)
1999 int i, j, k;
2000 int max_coefficient;
2001 int total;
2003 max_coefficient = ctx->opt->schedule_max_coefficient;
2005 if (max_coefficient == -1)
2006 return 0;
2008 total = isl_basic_set_total_dim(graph->lp);
2010 for (i = 0; i < graph->n; ++i) {
2011 struct isl_sched_node *node = &graph->node[i];
2012 for (j = 0; j < 2 * node->nparam + 2 * node->nvar; ++j) {
2013 int dim;
2014 k = isl_basic_set_alloc_inequality(graph->lp);
2015 if (k < 0)
2016 return -1;
2017 dim = 1 + node->start + 1 + j;
2018 isl_seq_clr(graph->lp->ineq[k], 1 + total);
2019 isl_int_set_si(graph->lp->ineq[k][dim], -1);
2020 isl_int_set_si(graph->lp->ineq[k][0], max_coefficient);
2024 return 0;
2027 /* Construct an ILP problem for finding schedule coefficients
2028 * that result in non-negative, but small dependence distances
2029 * over all dependences.
2030 * In particular, the dependence distances over proximity edges
2031 * are bounded by m_0 + m_n n and we compute schedule coefficients
2032 * with small values (preferably zero) of m_n and m_0.
2034 * All variables of the ILP are non-negative. The actual coefficients
2035 * may be negative, so each coefficient is represented as the difference
2036 * of two non-negative variables. The negative part always appears
2037 * immediately before the positive part.
2038 * Other than that, the variables have the following order
2040 * - sum of positive and negative parts of m_n coefficients
2041 * - m_0
2042 * - sum of positive and negative parts of all c_n coefficients
2043 * (unconstrained when computing non-parametric schedules)
2044 * - sum of positive and negative parts of all c_x coefficients
2045 * - positive and negative parts of m_n coefficients
2046 * - for each node
2047 * - c_i_0
2048 * - positive and negative parts of c_i_n (if parametric)
2049 * - positive and negative parts of c_i_x
2051 * The c_i_x are not represented directly, but through the columns of
2052 * node->cmap. That is, the computed values are for variable t_i_x
2053 * such that c_i_x = Q t_i_x with Q equal to node->cmap.
2055 * The constraints are those from the edges plus two or three equalities
2056 * to express the sums.
2058 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
2059 * Otherwise, we ignore them.
2061 static int setup_lp(isl_ctx *ctx, struct isl_sched_graph *graph,
2062 int use_coincidence)
2064 int i, j;
2065 int k;
2066 unsigned nparam;
2067 unsigned total;
2068 isl_space *dim;
2069 int parametric;
2070 int param_pos;
2071 int n_eq, n_ineq;
2072 int max_constant_term;
2074 max_constant_term = ctx->opt->schedule_max_constant_term;
2076 parametric = ctx->opt->schedule_parametric;
2077 nparam = isl_space_dim(graph->node[0].space, isl_dim_param);
2078 param_pos = 4;
2079 total = param_pos + 2 * nparam;
2080 for (i = 0; i < graph->n; ++i) {
2081 struct isl_sched_node *node = &graph->node[graph->sorted[i]];
2082 if (node_update_cmap(node) < 0)
2083 return -1;
2084 node->start = total;
2085 total += 1 + 2 * (node->nparam + node->nvar);
2088 if (count_constraints(graph, &n_eq, &n_ineq, use_coincidence) < 0)
2089 return -1;
2090 if (count_bound_coefficient_constraints(ctx, graph, &n_eq, &n_ineq) < 0)
2091 return -1;
2093 dim = isl_space_set_alloc(ctx, 0, total);
2094 isl_basic_set_free(graph->lp);
2095 n_eq += 2 + parametric;
2096 if (max_constant_term != -1)
2097 n_ineq += graph->n;
2099 graph->lp = isl_basic_set_alloc_space(dim, 0, n_eq, n_ineq);
2101 k = isl_basic_set_alloc_equality(graph->lp);
2102 if (k < 0)
2103 return -1;
2104 isl_seq_clr(graph->lp->eq[k], 1 + total);
2105 isl_int_set_si(graph->lp->eq[k][1], -1);
2106 for (i = 0; i < 2 * nparam; ++i)
2107 isl_int_set_si(graph->lp->eq[k][1 + param_pos + i], 1);
2109 if (parametric) {
2110 k = isl_basic_set_alloc_equality(graph->lp);
2111 if (k < 0)
2112 return -1;
2113 isl_seq_clr(graph->lp->eq[k], 1 + total);
2114 isl_int_set_si(graph->lp->eq[k][3], -1);
2115 for (i = 0; i < graph->n; ++i) {
2116 int pos = 1 + graph->node[i].start + 1;
2118 for (j = 0; j < 2 * graph->node[i].nparam; ++j)
2119 isl_int_set_si(graph->lp->eq[k][pos + j], 1);
2123 k = isl_basic_set_alloc_equality(graph->lp);
2124 if (k < 0)
2125 return -1;
2126 isl_seq_clr(graph->lp->eq[k], 1 + total);
2127 isl_int_set_si(graph->lp->eq[k][4], -1);
2128 for (i = 0; i < graph->n; ++i) {
2129 struct isl_sched_node *node = &graph->node[i];
2130 int pos = 1 + node->start + 1 + 2 * node->nparam;
2132 for (j = 0; j < 2 * node->nvar; ++j)
2133 isl_int_set_si(graph->lp->eq[k][pos + j], 1);
2136 if (max_constant_term != -1)
2137 for (i = 0; i < graph->n; ++i) {
2138 struct isl_sched_node *node = &graph->node[i];
2139 k = isl_basic_set_alloc_inequality(graph->lp);
2140 if (k < 0)
2141 return -1;
2142 isl_seq_clr(graph->lp->ineq[k], 1 + total);
2143 isl_int_set_si(graph->lp->ineq[k][1 + node->start], -1);
2144 isl_int_set_si(graph->lp->ineq[k][0], max_constant_term);
2147 if (add_bound_coefficient_constraints(ctx, graph) < 0)
2148 return -1;
2149 if (add_all_validity_constraints(graph, use_coincidence) < 0)
2150 return -1;
2151 if (add_all_proximity_constraints(graph, use_coincidence) < 0)
2152 return -1;
2154 return 0;
2157 /* Analyze the conflicting constraint found by
2158 * isl_tab_basic_set_non_trivial_lexmin. If it corresponds to the validity
2159 * constraint of one of the edges between distinct nodes, living, moreover
2160 * in distinct SCCs, then record the source and sink SCC as this may
2161 * be a good place to cut between SCCs.
2163 static int check_conflict(int con, void *user)
2165 int i;
2166 struct isl_sched_graph *graph = user;
2168 if (graph->src_scc >= 0)
2169 return 0;
2171 con -= graph->lp->n_eq;
2173 if (con >= graph->lp->n_ineq)
2174 return 0;
2176 for (i = 0; i < graph->n_edge; ++i) {
2177 if (!graph->edge[i].validity)
2178 continue;
2179 if (graph->edge[i].src == graph->edge[i].dst)
2180 continue;
2181 if (graph->edge[i].src->scc == graph->edge[i].dst->scc)
2182 continue;
2183 if (graph->edge[i].start > con)
2184 continue;
2185 if (graph->edge[i].end <= con)
2186 continue;
2187 graph->src_scc = graph->edge[i].src->scc;
2188 graph->dst_scc = graph->edge[i].dst->scc;
2191 return 0;
2194 /* Check whether the next schedule row of the given node needs to be
2195 * non-trivial. Lower-dimensional domains may have some trivial rows,
2196 * but as soon as the number of remaining required non-trivial rows
2197 * is as large as the number or remaining rows to be computed,
2198 * all remaining rows need to be non-trivial.
2200 static int needs_row(struct isl_sched_graph *graph, struct isl_sched_node *node)
2202 return node->nvar - node->rank >= graph->maxvar - graph->n_row;
2205 /* Solve the ILP problem constructed in setup_lp.
2206 * For each node such that all the remaining rows of its schedule
2207 * need to be non-trivial, we construct a non-triviality region.
2208 * This region imposes that the next row is independent of previous rows.
2209 * In particular the coefficients c_i_x are represented by t_i_x
2210 * variables with c_i_x = Q t_i_x and Q a unimodular matrix such that
2211 * its first columns span the rows of the previously computed part
2212 * of the schedule. The non-triviality region enforces that at least
2213 * one of the remaining components of t_i_x is non-zero, i.e.,
2214 * that the new schedule row depends on at least one of the remaining
2215 * columns of Q.
2217 static __isl_give isl_vec *solve_lp(struct isl_sched_graph *graph)
2219 int i;
2220 isl_vec *sol;
2221 isl_basic_set *lp;
2223 for (i = 0; i < graph->n; ++i) {
2224 struct isl_sched_node *node = &graph->node[i];
2225 int skip = node->rank;
2226 graph->region[i].pos = node->start + 1 + 2*(node->nparam+skip);
2227 if (needs_row(graph, node))
2228 graph->region[i].len = 2 * (node->nvar - skip);
2229 else
2230 graph->region[i].len = 0;
2232 lp = isl_basic_set_copy(graph->lp);
2233 sol = isl_tab_basic_set_non_trivial_lexmin(lp, 2, graph->n,
2234 graph->region, &check_conflict, graph);
2235 return sol;
2238 /* Update the schedules of all nodes based on the given solution
2239 * of the LP problem.
2240 * The new row is added to the current band.
2241 * All possibly negative coefficients are encoded as a difference
2242 * of two non-negative variables, so we need to perform the subtraction
2243 * here. Moreover, if use_cmap is set, then the solution does
2244 * not refer to the actual coefficients c_i_x, but instead to variables
2245 * t_i_x such that c_i_x = Q t_i_x and Q is equal to node->cmap.
2246 * In this case, we then also need to perform this multiplication
2247 * to obtain the values of c_i_x.
2249 * If coincident is set, then the caller guarantees that the new
2250 * row satisfies the coincidence constraints.
2252 static int update_schedule(struct isl_sched_graph *graph,
2253 __isl_take isl_vec *sol, int use_cmap, int coincident)
2255 int i, j;
2256 isl_vec *csol = NULL;
2258 if (!sol)
2259 goto error;
2260 if (sol->size == 0)
2261 isl_die(sol->ctx, isl_error_internal,
2262 "no solution found", goto error);
2263 if (graph->n_total_row >= graph->max_row)
2264 isl_die(sol->ctx, isl_error_internal,
2265 "too many schedule rows", goto error);
2267 for (i = 0; i < graph->n; ++i) {
2268 struct isl_sched_node *node = &graph->node[i];
2269 int pos = node->start;
2270 int row = isl_mat_rows(node->sched);
2272 isl_vec_free(csol);
2273 csol = isl_vec_alloc(sol->ctx, node->nvar);
2274 if (!csol)
2275 goto error;
2277 isl_map_free(node->sched_map);
2278 node->sched_map = NULL;
2279 node->sched = isl_mat_add_rows(node->sched, 1);
2280 if (!node->sched)
2281 goto error;
2282 node->sched = isl_mat_set_element(node->sched, row, 0,
2283 sol->el[1 + pos]);
2284 for (j = 0; j < node->nparam + node->nvar; ++j)
2285 isl_int_sub(sol->el[1 + pos + 1 + 2 * j + 1],
2286 sol->el[1 + pos + 1 + 2 * j + 1],
2287 sol->el[1 + pos + 1 + 2 * j]);
2288 for (j = 0; j < node->nparam; ++j)
2289 node->sched = isl_mat_set_element(node->sched,
2290 row, 1 + j, sol->el[1+pos+1+2*j+1]);
2291 for (j = 0; j < node->nvar; ++j)
2292 isl_int_set(csol->el[j],
2293 sol->el[1+pos+1+2*(node->nparam+j)+1]);
2294 if (use_cmap)
2295 csol = isl_mat_vec_product(isl_mat_copy(node->cmap),
2296 csol);
2297 if (!csol)
2298 goto error;
2299 for (j = 0; j < node->nvar; ++j)
2300 node->sched = isl_mat_set_element(node->sched,
2301 row, 1 + node->nparam + j, csol->el[j]);
2302 node->band[graph->n_total_row] = graph->n_band;
2303 node->coincident[graph->n_total_row] = coincident;
2305 isl_vec_free(sol);
2306 isl_vec_free(csol);
2308 graph->n_row++;
2309 graph->n_total_row++;
2311 return 0;
2312 error:
2313 isl_vec_free(sol);
2314 isl_vec_free(csol);
2315 return -1;
2318 /* Convert row "row" of node->sched into an isl_aff living in "ls"
2319 * and return this isl_aff.
2321 static __isl_give isl_aff *extract_schedule_row(__isl_take isl_local_space *ls,
2322 struct isl_sched_node *node, int row)
2324 int j;
2325 isl_int v;
2326 isl_aff *aff;
2328 isl_int_init(v);
2330 aff = isl_aff_zero_on_domain(ls);
2331 isl_mat_get_element(node->sched, row, 0, &v);
2332 aff = isl_aff_set_constant(aff, v);
2333 for (j = 0; j < node->nparam; ++j) {
2334 isl_mat_get_element(node->sched, row, 1 + j, &v);
2335 aff = isl_aff_set_coefficient(aff, isl_dim_param, j, v);
2337 for (j = 0; j < node->nvar; ++j) {
2338 isl_mat_get_element(node->sched, row, 1 + node->nparam + j, &v);
2339 aff = isl_aff_set_coefficient(aff, isl_dim_in, j, v);
2342 isl_int_clear(v);
2344 return aff;
2347 /* Convert node->sched into a multi_aff and return this multi_aff.
2349 * The result is defined over the uncompressed node domain.
2351 static __isl_give isl_multi_aff *node_extract_schedule_multi_aff(
2352 struct isl_sched_node *node)
2354 int i;
2355 isl_space *space;
2356 isl_local_space *ls;
2357 isl_aff *aff;
2358 isl_multi_aff *ma;
2359 int nrow, ncol;
2361 nrow = isl_mat_rows(node->sched);
2362 ncol = isl_mat_cols(node->sched) - 1;
2363 if (node->compressed)
2364 space = isl_multi_aff_get_domain_space(node->decompress);
2365 else
2366 space = isl_space_copy(node->space);
2367 ls = isl_local_space_from_space(isl_space_copy(space));
2368 space = isl_space_from_domain(space);
2369 space = isl_space_add_dims(space, isl_dim_out, nrow);
2370 ma = isl_multi_aff_zero(space);
2372 for (i = 0; i < nrow; ++i) {
2373 aff = extract_schedule_row(isl_local_space_copy(ls), node, i);
2374 ma = isl_multi_aff_set_aff(ma, i, aff);
2377 isl_local_space_free(ls);
2379 if (node->compressed)
2380 ma = isl_multi_aff_pullback_multi_aff(ma,
2381 isl_multi_aff_copy(node->compress));
2383 return ma;
2386 /* Convert node->sched into a map and return this map.
2388 * The result is cached in node->sched_map, which needs to be released
2389 * whenever node->sched is updated.
2390 * It is defined over the uncompressed node domain.
2392 static __isl_give isl_map *node_extract_schedule(struct isl_sched_node *node)
2394 if (!node->sched_map) {
2395 isl_multi_aff *ma;
2397 ma = node_extract_schedule_multi_aff(node);
2398 node->sched_map = isl_map_from_multi_aff(ma);
2401 return isl_map_copy(node->sched_map);
2404 /* Construct a map that can be used to update a dependence relation
2405 * based on the current schedule.
2406 * That is, construct a map expressing that source and sink
2407 * are executed within the same iteration of the current schedule.
2408 * This map can then be intersected with the dependence relation.
2409 * This is not the most efficient way, but this shouldn't be a critical
2410 * operation.
2412 static __isl_give isl_map *specializer(struct isl_sched_node *src,
2413 struct isl_sched_node *dst)
2415 isl_map *src_sched, *dst_sched;
2417 src_sched = node_extract_schedule(src);
2418 dst_sched = node_extract_schedule(dst);
2419 return isl_map_apply_range(src_sched, isl_map_reverse(dst_sched));
2422 /* Intersect the domains of the nested relations in domain and range
2423 * of "umap" with "map".
2425 static __isl_give isl_union_map *intersect_domains(
2426 __isl_take isl_union_map *umap, __isl_keep isl_map *map)
2428 isl_union_set *uset;
2430 umap = isl_union_map_zip(umap);
2431 uset = isl_union_set_from_set(isl_map_wrap(isl_map_copy(map)));
2432 umap = isl_union_map_intersect_domain(umap, uset);
2433 umap = isl_union_map_zip(umap);
2434 return umap;
2437 /* Update the dependence relation of the given edge based
2438 * on the current schedule.
2439 * If the dependence is carried completely by the current schedule, then
2440 * it is removed from the edge_tables. It is kept in the list of edges
2441 * as otherwise all edge_tables would have to be recomputed.
2443 static int update_edge(struct isl_sched_graph *graph,
2444 struct isl_sched_edge *edge)
2446 isl_map *id;
2448 id = specializer(edge->src, edge->dst);
2449 edge->map = isl_map_intersect(edge->map, isl_map_copy(id));
2450 if (!edge->map)
2451 goto error;
2453 if (edge->tagged_condition) {
2454 edge->tagged_condition =
2455 intersect_domains(edge->tagged_condition, id);
2456 if (!edge->tagged_condition)
2457 goto error;
2459 if (edge->tagged_validity) {
2460 edge->tagged_validity =
2461 intersect_domains(edge->tagged_validity, id);
2462 if (!edge->tagged_validity)
2463 goto error;
2466 isl_map_free(id);
2467 if (isl_map_plain_is_empty(edge->map))
2468 graph_remove_edge(graph, edge);
2470 return 0;
2471 error:
2472 isl_map_free(id);
2473 return -1;
2476 /* Update the dependence relations of all edges based on the current schedule.
2478 static int update_edges(isl_ctx *ctx, struct isl_sched_graph *graph)
2480 int i;
2482 for (i = graph->n_edge - 1; i >= 0; --i) {
2483 if (update_edge(graph, &graph->edge[i]) < 0)
2484 return -1;
2487 return 0;
2490 static void next_band(struct isl_sched_graph *graph)
2492 graph->band_start = graph->n_total_row;
2493 graph->n_band++;
2496 /* Topologically sort statements mapped to the same schedule iteration
2497 * and add a row to the schedule corresponding to this order.
2499 static int sort_statements(isl_ctx *ctx, struct isl_sched_graph *graph)
2501 int i, j;
2503 if (graph->n <= 1)
2504 return 0;
2506 if (update_edges(ctx, graph) < 0)
2507 return -1;
2509 if (graph->n_edge == 0)
2510 return 0;
2512 if (detect_sccs(ctx, graph) < 0)
2513 return -1;
2515 if (graph->n_total_row >= graph->max_row)
2516 isl_die(ctx, isl_error_internal,
2517 "too many schedule rows", return -1);
2519 for (i = 0; i < graph->n; ++i) {
2520 struct isl_sched_node *node = &graph->node[i];
2521 int row = isl_mat_rows(node->sched);
2522 int cols = isl_mat_cols(node->sched);
2524 isl_map_free(node->sched_map);
2525 node->sched_map = NULL;
2526 node->sched = isl_mat_add_rows(node->sched, 1);
2527 if (!node->sched)
2528 return -1;
2529 node->sched = isl_mat_set_element_si(node->sched, row, 0,
2530 node->scc);
2531 for (j = 1; j < cols; ++j)
2532 node->sched = isl_mat_set_element_si(node->sched,
2533 row, j, 0);
2534 node->band[graph->n_total_row] = graph->n_band;
2537 graph->n_total_row++;
2538 next_band(graph);
2540 return 0;
2543 /* Construct an isl_schedule based on the computed schedule stored
2544 * in graph and with parameters specified by dim.
2546 static __isl_give isl_schedule *extract_schedule(struct isl_sched_graph *graph,
2547 __isl_take isl_space *dim)
2549 int i;
2550 isl_ctx *ctx;
2551 isl_schedule *sched = NULL;
2553 if (!dim)
2554 return NULL;
2556 ctx = isl_space_get_ctx(dim);
2557 sched = isl_calloc(ctx, struct isl_schedule,
2558 sizeof(struct isl_schedule) +
2559 (graph->n - 1) *
2560 sizeof(struct isl_schedule_domain_node));
2561 if (!sched)
2562 goto error;
2564 sched->ref = 1;
2565 sched->leaf.ctx = ctx;
2566 isl_ctx_ref(ctx);
2567 sched->n = graph->n;
2568 sched->n_band = graph->n_band;
2569 sched->n_total_row = graph->n_total_row;
2571 for (i = 0; i < sched->n; ++i) {
2572 int r, b;
2573 int *band_end, *band_id, *coincident;
2575 sched->node[i].sched =
2576 node_extract_schedule_multi_aff(&graph->node[i]);
2577 if (!sched->node[i].sched)
2578 goto error;
2580 sched->node[i].n_band = graph->n_band;
2581 if (graph->n_band == 0)
2582 continue;
2584 band_end = isl_alloc_array(ctx, int, graph->n_band);
2585 band_id = isl_alloc_array(ctx, int, graph->n_band);
2586 coincident = isl_alloc_array(ctx, int, graph->n_total_row);
2587 sched->node[i].band_end = band_end;
2588 sched->node[i].band_id = band_id;
2589 sched->node[i].coincident = coincident;
2590 if (!band_end || !band_id || !coincident)
2591 goto error;
2593 for (r = 0; r < graph->n_total_row; ++r)
2594 coincident[r] = graph->node[i].coincident[r];
2595 for (r = b = 0; r < graph->n_total_row; ++r) {
2596 if (graph->node[i].band[r] == b)
2597 continue;
2598 band_end[b++] = r;
2599 if (graph->node[i].band[r] == -1)
2600 break;
2602 if (r == graph->n_total_row)
2603 band_end[b++] = r;
2604 sched->node[i].n_band = b;
2605 for (--b; b >= 0; --b)
2606 band_id[b] = graph->node[i].band_id[b];
2609 sched->dim = dim;
2611 return sched;
2612 error:
2613 isl_space_free(dim);
2614 isl_schedule_free(sched);
2615 return NULL;
2618 /* Copy nodes that satisfy node_pred from the src dependence graph
2619 * to the dst dependence graph.
2621 static int copy_nodes(struct isl_sched_graph *dst, struct isl_sched_graph *src,
2622 int (*node_pred)(struct isl_sched_node *node, int data), int data)
2624 int i;
2626 dst->n = 0;
2627 for (i = 0; i < src->n; ++i) {
2628 int j;
2630 if (!node_pred(&src->node[i], data))
2631 continue;
2633 j = dst->n;
2634 dst->node[j].space = isl_space_copy(src->node[i].space);
2635 dst->node[j].compressed = src->node[i].compressed;
2636 dst->node[j].hull = isl_set_copy(src->node[i].hull);
2637 dst->node[j].compress =
2638 isl_multi_aff_copy(src->node[i].compress);
2639 dst->node[j].decompress =
2640 isl_multi_aff_copy(src->node[i].decompress);
2641 dst->node[j].nvar = src->node[i].nvar;
2642 dst->node[j].nparam = src->node[i].nparam;
2643 dst->node[j].sched = isl_mat_copy(src->node[i].sched);
2644 dst->node[j].sched_map = isl_map_copy(src->node[i].sched_map);
2645 dst->node[j].band = src->node[i].band;
2646 dst->node[j].band_id = src->node[i].band_id;
2647 dst->node[j].coincident = src->node[i].coincident;
2648 dst->n++;
2650 if (!dst->node[j].space || !dst->node[j].sched)
2651 return -1;
2652 if (dst->node[j].compressed &&
2653 (!dst->node[j].hull || !dst->node[j].compress ||
2654 !dst->node[j].decompress))
2655 return -1;
2658 return 0;
2661 /* Copy non-empty edges that satisfy edge_pred from the src dependence graph
2662 * to the dst dependence graph.
2663 * If the source or destination node of the edge is not in the destination
2664 * graph, then it must be a backward proximity edge and it should simply
2665 * be ignored.
2667 static int copy_edges(isl_ctx *ctx, struct isl_sched_graph *dst,
2668 struct isl_sched_graph *src,
2669 int (*edge_pred)(struct isl_sched_edge *edge, int data), int data)
2671 int i;
2672 enum isl_edge_type t;
2674 dst->n_edge = 0;
2675 for (i = 0; i < src->n_edge; ++i) {
2676 struct isl_sched_edge *edge = &src->edge[i];
2677 isl_map *map;
2678 isl_union_map *tagged_condition;
2679 isl_union_map *tagged_validity;
2680 struct isl_sched_node *dst_src, *dst_dst;
2682 if (!edge_pred(edge, data))
2683 continue;
2685 if (isl_map_plain_is_empty(edge->map))
2686 continue;
2688 dst_src = graph_find_node(ctx, dst, edge->src->space);
2689 dst_dst = graph_find_node(ctx, dst, edge->dst->space);
2690 if (!dst_src || !dst_dst) {
2691 if (edge->validity || edge->conditional_validity)
2692 isl_die(ctx, isl_error_internal,
2693 "backward (conditional) validity edge",
2694 return -1);
2695 continue;
2698 map = isl_map_copy(edge->map);
2699 tagged_condition = isl_union_map_copy(edge->tagged_condition);
2700 tagged_validity = isl_union_map_copy(edge->tagged_validity);
2702 dst->edge[dst->n_edge].src = dst_src;
2703 dst->edge[dst->n_edge].dst = dst_dst;
2704 dst->edge[dst->n_edge].map = map;
2705 dst->edge[dst->n_edge].tagged_condition = tagged_condition;
2706 dst->edge[dst->n_edge].tagged_validity = tagged_validity;
2707 dst->edge[dst->n_edge].validity = edge->validity;
2708 dst->edge[dst->n_edge].proximity = edge->proximity;
2709 dst->edge[dst->n_edge].coincidence = edge->coincidence;
2710 dst->edge[dst->n_edge].condition = edge->condition;
2711 dst->edge[dst->n_edge].conditional_validity =
2712 edge->conditional_validity;
2713 dst->n_edge++;
2715 if (edge->tagged_condition && !tagged_condition)
2716 return -1;
2717 if (edge->tagged_validity && !tagged_validity)
2718 return -1;
2720 for (t = isl_edge_first; t <= isl_edge_last; ++t) {
2721 if (edge !=
2722 graph_find_edge(src, t, edge->src, edge->dst))
2723 continue;
2724 if (graph_edge_table_add(ctx, dst, t,
2725 &dst->edge[dst->n_edge - 1]) < 0)
2726 return -1;
2730 return 0;
2733 /* Given a "src" dependence graph that contains the nodes from "dst"
2734 * that satisfy node_pred, copy the schedule computed in "src"
2735 * for those nodes back to "dst".
2737 static int copy_schedule(struct isl_sched_graph *dst,
2738 struct isl_sched_graph *src,
2739 int (*node_pred)(struct isl_sched_node *node, int data), int data)
2741 int i;
2743 src->n = 0;
2744 for (i = 0; i < dst->n; ++i) {
2745 if (!node_pred(&dst->node[i], data))
2746 continue;
2747 isl_mat_free(dst->node[i].sched);
2748 isl_map_free(dst->node[i].sched_map);
2749 dst->node[i].sched = isl_mat_copy(src->node[src->n].sched);
2750 dst->node[i].sched_map =
2751 isl_map_copy(src->node[src->n].sched_map);
2752 src->n++;
2755 dst->max_row = src->max_row;
2756 dst->n_total_row = src->n_total_row;
2757 dst->n_band = src->n_band;
2759 return 0;
2762 /* Compute the maximal number of variables over all nodes.
2763 * This is the maximal number of linearly independent schedule
2764 * rows that we need to compute.
2765 * Just in case we end up in a part of the dependence graph
2766 * with only lower-dimensional domains, we make sure we will
2767 * compute the required amount of extra linearly independent rows.
2769 static int compute_maxvar(struct isl_sched_graph *graph)
2771 int i;
2773 graph->maxvar = 0;
2774 for (i = 0; i < graph->n; ++i) {
2775 struct isl_sched_node *node = &graph->node[i];
2776 int nvar;
2778 if (node_update_cmap(node) < 0)
2779 return -1;
2780 nvar = node->nvar + graph->n_row - node->rank;
2781 if (nvar > graph->maxvar)
2782 graph->maxvar = nvar;
2785 return 0;
2788 static int compute_schedule(isl_ctx *ctx, struct isl_sched_graph *graph);
2789 static int compute_schedule_wcc(isl_ctx *ctx, struct isl_sched_graph *graph);
2791 /* Compute a schedule for a subgraph of "graph". In particular, for
2792 * the graph composed of nodes that satisfy node_pred and edges that
2793 * that satisfy edge_pred. The caller should precompute the number
2794 * of nodes and edges that satisfy these predicates and pass them along
2795 * as "n" and "n_edge".
2796 * If the subgraph is known to consist of a single component, then wcc should
2797 * be set and then we call compute_schedule_wcc on the constructed subgraph.
2798 * Otherwise, we call compute_schedule, which will check whether the subgraph
2799 * is connected.
2801 static int compute_sub_schedule(isl_ctx *ctx,
2802 struct isl_sched_graph *graph, int n, int n_edge,
2803 int (*node_pred)(struct isl_sched_node *node, int data),
2804 int (*edge_pred)(struct isl_sched_edge *edge, int data),
2805 int data, int wcc)
2807 struct isl_sched_graph split = { 0 };
2808 int t;
2810 if (graph_alloc(ctx, &split, n, n_edge) < 0)
2811 goto error;
2812 if (copy_nodes(&split, graph, node_pred, data) < 0)
2813 goto error;
2814 if (graph_init_table(ctx, &split) < 0)
2815 goto error;
2816 for (t = 0; t <= isl_edge_last; ++t)
2817 split.max_edge[t] = graph->max_edge[t];
2818 if (graph_init_edge_tables(ctx, &split) < 0)
2819 goto error;
2820 if (copy_edges(ctx, &split, graph, edge_pred, data) < 0)
2821 goto error;
2822 split.n_row = graph->n_row;
2823 split.max_row = graph->max_row;
2824 split.n_total_row = graph->n_total_row;
2825 split.n_band = graph->n_band;
2826 split.band_start = graph->band_start;
2828 if (wcc && compute_schedule_wcc(ctx, &split) < 0)
2829 goto error;
2830 if (!wcc && compute_schedule(ctx, &split) < 0)
2831 goto error;
2833 copy_schedule(graph, &split, node_pred, data);
2835 graph_free(ctx, &split);
2836 return 0;
2837 error:
2838 graph_free(ctx, &split);
2839 return -1;
2842 static int edge_scc_exactly(struct isl_sched_edge *edge, int scc)
2844 return edge->src->scc == scc && edge->dst->scc == scc;
2847 static int edge_dst_scc_at_most(struct isl_sched_edge *edge, int scc)
2849 return edge->dst->scc <= scc;
2852 static int edge_src_scc_at_least(struct isl_sched_edge *edge, int scc)
2854 return edge->src->scc >= scc;
2857 /* Pad the schedules of all nodes with zero rows such that in the end
2858 * they all have graph->n_total_row rows.
2859 * The extra rows don't belong to any band, so they get assigned band number -1.
2861 static int pad_schedule(struct isl_sched_graph *graph)
2863 int i, j;
2865 for (i = 0; i < graph->n; ++i) {
2866 struct isl_sched_node *node = &graph->node[i];
2867 int row = isl_mat_rows(node->sched);
2868 if (graph->n_total_row > row) {
2869 isl_map_free(node->sched_map);
2870 node->sched_map = NULL;
2872 node->sched = isl_mat_add_zero_rows(node->sched,
2873 graph->n_total_row - row);
2874 if (!node->sched)
2875 return -1;
2876 for (j = row; j < graph->n_total_row; ++j)
2877 node->band[j] = -1;
2880 return 0;
2883 /* Reset the current band by dropping all its schedule rows.
2885 static int reset_band(struct isl_sched_graph *graph)
2887 int i;
2888 int drop;
2890 drop = graph->n_total_row - graph->band_start;
2891 graph->n_total_row -= drop;
2892 graph->n_row -= drop;
2894 for (i = 0; i < graph->n; ++i) {
2895 struct isl_sched_node *node = &graph->node[i];
2897 isl_map_free(node->sched_map);
2898 node->sched_map = NULL;
2900 node->sched = isl_mat_drop_rows(node->sched,
2901 graph->band_start, drop);
2903 if (!node->sched)
2904 return -1;
2907 return 0;
2910 /* Split the current graph into two parts and compute a schedule for each
2911 * part individually. In particular, one part consists of all SCCs up
2912 * to and including graph->src_scc, while the other part contains the other
2913 * SCCS.
2915 * The split is enforced in the schedule by constant rows with two different
2916 * values (0 and 1). These constant rows replace the previously computed rows
2917 * in the current band.
2918 * It would be possible to reuse them as the first rows in the next
2919 * band, but recomputing them may result in better rows as we are looking
2920 * at a smaller part of the dependence graph.
2922 * Since we do not enforce coincidence, we conservatively mark the
2923 * splitting row as not coincident.
2925 * The band_id of the second group is set to n, where n is the number
2926 * of nodes in the first group. This ensures that the band_ids over
2927 * the two groups remain disjoint, even if either or both of the two
2928 * groups contain independent components.
2930 static int compute_split_schedule(isl_ctx *ctx, struct isl_sched_graph *graph)
2932 int i, j, n, e1, e2;
2933 int n_total_row, orig_total_row;
2934 int n_band, orig_band;
2936 if (graph->n_total_row >= graph->max_row)
2937 isl_die(ctx, isl_error_internal,
2938 "too many schedule rows", return -1);
2940 if (reset_band(graph) < 0)
2941 return -1;
2943 n = 0;
2944 for (i = 0; i < graph->n; ++i) {
2945 struct isl_sched_node *node = &graph->node[i];
2946 int row = isl_mat_rows(node->sched);
2947 int cols = isl_mat_cols(node->sched);
2948 int before = node->scc <= graph->src_scc;
2950 if (before)
2951 n++;
2953 isl_map_free(node->sched_map);
2954 node->sched_map = NULL;
2955 node->sched = isl_mat_add_rows(node->sched, 1);
2956 if (!node->sched)
2957 return -1;
2958 node->sched = isl_mat_set_element_si(node->sched, row, 0,
2959 !before);
2960 for (j = 1; j < cols; ++j)
2961 node->sched = isl_mat_set_element_si(node->sched,
2962 row, j, 0);
2963 node->band[graph->n_total_row] = graph->n_band;
2964 node->coincident[graph->n_total_row] = 0;
2967 e1 = e2 = 0;
2968 for (i = 0; i < graph->n_edge; ++i) {
2969 if (graph->edge[i].dst->scc <= graph->src_scc)
2970 e1++;
2971 if (graph->edge[i].src->scc > graph->src_scc)
2972 e2++;
2975 graph->n_total_row++;
2976 next_band(graph);
2978 for (i = 0; i < graph->n; ++i) {
2979 struct isl_sched_node *node = &graph->node[i];
2980 if (node->scc > graph->src_scc)
2981 node->band_id[graph->n_band] = n;
2984 orig_total_row = graph->n_total_row;
2985 orig_band = graph->n_band;
2986 if (compute_sub_schedule(ctx, graph, n, e1,
2987 &node_scc_at_most, &edge_dst_scc_at_most,
2988 graph->src_scc, 0) < 0)
2989 return -1;
2990 n_total_row = graph->n_total_row;
2991 graph->n_total_row = orig_total_row;
2992 n_band = graph->n_band;
2993 graph->n_band = orig_band;
2994 if (compute_sub_schedule(ctx, graph, graph->n - n, e2,
2995 &node_scc_at_least, &edge_src_scc_at_least,
2996 graph->src_scc + 1, 0) < 0)
2997 return -1;
2998 if (n_total_row > graph->n_total_row)
2999 graph->n_total_row = n_total_row;
3000 if (n_band > graph->n_band)
3001 graph->n_band = n_band;
3003 return pad_schedule(graph);
3006 /* Compute the next band of the schedule after updating the dependence
3007 * relations based on the the current schedule.
3009 static int compute_next_band(isl_ctx *ctx, struct isl_sched_graph *graph)
3011 if (update_edges(ctx, graph) < 0)
3012 return -1;
3013 next_band(graph);
3015 return compute_schedule(ctx, graph);
3018 /* Add constraints to graph->lp that force the dependence "map" (which
3019 * is part of the dependence relation of "edge")
3020 * to be respected and attempt to carry it, where the edge is one from
3021 * a node j to itself. "pos" is the sequence number of the given map.
3022 * That is, add constraints that enforce
3024 * (c_j_0 + c_j_n n + c_j_x y) - (c_j_0 + c_j_n n + c_j_x x)
3025 * = c_j_x (y - x) >= e_i
3027 * for each (x,y) in R.
3028 * We obtain general constraints on coefficients (c_0, c_n, c_x)
3029 * of valid constraints for (y - x) and then plug in (-e_i, 0, c_j_x),
3030 * with each coefficient in c_j_x represented as a pair of non-negative
3031 * coefficients.
3033 static int add_intra_constraints(struct isl_sched_graph *graph,
3034 struct isl_sched_edge *edge, __isl_take isl_map *map, int pos)
3036 unsigned total;
3037 isl_ctx *ctx = isl_map_get_ctx(map);
3038 isl_space *dim;
3039 isl_dim_map *dim_map;
3040 isl_basic_set *coef;
3041 struct isl_sched_node *node = edge->src;
3043 coef = intra_coefficients(graph, node, map);
3044 if (!coef)
3045 return -1;
3047 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
3049 total = isl_basic_set_total_dim(graph->lp);
3050 dim_map = isl_dim_map_alloc(ctx, total);
3051 isl_dim_map_range(dim_map, 3 + pos, 0, 0, 0, 1, -1);
3052 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 1, 2,
3053 isl_space_dim(dim, isl_dim_set), 1,
3054 node->nvar, -1);
3055 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 2, 2,
3056 isl_space_dim(dim, isl_dim_set), 1,
3057 node->nvar, 1);
3058 graph->lp = isl_basic_set_extend_constraints(graph->lp,
3059 coef->n_eq, coef->n_ineq);
3060 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
3061 coef, dim_map);
3062 isl_space_free(dim);
3064 return 0;
3067 /* Add constraints to graph->lp that force the dependence "map" (which
3068 * is part of the dependence relation of "edge")
3069 * to be respected and attempt to carry it, where the edge is one from
3070 * node j to node k. "pos" is the sequence number of the given map.
3071 * That is, add constraints that enforce
3073 * (c_k_0 + c_k_n n + c_k_x y) - (c_j_0 + c_j_n n + c_j_x x) >= e_i
3075 * for each (x,y) in R.
3076 * We obtain general constraints on coefficients (c_0, c_n, c_x)
3077 * of valid constraints for R and then plug in
3078 * (-e_i + c_k_0 - c_j_0, c_k_n - c_j_n, c_k_x - c_j_x)
3079 * with each coefficient (except e_i, c_k_0 and c_j_0)
3080 * represented as a pair of non-negative coefficients.
3082 static int add_inter_constraints(struct isl_sched_graph *graph,
3083 struct isl_sched_edge *edge, __isl_take isl_map *map, int pos)
3085 unsigned total;
3086 isl_ctx *ctx = isl_map_get_ctx(map);
3087 isl_space *dim;
3088 isl_dim_map *dim_map;
3089 isl_basic_set *coef;
3090 struct isl_sched_node *src = edge->src;
3091 struct isl_sched_node *dst = edge->dst;
3093 coef = inter_coefficients(graph, edge, map);
3094 if (!coef)
3095 return -1;
3097 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
3099 total = isl_basic_set_total_dim(graph->lp);
3100 dim_map = isl_dim_map_alloc(ctx, total);
3102 isl_dim_map_range(dim_map, 3 + pos, 0, 0, 0, 1, -1);
3104 isl_dim_map_range(dim_map, dst->start, 0, 0, 0, 1, 1);
3105 isl_dim_map_range(dim_map, dst->start + 1, 2, 1, 1, dst->nparam, -1);
3106 isl_dim_map_range(dim_map, dst->start + 2, 2, 1, 1, dst->nparam, 1);
3107 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 1, 2,
3108 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
3109 dst->nvar, -1);
3110 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 2, 2,
3111 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
3112 dst->nvar, 1);
3114 isl_dim_map_range(dim_map, src->start, 0, 0, 0, 1, -1);
3115 isl_dim_map_range(dim_map, src->start + 1, 2, 1, 1, src->nparam, 1);
3116 isl_dim_map_range(dim_map, src->start + 2, 2, 1, 1, src->nparam, -1);
3117 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 1, 2,
3118 isl_space_dim(dim, isl_dim_set), 1,
3119 src->nvar, 1);
3120 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 2, 2,
3121 isl_space_dim(dim, isl_dim_set), 1,
3122 src->nvar, -1);
3124 graph->lp = isl_basic_set_extend_constraints(graph->lp,
3125 coef->n_eq, coef->n_ineq);
3126 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
3127 coef, dim_map);
3128 isl_space_free(dim);
3130 return 0;
3133 /* Add constraints to graph->lp that force all (conditional) validity
3134 * dependences to be respected and attempt to carry them.
3136 static int add_all_constraints(struct isl_sched_graph *graph)
3138 int i, j;
3139 int pos;
3141 pos = 0;
3142 for (i = 0; i < graph->n_edge; ++i) {
3143 struct isl_sched_edge *edge= &graph->edge[i];
3145 if (!edge->validity && !edge->conditional_validity)
3146 continue;
3148 for (j = 0; j < edge->map->n; ++j) {
3149 isl_basic_map *bmap;
3150 isl_map *map;
3152 bmap = isl_basic_map_copy(edge->map->p[j]);
3153 map = isl_map_from_basic_map(bmap);
3155 if (edge->src == edge->dst &&
3156 add_intra_constraints(graph, edge, map, pos) < 0)
3157 return -1;
3158 if (edge->src != edge->dst &&
3159 add_inter_constraints(graph, edge, map, pos) < 0)
3160 return -1;
3161 ++pos;
3165 return 0;
3168 /* Count the number of equality and inequality constraints
3169 * that will be added to the carry_lp problem.
3170 * We count each edge exactly once.
3172 static int count_all_constraints(struct isl_sched_graph *graph,
3173 int *n_eq, int *n_ineq)
3175 int i, j;
3177 *n_eq = *n_ineq = 0;
3178 for (i = 0; i < graph->n_edge; ++i) {
3179 struct isl_sched_edge *edge= &graph->edge[i];
3180 for (j = 0; j < edge->map->n; ++j) {
3181 isl_basic_map *bmap;
3182 isl_map *map;
3184 bmap = isl_basic_map_copy(edge->map->p[j]);
3185 map = isl_map_from_basic_map(bmap);
3187 if (count_map_constraints(graph, edge, map,
3188 n_eq, n_ineq, 1, 0) < 0)
3189 return -1;
3193 return 0;
3196 /* Construct an LP problem for finding schedule coefficients
3197 * such that the schedule carries as many dependences as possible.
3198 * In particular, for each dependence i, we bound the dependence distance
3199 * from below by e_i, with 0 <= e_i <= 1 and then maximize the sum
3200 * of all e_i's. Dependence with e_i = 0 in the solution are simply
3201 * respected, while those with e_i > 0 (in practice e_i = 1) are carried.
3202 * Note that if the dependence relation is a union of basic maps,
3203 * then we have to consider each basic map individually as it may only
3204 * be possible to carry the dependences expressed by some of those
3205 * basic maps and not all off them.
3206 * Below, we consider each of those basic maps as a separate "edge".
3208 * All variables of the LP are non-negative. The actual coefficients
3209 * may be negative, so each coefficient is represented as the difference
3210 * of two non-negative variables. The negative part always appears
3211 * immediately before the positive part.
3212 * Other than that, the variables have the following order
3214 * - sum of (1 - e_i) over all edges
3215 * - sum of positive and negative parts of all c_n coefficients
3216 * (unconstrained when computing non-parametric schedules)
3217 * - sum of positive and negative parts of all c_x coefficients
3218 * - for each edge
3219 * - e_i
3220 * - for each node
3221 * - c_i_0
3222 * - positive and negative parts of c_i_n (if parametric)
3223 * - positive and negative parts of c_i_x
3225 * The constraints are those from the (validity) edges plus three equalities
3226 * to express the sums and n_edge inequalities to express e_i <= 1.
3228 static int setup_carry_lp(isl_ctx *ctx, struct isl_sched_graph *graph)
3230 int i, j;
3231 int k;
3232 isl_space *dim;
3233 unsigned total;
3234 int n_eq, n_ineq;
3235 int n_edge;
3237 n_edge = 0;
3238 for (i = 0; i < graph->n_edge; ++i)
3239 n_edge += graph->edge[i].map->n;
3241 total = 3 + n_edge;
3242 for (i = 0; i < graph->n; ++i) {
3243 struct isl_sched_node *node = &graph->node[graph->sorted[i]];
3244 node->start = total;
3245 total += 1 + 2 * (node->nparam + node->nvar);
3248 if (count_all_constraints(graph, &n_eq, &n_ineq) < 0)
3249 return -1;
3250 if (count_bound_coefficient_constraints(ctx, graph, &n_eq, &n_ineq) < 0)
3251 return -1;
3253 dim = isl_space_set_alloc(ctx, 0, total);
3254 isl_basic_set_free(graph->lp);
3255 n_eq += 3;
3256 n_ineq += n_edge;
3257 graph->lp = isl_basic_set_alloc_space(dim, 0, n_eq, n_ineq);
3258 graph->lp = isl_basic_set_set_rational(graph->lp);
3260 k = isl_basic_set_alloc_equality(graph->lp);
3261 if (k < 0)
3262 return -1;
3263 isl_seq_clr(graph->lp->eq[k], 1 + total);
3264 isl_int_set_si(graph->lp->eq[k][0], -n_edge);
3265 isl_int_set_si(graph->lp->eq[k][1], 1);
3266 for (i = 0; i < n_edge; ++i)
3267 isl_int_set_si(graph->lp->eq[k][4 + i], 1);
3269 k = isl_basic_set_alloc_equality(graph->lp);
3270 if (k < 0)
3271 return -1;
3272 isl_seq_clr(graph->lp->eq[k], 1 + total);
3273 isl_int_set_si(graph->lp->eq[k][2], -1);
3274 for (i = 0; i < graph->n; ++i) {
3275 int pos = 1 + graph->node[i].start + 1;
3277 for (j = 0; j < 2 * graph->node[i].nparam; ++j)
3278 isl_int_set_si(graph->lp->eq[k][pos + j], 1);
3281 k = isl_basic_set_alloc_equality(graph->lp);
3282 if (k < 0)
3283 return -1;
3284 isl_seq_clr(graph->lp->eq[k], 1 + total);
3285 isl_int_set_si(graph->lp->eq[k][3], -1);
3286 for (i = 0; i < graph->n; ++i) {
3287 struct isl_sched_node *node = &graph->node[i];
3288 int pos = 1 + node->start + 1 + 2 * node->nparam;
3290 for (j = 0; j < 2 * node->nvar; ++j)
3291 isl_int_set_si(graph->lp->eq[k][pos + j], 1);
3294 for (i = 0; i < n_edge; ++i) {
3295 k = isl_basic_set_alloc_inequality(graph->lp);
3296 if (k < 0)
3297 return -1;
3298 isl_seq_clr(graph->lp->ineq[k], 1 + total);
3299 isl_int_set_si(graph->lp->ineq[k][4 + i], -1);
3300 isl_int_set_si(graph->lp->ineq[k][0], 1);
3303 if (add_bound_coefficient_constraints(ctx, graph) < 0)
3304 return -1;
3305 if (add_all_constraints(graph) < 0)
3306 return -1;
3308 return 0;
3311 static int compute_component_schedule(isl_ctx *ctx,
3312 struct isl_sched_graph *graph, int wcc);
3314 /* Comparison function for sorting the statements based on
3315 * the corresponding value in "r".
3317 static int smaller_value(const void *a, const void *b, void *data)
3319 isl_vec *r = data;
3320 const int *i1 = a;
3321 const int *i2 = b;
3323 return isl_int_cmp(r->el[*i1], r->el[*i2]);
3326 /* If the schedule_split_scaled option is set and if the linear
3327 * parts of the scheduling rows for all nodes in the graphs have
3328 * a non-trivial common divisor, then split off the remainder of the
3329 * constant term modulo this common divisor from the linear part.
3330 * Otherwise, continue with the construction of the schedule.
3332 * If a non-trivial common divisor is found, then
3333 * the linear part is reduced and the remainder is enforced
3334 * by a piecewise constant schedule based on the order of these remainders.
3335 * In particular, we assign an scc index based on the remainder and
3336 * then rely on compute_component_schedule to insert the schedule row and
3337 * to continue the schedule construction on each part.
3339 static int split_scaled(isl_ctx *ctx, struct isl_sched_graph *graph)
3341 int i;
3342 int row;
3343 int scc;
3344 isl_int gcd, gcd_i;
3345 isl_vec *r;
3346 int *order;
3348 if (!ctx->opt->schedule_split_scaled)
3349 return compute_next_band(ctx, graph);
3350 if (graph->n <= 1)
3351 return compute_next_band(ctx, graph);
3353 isl_int_init(gcd);
3354 isl_int_init(gcd_i);
3356 isl_int_set_si(gcd, 0);
3358 row = isl_mat_rows(graph->node[0].sched) - 1;
3360 for (i = 0; i < graph->n; ++i) {
3361 struct isl_sched_node *node = &graph->node[i];
3362 int cols = isl_mat_cols(node->sched);
3364 isl_seq_gcd(node->sched->row[row] + 1, cols - 1, &gcd_i);
3365 isl_int_gcd(gcd, gcd, gcd_i);
3368 isl_int_clear(gcd_i);
3370 if (isl_int_cmp_si(gcd, 1) <= 0) {
3371 isl_int_clear(gcd);
3372 return compute_next_band(ctx, graph);
3375 r = isl_vec_alloc(ctx, graph->n);
3376 order = isl_calloc_array(ctx, int, graph->n);
3377 if (!r || !order)
3378 goto error;
3380 for (i = 0; i < graph->n; ++i) {
3381 struct isl_sched_node *node = &graph->node[i];
3383 order[i] = i;
3384 isl_int_fdiv_r(r->el[i], node->sched->row[row][0], gcd);
3385 isl_int_fdiv_q(node->sched->row[row][0],
3386 node->sched->row[row][0], gcd);
3387 isl_int_mul(node->sched->row[row][0],
3388 node->sched->row[row][0], gcd);
3389 node->sched = isl_mat_scale_down_row(node->sched, row, gcd);
3390 if (!node->sched)
3391 goto error;
3394 if (isl_sort(order, graph->n, sizeof(order[0]), &smaller_value, r) < 0)
3395 goto error;
3397 scc = 0;
3398 for (i = 0; i < graph->n; ++i) {
3399 if (i > 0 && isl_int_ne(r->el[order[i - 1]], r->el[order[i]]))
3400 ++scc;
3401 graph->node[order[i]].scc = scc;
3403 graph->scc = ++scc;
3404 graph->weak = 0;
3406 isl_int_clear(gcd);
3407 isl_vec_free(r);
3408 free(order);
3410 if (update_edges(ctx, graph) < 0)
3411 return -1;
3412 next_band(graph);
3414 return compute_component_schedule(ctx, graph, 0);
3415 error:
3416 isl_int_clear(gcd);
3417 return -1;
3420 /* Is the schedule row "sol" trivial on node "node"?
3421 * That is, is the solution zero on the dimensions orthogonal to
3422 * the previously found solutions?
3423 * Return 1 if the solution is trivial, 0 if it is not and -1 on error.
3425 * Each coefficient is represented as the difference between
3426 * two non-negative values in "sol". "sol" has been computed
3427 * in terms of the original iterators (i.e., without use of cmap).
3428 * We construct the schedule row s and write it as a linear
3429 * combination of (linear combinations of) previously computed schedule rows.
3430 * s = Q c or c = U s.
3431 * If the final entries of c are all zero, then the solution is trivial.
3433 static int is_trivial(struct isl_sched_node *node, __isl_keep isl_vec *sol)
3435 int i;
3436 int pos;
3437 int trivial;
3438 isl_ctx *ctx;
3439 isl_vec *node_sol;
3441 if (!sol)
3442 return -1;
3443 if (node->nvar == node->rank)
3444 return 0;
3446 ctx = isl_vec_get_ctx(sol);
3447 node_sol = isl_vec_alloc(ctx, node->nvar);
3448 if (!node_sol)
3449 return -1;
3451 pos = 1 + node->start + 1 + 2 * node->nparam;
3453 for (i = 0; i < node->nvar; ++i)
3454 isl_int_sub(node_sol->el[i],
3455 sol->el[pos + 2 * i + 1], sol->el[pos + 2 * i]);
3457 node_sol = isl_mat_vec_product(isl_mat_copy(node->cinv), node_sol);
3459 if (!node_sol)
3460 return -1;
3462 trivial = isl_seq_first_non_zero(node_sol->el + node->rank,
3463 node->nvar - node->rank) == -1;
3465 isl_vec_free(node_sol);
3467 return trivial;
3470 /* Is the schedule row "sol" trivial on any node where it should
3471 * not be trivial?
3472 * "sol" has been computed in terms of the original iterators
3473 * (i.e., without use of cmap).
3474 * Return 1 if any solution is trivial, 0 if they are not and -1 on error.
3476 static int is_any_trivial(struct isl_sched_graph *graph,
3477 __isl_keep isl_vec *sol)
3479 int i;
3481 for (i = 0; i < graph->n; ++i) {
3482 struct isl_sched_node *node = &graph->node[i];
3483 int trivial;
3485 if (!needs_row(graph, node))
3486 continue;
3487 trivial = is_trivial(node, sol);
3488 if (trivial < 0 || trivial)
3489 return trivial;
3492 return 0;
3495 /* Construct a schedule row for each node such that as many dependences
3496 * as possible are carried and then continue with the next band.
3498 * If the computed schedule row turns out to be trivial on one or
3499 * more nodes where it should not be trivial, then we throw it away
3500 * and try again on each component separately.
3502 * If there is only one component, then we accept the schedule row anyway,
3503 * but we do not consider it as a complete row and therefore do not
3504 * increment graph->n_row. Note that the ranks of the nodes that
3505 * do get a non-trivial schedule part will get updated regardless and
3506 * graph->maxvar is computed based on these ranks. The test for
3507 * whether more schedule rows are required in compute_schedule_wcc
3508 * is therefore not affected.
3510 * Continue with the construction of the schedule in split_scaled
3511 * after optionally checking for non-trivial common divisors.
3513 static int carry_dependences(isl_ctx *ctx, struct isl_sched_graph *graph)
3515 int i;
3516 int n_edge;
3517 int trivial;
3518 isl_vec *sol;
3519 isl_basic_set *lp;
3521 n_edge = 0;
3522 for (i = 0; i < graph->n_edge; ++i)
3523 n_edge += graph->edge[i].map->n;
3525 if (setup_carry_lp(ctx, graph) < 0)
3526 return -1;
3528 lp = isl_basic_set_copy(graph->lp);
3529 sol = isl_tab_basic_set_non_neg_lexmin(lp);
3530 if (!sol)
3531 return -1;
3533 if (sol->size == 0) {
3534 isl_vec_free(sol);
3535 isl_die(ctx, isl_error_internal,
3536 "error in schedule construction", return -1);
3539 isl_int_divexact(sol->el[1], sol->el[1], sol->el[0]);
3540 if (isl_int_cmp_si(sol->el[1], n_edge) >= 0) {
3541 isl_vec_free(sol);
3542 isl_die(ctx, isl_error_unknown,
3543 "unable to carry dependences", return -1);
3546 trivial = is_any_trivial(graph, sol);
3547 if (trivial < 0) {
3548 sol = isl_vec_free(sol);
3549 } else if (trivial && graph->scc > 1) {
3550 isl_vec_free(sol);
3551 return compute_component_schedule(ctx, graph, 1);
3554 if (update_schedule(graph, sol, 0, 0) < 0)
3555 return -1;
3556 if (trivial)
3557 graph->n_row--;
3559 return split_scaled(ctx, graph);
3562 /* Are there any (non-empty) (conditional) validity edges in the graph?
3564 static int has_validity_edges(struct isl_sched_graph *graph)
3566 int i;
3568 for (i = 0; i < graph->n_edge; ++i) {
3569 int empty;
3571 empty = isl_map_plain_is_empty(graph->edge[i].map);
3572 if (empty < 0)
3573 return -1;
3574 if (empty)
3575 continue;
3576 if (graph->edge[i].validity ||
3577 graph->edge[i].conditional_validity)
3578 return 1;
3581 return 0;
3584 /* Should we apply a Feautrier step?
3585 * That is, did the user request the Feautrier algorithm and are
3586 * there any validity dependences (left)?
3588 static int need_feautrier_step(isl_ctx *ctx, struct isl_sched_graph *graph)
3590 if (ctx->opt->schedule_algorithm != ISL_SCHEDULE_ALGORITHM_FEAUTRIER)
3591 return 0;
3593 return has_validity_edges(graph);
3596 /* Compute a schedule for a connected dependence graph using Feautrier's
3597 * multi-dimensional scheduling algorithm.
3598 * The original algorithm is described in [1].
3599 * The main idea is to minimize the number of scheduling dimensions, by
3600 * trying to satisfy as many dependences as possible per scheduling dimension.
3602 * [1] P. Feautrier, Some Efficient Solutions to the Affine Scheduling
3603 * Problem, Part II: Multi-Dimensional Time.
3604 * In Intl. Journal of Parallel Programming, 1992.
3606 static int compute_schedule_wcc_feautrier(isl_ctx *ctx,
3607 struct isl_sched_graph *graph)
3609 return carry_dependences(ctx, graph);
3612 /* Turn off the "local" bit on all (condition) edges.
3614 static void clear_local_edges(struct isl_sched_graph *graph)
3616 int i;
3618 for (i = 0; i < graph->n_edge; ++i)
3619 if (graph->edge[i].condition)
3620 graph->edge[i].local = 0;
3623 /* Does "graph" have both condition and conditional validity edges?
3625 static int need_condition_check(struct isl_sched_graph *graph)
3627 int i;
3628 int any_condition = 0;
3629 int any_conditional_validity = 0;
3631 for (i = 0; i < graph->n_edge; ++i) {
3632 if (graph->edge[i].condition)
3633 any_condition = 1;
3634 if (graph->edge[i].conditional_validity)
3635 any_conditional_validity = 1;
3638 return any_condition && any_conditional_validity;
3641 /* Does "graph" contain any coincidence edge?
3643 static int has_any_coincidence(struct isl_sched_graph *graph)
3645 int i;
3647 for (i = 0; i < graph->n_edge; ++i)
3648 if (graph->edge[i].coincidence)
3649 return 1;
3651 return 0;
3654 /* Extract the final schedule row as a map with the iteration domain
3655 * of "node" as domain.
3657 static __isl_give isl_map *final_row(struct isl_sched_node *node)
3659 isl_local_space *ls;
3660 isl_aff *aff;
3661 int row;
3663 row = isl_mat_rows(node->sched) - 1;
3664 ls = isl_local_space_from_space(isl_space_copy(node->space));
3665 aff = extract_schedule_row(ls, node, row);
3666 return isl_map_from_aff(aff);
3669 /* Is the conditional validity dependence in the edge with index "edge_index"
3670 * violated by the latest (i.e., final) row of the schedule?
3671 * That is, is i scheduled after j
3672 * for any conditional validity dependence i -> j?
3674 static int is_violated(struct isl_sched_graph *graph, int edge_index)
3676 isl_map *src_sched, *dst_sched, *map;
3677 struct isl_sched_edge *edge = &graph->edge[edge_index];
3678 int empty;
3680 src_sched = final_row(edge->src);
3681 dst_sched = final_row(edge->dst);
3682 map = isl_map_copy(edge->map);
3683 map = isl_map_apply_domain(map, src_sched);
3684 map = isl_map_apply_range(map, dst_sched);
3685 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_out, 0);
3686 empty = isl_map_is_empty(map);
3687 isl_map_free(map);
3689 if (empty < 0)
3690 return -1;
3692 return !empty;
3695 /* Does the domain of "umap" intersect "uset"?
3697 static int domain_intersects(__isl_keep isl_union_map *umap,
3698 __isl_keep isl_union_set *uset)
3700 int empty;
3702 umap = isl_union_map_copy(umap);
3703 umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(uset));
3704 empty = isl_union_map_is_empty(umap);
3705 isl_union_map_free(umap);
3707 return empty < 0 ? -1 : !empty;
3710 /* Does the range of "umap" intersect "uset"?
3712 static int range_intersects(__isl_keep isl_union_map *umap,
3713 __isl_keep isl_union_set *uset)
3715 int empty;
3717 umap = isl_union_map_copy(umap);
3718 umap = isl_union_map_intersect_range(umap, isl_union_set_copy(uset));
3719 empty = isl_union_map_is_empty(umap);
3720 isl_union_map_free(umap);
3722 return empty < 0 ? -1 : !empty;
3725 /* Are the condition dependences of "edge" local with respect to
3726 * the current schedule?
3728 * That is, are domain and range of the condition dependences mapped
3729 * to the same point?
3731 * In other words, is the condition false?
3733 static int is_condition_false(struct isl_sched_edge *edge)
3735 isl_union_map *umap;
3736 isl_map *map, *sched, *test;
3737 int local;
3739 umap = isl_union_map_copy(edge->tagged_condition);
3740 umap = isl_union_map_zip(umap);
3741 umap = isl_union_set_unwrap(isl_union_map_domain(umap));
3742 map = isl_map_from_union_map(umap);
3744 sched = node_extract_schedule(edge->src);
3745 map = isl_map_apply_domain(map, sched);
3746 sched = node_extract_schedule(edge->dst);
3747 map = isl_map_apply_range(map, sched);
3749 test = isl_map_identity(isl_map_get_space(map));
3750 local = isl_map_is_subset(map, test);
3751 isl_map_free(map);
3752 isl_map_free(test);
3754 return local;
3757 /* Does "graph" have any satisfied condition edges that
3758 * are adjacent to the conditional validity constraint with
3759 * domain "conditional_source" and range "conditional_sink"?
3761 * A satisfied condition is one that is not local.
3762 * If a condition was forced to be local already (i.e., marked as local)
3763 * then there is no need to check if it is in fact local.
3765 * Additionally, mark all adjacent condition edges found as local.
3767 static int has_adjacent_true_conditions(struct isl_sched_graph *graph,
3768 __isl_keep isl_union_set *conditional_source,
3769 __isl_keep isl_union_set *conditional_sink)
3771 int i;
3772 int any = 0;
3774 for (i = 0; i < graph->n_edge; ++i) {
3775 int adjacent, local;
3776 isl_union_map *condition;
3778 if (!graph->edge[i].condition)
3779 continue;
3780 if (graph->edge[i].local)
3781 continue;
3783 condition = graph->edge[i].tagged_condition;
3784 adjacent = domain_intersects(condition, conditional_sink);
3785 if (adjacent >= 0 && !adjacent)
3786 adjacent = range_intersects(condition,
3787 conditional_source);
3788 if (adjacent < 0)
3789 return -1;
3790 if (!adjacent)
3791 continue;
3793 graph->edge[i].local = 1;
3795 local = is_condition_false(&graph->edge[i]);
3796 if (local < 0)
3797 return -1;
3798 if (!local)
3799 any = 1;
3802 return any;
3805 /* Are there any violated conditional validity dependences with
3806 * adjacent condition dependences that are not local with respect
3807 * to the current schedule?
3808 * That is, is the conditional validity constraint violated?
3810 * Additionally, mark all those adjacent condition dependences as local.
3811 * We also mark those adjacent condition dependences that were not marked
3812 * as local before, but just happened to be local already. This ensures
3813 * that they remain local if the schedule is recomputed.
3815 * We first collect domain and range of all violated conditional validity
3816 * dependences and then check if there are any adjacent non-local
3817 * condition dependences.
3819 static int has_violated_conditional_constraint(isl_ctx *ctx,
3820 struct isl_sched_graph *graph)
3822 int i;
3823 int any = 0;
3824 isl_union_set *source, *sink;
3826 source = isl_union_set_empty(isl_space_params_alloc(ctx, 0));
3827 sink = isl_union_set_empty(isl_space_params_alloc(ctx, 0));
3828 for (i = 0; i < graph->n_edge; ++i) {
3829 isl_union_set *uset;
3830 isl_union_map *umap;
3831 int violated;
3833 if (!graph->edge[i].conditional_validity)
3834 continue;
3836 violated = is_violated(graph, i);
3837 if (violated < 0)
3838 goto error;
3839 if (!violated)
3840 continue;
3842 any = 1;
3844 umap = isl_union_map_copy(graph->edge[i].tagged_validity);
3845 uset = isl_union_map_domain(umap);
3846 source = isl_union_set_union(source, uset);
3847 source = isl_union_set_coalesce(source);
3849 umap = isl_union_map_copy(graph->edge[i].tagged_validity);
3850 uset = isl_union_map_range(umap);
3851 sink = isl_union_set_union(sink, uset);
3852 sink = isl_union_set_coalesce(sink);
3855 if (any)
3856 any = has_adjacent_true_conditions(graph, source, sink);
3858 isl_union_set_free(source);
3859 isl_union_set_free(sink);
3860 return any;
3861 error:
3862 isl_union_set_free(source);
3863 isl_union_set_free(sink);
3864 return -1;
3867 /* Compute a schedule for a connected dependence graph.
3868 * We try to find a sequence of as many schedule rows as possible that result
3869 * in non-negative dependence distances (independent of the previous rows
3870 * in the sequence, i.e., such that the sequence is tilable), with as
3871 * many of the initial rows as possible satisfying the coincidence constraints.
3872 * If we can't find any more rows we either
3873 * - split between SCCs and start over (assuming we found an interesting
3874 * pair of SCCs between which to split)
3875 * - continue with the next band (assuming the current band has at least
3876 * one row)
3877 * - try to carry as many dependences as possible and continue with the next
3878 * band
3880 * If Feautrier's algorithm is selected, we first recursively try to satisfy
3881 * as many validity dependences as possible. When all validity dependences
3882 * are satisfied we extend the schedule to a full-dimensional schedule.
3884 * If we manage to complete the schedule, we finish off by topologically
3885 * sorting the statements based on the remaining dependences.
3887 * If ctx->opt->schedule_outer_coincidence is set, then we force the
3888 * outermost dimension to satisfy the coincidence constraints. If this
3889 * turns out to be impossible, we fall back on the general scheme above
3890 * and try to carry as many dependences as possible.
3892 * If "graph" contains both condition and conditional validity dependences,
3893 * then we need to check that that the conditional schedule constraint
3894 * is satisfied, i.e., there are no violated conditional validity dependences
3895 * that are adjacent to any non-local condition dependences.
3896 * If there are, then we mark all those adjacent condition dependences
3897 * as local and recompute the current band. Those dependences that
3898 * are marked local will then be forced to be local.
3899 * The initial computation is performed with no dependences marked as local.
3900 * If we are lucky, then there will be no violated conditional validity
3901 * dependences adjacent to any non-local condition dependences.
3902 * Otherwise, we mark some additional condition dependences as local and
3903 * recompute. We continue this process until there are no violations left or
3904 * until we are no longer able to compute a schedule.
3905 * Since there are only a finite number of dependences,
3906 * there will only be a finite number of iterations.
3908 static int compute_schedule_wcc(isl_ctx *ctx, struct isl_sched_graph *graph)
3910 int has_coincidence;
3911 int use_coincidence;
3912 int force_coincidence = 0;
3913 int check_conditional;
3915 if (detect_sccs(ctx, graph) < 0)
3916 return -1;
3917 if (sort_sccs(graph) < 0)
3918 return -1;
3920 if (compute_maxvar(graph) < 0)
3921 return -1;
3923 if (need_feautrier_step(ctx, graph))
3924 return compute_schedule_wcc_feautrier(ctx, graph);
3926 clear_local_edges(graph);
3927 check_conditional = need_condition_check(graph);
3928 has_coincidence = has_any_coincidence(graph);
3930 if (ctx->opt->schedule_outer_coincidence)
3931 force_coincidence = 1;
3933 use_coincidence = has_coincidence;
3934 while (graph->n_row < graph->maxvar) {
3935 isl_vec *sol;
3936 int violated;
3937 int coincident;
3939 graph->src_scc = -1;
3940 graph->dst_scc = -1;
3942 if (setup_lp(ctx, graph, use_coincidence) < 0)
3943 return -1;
3944 sol = solve_lp(graph);
3945 if (!sol)
3946 return -1;
3947 if (sol->size == 0) {
3948 int empty = graph->n_total_row == graph->band_start;
3950 isl_vec_free(sol);
3951 if (use_coincidence && (!force_coincidence || !empty)) {
3952 use_coincidence = 0;
3953 continue;
3955 if (!ctx->opt->schedule_maximize_band_depth && !empty)
3956 return compute_next_band(ctx, graph);
3957 if (graph->src_scc >= 0)
3958 return compute_split_schedule(ctx, graph);
3959 if (!empty)
3960 return compute_next_band(ctx, graph);
3961 return carry_dependences(ctx, graph);
3963 coincident = !has_coincidence || use_coincidence;
3964 if (update_schedule(graph, sol, 1, coincident) < 0)
3965 return -1;
3967 if (!check_conditional)
3968 continue;
3969 violated = has_violated_conditional_constraint(ctx, graph);
3970 if (violated < 0)
3971 return -1;
3972 if (!violated)
3973 continue;
3974 if (reset_band(graph) < 0)
3975 return -1;
3976 use_coincidence = has_coincidence;
3979 if (graph->n_total_row > graph->band_start)
3980 next_band(graph);
3981 return sort_statements(ctx, graph);
3984 /* Add a row to the schedules that separates the SCCs and move
3985 * to the next band.
3987 static int split_on_scc(isl_ctx *ctx, struct isl_sched_graph *graph)
3989 int i;
3991 if (graph->n_total_row >= graph->max_row)
3992 isl_die(ctx, isl_error_internal,
3993 "too many schedule rows", return -1);
3995 for (i = 0; i < graph->n; ++i) {
3996 struct isl_sched_node *node = &graph->node[i];
3997 int row = isl_mat_rows(node->sched);
3999 isl_map_free(node->sched_map);
4000 node->sched_map = NULL;
4001 node->sched = isl_mat_add_zero_rows(node->sched, 1);
4002 node->sched = isl_mat_set_element_si(node->sched, row, 0,
4003 node->scc);
4004 if (!node->sched)
4005 return -1;
4006 node->band[graph->n_total_row] = graph->n_band;
4009 graph->n_total_row++;
4010 next_band(graph);
4012 return 0;
4015 /* Compute a schedule for each group of nodes identified by node->scc
4016 * separately and then combine the results.
4017 * If "wcc" is set then each of these groups belongs to a single
4018 * weakly connected component in the dependence graph so that
4019 * there is no need for compute_sub_schedule to look for weakly
4020 * connected components.
4022 * An extra schedule row is added first to separate the groups
4023 * unless the groups represent weakly connected components
4024 * (graph->weak is set) and the option schedule_separate_components
4025 * is not set.
4027 * The band_id is adjusted such that each component has a separate id.
4028 * Note that the band_id may have already been set to a value different
4029 * from zero by compute_split_schedule.
4031 static int compute_component_schedule(isl_ctx *ctx,
4032 struct isl_sched_graph *graph, int wcc)
4034 int component, i;
4035 int n, n_edge;
4036 int n_total_row, orig_total_row;
4037 int n_band, orig_band;
4039 if (!graph->weak || ctx->opt->schedule_separate_components)
4040 if (split_on_scc(ctx, graph) < 0)
4041 return -1;
4043 n_total_row = 0;
4044 orig_total_row = graph->n_total_row;
4045 n_band = 0;
4046 orig_band = graph->n_band;
4047 for (i = 0; i < graph->n; ++i)
4048 graph->node[i].band_id[graph->n_band] += graph->node[i].scc;
4049 for (component = 0; component < graph->scc; ++component) {
4050 n = 0;
4051 for (i = 0; i < graph->n; ++i)
4052 if (graph->node[i].scc == component)
4053 n++;
4054 n_edge = 0;
4055 for (i = 0; i < graph->n_edge; ++i)
4056 if (graph->edge[i].src->scc == component &&
4057 graph->edge[i].dst->scc == component)
4058 n_edge++;
4060 if (compute_sub_schedule(ctx, graph, n, n_edge,
4061 &node_scc_exactly,
4062 &edge_scc_exactly, component, wcc) < 0)
4063 return -1;
4064 if (graph->n_total_row > n_total_row)
4065 n_total_row = graph->n_total_row;
4066 graph->n_total_row = orig_total_row;
4067 if (graph->n_band > n_band)
4068 n_band = graph->n_band;
4069 graph->n_band = orig_band;
4072 graph->n_total_row = n_total_row;
4073 graph->n_band = n_band;
4075 return pad_schedule(graph);
4078 /* Compute a schedule for the given dependence graph.
4079 * We first check if the graph is connected (through validity and conditional
4080 * validity dependences) and, if not, compute a schedule
4081 * for each component separately.
4082 * If schedule_fuse is set to minimal fusion, then we check for strongly
4083 * connected components instead and compute a separate schedule for
4084 * each such strongly connected component.
4086 static int compute_schedule(isl_ctx *ctx, struct isl_sched_graph *graph)
4088 if (ctx->opt->schedule_fuse == ISL_SCHEDULE_FUSE_MIN) {
4089 if (detect_sccs(ctx, graph) < 0)
4090 return -1;
4091 } else {
4092 if (detect_wccs(ctx, graph) < 0)
4093 return -1;
4096 if (graph->scc > 1)
4097 return compute_component_schedule(ctx, graph, 1);
4099 return compute_schedule_wcc(ctx, graph);
4102 /* Compute a schedule on sc->domain that respects the given schedule
4103 * constraints.
4105 * In particular, the schedule respects all the validity dependences.
4106 * If the default isl scheduling algorithm is used, it tries to minimize
4107 * the dependence distances over the proximity dependences.
4108 * If Feautrier's scheduling algorithm is used, the proximity dependence
4109 * distances are only minimized during the extension to a full-dimensional
4110 * schedule.
4112 * If there are any condition and conditional validity dependences,
4113 * then the conditional validity dependences may be violated inside
4114 * a tilable band, provided they have no adjacent non-local
4115 * condition dependences.
4117 __isl_give isl_schedule *isl_schedule_constraints_compute_schedule(
4118 __isl_take isl_schedule_constraints *sc)
4120 isl_ctx *ctx = isl_schedule_constraints_get_ctx(sc);
4121 struct isl_sched_graph graph = { 0 };
4122 isl_schedule *sched;
4123 struct isl_extract_edge_data data;
4124 enum isl_edge_type i;
4126 sc = isl_schedule_constraints_align_params(sc);
4127 if (!sc)
4128 return NULL;
4130 graph.n = isl_union_set_n_set(sc->domain);
4131 if (graph.n == 0)
4132 goto empty;
4133 if (graph_alloc(ctx, &graph, graph.n,
4134 isl_schedule_constraints_n_map(sc)) < 0)
4135 goto error;
4136 if (compute_max_row(&graph, sc) < 0)
4137 goto error;
4138 graph.root = 1;
4139 graph.n = 0;
4140 if (isl_union_set_foreach_set(sc->domain, &extract_node, &graph) < 0)
4141 goto error;
4142 if (graph_init_table(ctx, &graph) < 0)
4143 goto error;
4144 for (i = isl_edge_first; i <= isl_edge_last; ++i)
4145 graph.max_edge[i] = isl_union_map_n_map(sc->constraint[i]);
4146 if (graph_init_edge_tables(ctx, &graph) < 0)
4147 goto error;
4148 graph.n_edge = 0;
4149 data.graph = &graph;
4150 for (i = isl_edge_first; i <= isl_edge_last; ++i) {
4151 data.type = i;
4152 if (isl_union_map_foreach_map(sc->constraint[i],
4153 &extract_edge, &data) < 0)
4154 goto error;
4157 if (compute_schedule(ctx, &graph) < 0)
4158 goto error;
4160 empty:
4161 sched = extract_schedule(&graph, isl_union_set_get_space(sc->domain));
4163 graph_free(ctx, &graph);
4164 isl_schedule_constraints_free(sc);
4166 return sched;
4167 error:
4168 graph_free(ctx, &graph);
4169 isl_schedule_constraints_free(sc);
4170 return NULL;
4173 /* Compute a schedule for the given union of domains that respects
4174 * all the validity dependences and minimizes
4175 * the dependence distances over the proximity dependences.
4177 * This function is kept for backward compatibility.
4179 __isl_give isl_schedule *isl_union_set_compute_schedule(
4180 __isl_take isl_union_set *domain,
4181 __isl_take isl_union_map *validity,
4182 __isl_take isl_union_map *proximity)
4184 isl_schedule_constraints *sc;
4186 sc = isl_schedule_constraints_on_domain(domain);
4187 sc = isl_schedule_constraints_set_validity(sc, validity);
4188 sc = isl_schedule_constraints_set_proximity(sc, proximity);
4190 return isl_schedule_constraints_compute_schedule(sc);