introduce schedule tree representation
[isl.git] / isl_scheduler.c
blobfcfccd06cb43fce007ab04c3ebdc9e2c9cdbf4a8
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 /* An edge in the dependence graph. An edge may be used to
374 * ensure validity of the generated schedule, to minimize the dependence
375 * distance or both
377 * map is the dependence relation, with i -> j in the map if j depends on i
378 * tagged_condition and tagged_validity contain the union of all tagged
379 * condition or conditional validity dependence relations that
380 * specialize the dependence relation "map"; that is,
381 * if (i -> a) -> (j -> b) is an element of "tagged_condition"
382 * or "tagged_validity", then i -> j is an element of "map".
383 * If these fields are NULL, then they represent the empty relation.
384 * src is the source node
385 * dst is the sink node
386 * validity is set if the edge is used to ensure correctness
387 * coincidence is used to enforce zero dependence distances
388 * proximity is set if the edge is used to minimize dependence distances
389 * condition is set if the edge represents a condition
390 * for a conditional validity schedule constraint
391 * local can only be set for condition edges and indicates that
392 * the dependence distance over the edge should be zero
393 * conditional_validity is set if the edge is used to conditionally
394 * ensure correctness
396 * For validity edges, start and end mark the sequence of inequality
397 * constraints in the LP problem that encode the validity constraint
398 * corresponding to this edge.
400 struct isl_sched_edge {
401 isl_map *map;
402 isl_union_map *tagged_condition;
403 isl_union_map *tagged_validity;
405 struct isl_sched_node *src;
406 struct isl_sched_node *dst;
408 unsigned validity : 1;
409 unsigned coincidence : 1;
410 unsigned proximity : 1;
411 unsigned local : 1;
412 unsigned condition : 1;
413 unsigned conditional_validity : 1;
415 int start;
416 int end;
419 /* Internal information about the dependence graph used during
420 * the construction of the schedule.
422 * intra_hmap is a cache, mapping dependence relations to their dual,
423 * for dependences from a node to itself
424 * inter_hmap is a cache, mapping dependence relations to their dual,
425 * for dependences between distinct nodes
426 * if compression is involved then the key for these maps
427 * it the original, uncompressed dependence relation, while
428 * the value is the dual of the compressed dependence relation.
430 * n is the number of nodes
431 * node is the list of nodes
432 * maxvar is the maximal number of variables over all nodes
433 * max_row is the allocated number of rows in the schedule
434 * n_row is the current (maximal) number of linearly independent
435 * rows in the node schedules
436 * n_total_row is the current number of rows in the node schedules
437 * n_band is the current number of completed bands
438 * band_start is the starting row in the node schedules of the current band
439 * root is set if this graph is the original dependence graph,
440 * without any splitting
442 * sorted contains a list of node indices sorted according to the
443 * SCC to which a node belongs
445 * n_edge is the number of edges
446 * edge is the list of edges
447 * max_edge contains the maximal number of edges of each type;
448 * in particular, it contains the number of edges in the inital graph.
449 * edge_table contains pointers into the edge array, hashed on the source
450 * and sink spaces; there is one such table for each type;
451 * a given edge may be referenced from more than one table
452 * if the corresponding relation appears in more than of the
453 * sets of dependences
455 * node_table contains pointers into the node array, hashed on the space
457 * region contains a list of variable sequences that should be non-trivial
459 * lp contains the (I)LP problem used to obtain new schedule rows
461 * src_scc and dst_scc are the source and sink SCCs of an edge with
462 * conflicting constraints
464 * scc represents the number of components
465 * weak is set if the components are weakly connected
467 struct isl_sched_graph {
468 isl_map_to_basic_set *intra_hmap;
469 isl_map_to_basic_set *inter_hmap;
471 struct isl_sched_node *node;
472 int n;
473 int maxvar;
474 int max_row;
475 int n_row;
477 int *sorted;
479 int n_band;
480 int n_total_row;
481 int band_start;
483 int root;
485 struct isl_sched_edge *edge;
486 int n_edge;
487 int max_edge[isl_edge_last + 1];
488 struct isl_hash_table *edge_table[isl_edge_last + 1];
490 struct isl_hash_table *node_table;
491 struct isl_region *region;
493 isl_basic_set *lp;
495 int src_scc;
496 int dst_scc;
498 int scc;
499 int weak;
502 /* Initialize node_table based on the list of nodes.
504 static int graph_init_table(isl_ctx *ctx, struct isl_sched_graph *graph)
506 int i;
508 graph->node_table = isl_hash_table_alloc(ctx, graph->n);
509 if (!graph->node_table)
510 return -1;
512 for (i = 0; i < graph->n; ++i) {
513 struct isl_hash_table_entry *entry;
514 uint32_t hash;
516 hash = isl_space_get_hash(graph->node[i].space);
517 entry = isl_hash_table_find(ctx, graph->node_table, hash,
518 &node_has_space,
519 graph->node[i].space, 1);
520 if (!entry)
521 return -1;
522 entry->data = &graph->node[i];
525 return 0;
528 /* Return a pointer to the node that lives within the given space,
529 * or NULL if there is no such node.
531 static struct isl_sched_node *graph_find_node(isl_ctx *ctx,
532 struct isl_sched_graph *graph, __isl_keep isl_space *dim)
534 struct isl_hash_table_entry *entry;
535 uint32_t hash;
537 hash = isl_space_get_hash(dim);
538 entry = isl_hash_table_find(ctx, graph->node_table, hash,
539 &node_has_space, dim, 0);
541 return entry ? entry->data : NULL;
544 static int edge_has_src_and_dst(const void *entry, const void *val)
546 const struct isl_sched_edge *edge = entry;
547 const struct isl_sched_edge *temp = val;
549 return edge->src == temp->src && edge->dst == temp->dst;
552 /* Add the given edge to graph->edge_table[type].
554 static int graph_edge_table_add(isl_ctx *ctx, struct isl_sched_graph *graph,
555 enum isl_edge_type type, struct isl_sched_edge *edge)
557 struct isl_hash_table_entry *entry;
558 uint32_t hash;
560 hash = isl_hash_init();
561 hash = isl_hash_builtin(hash, edge->src);
562 hash = isl_hash_builtin(hash, edge->dst);
563 entry = isl_hash_table_find(ctx, graph->edge_table[type], hash,
564 &edge_has_src_and_dst, edge, 1);
565 if (!entry)
566 return -1;
567 entry->data = edge;
569 return 0;
572 /* Allocate the edge_tables based on the maximal number of edges of
573 * each type.
575 static int graph_init_edge_tables(isl_ctx *ctx, struct isl_sched_graph *graph)
577 int i;
579 for (i = 0; i <= isl_edge_last; ++i) {
580 graph->edge_table[i] = isl_hash_table_alloc(ctx,
581 graph->max_edge[i]);
582 if (!graph->edge_table[i])
583 return -1;
586 return 0;
589 /* If graph->edge_table[type] contains an edge from the given source
590 * to the given destination, then return the hash table entry of this edge.
591 * Otherwise, return NULL.
593 static struct isl_hash_table_entry *graph_find_edge_entry(
594 struct isl_sched_graph *graph,
595 enum isl_edge_type type,
596 struct isl_sched_node *src, struct isl_sched_node *dst)
598 isl_ctx *ctx = isl_space_get_ctx(src->space);
599 uint32_t hash;
600 struct isl_sched_edge temp = { .src = src, .dst = dst };
602 hash = isl_hash_init();
603 hash = isl_hash_builtin(hash, temp.src);
604 hash = isl_hash_builtin(hash, temp.dst);
605 return isl_hash_table_find(ctx, graph->edge_table[type], hash,
606 &edge_has_src_and_dst, &temp, 0);
610 /* If graph->edge_table[type] contains an edge from the given source
611 * to the given destination, then return this edge.
612 * Otherwise, return NULL.
614 static struct isl_sched_edge *graph_find_edge(struct isl_sched_graph *graph,
615 enum isl_edge_type type,
616 struct isl_sched_node *src, struct isl_sched_node *dst)
618 struct isl_hash_table_entry *entry;
620 entry = graph_find_edge_entry(graph, type, src, dst);
621 if (!entry)
622 return NULL;
624 return entry->data;
627 /* Check whether the dependence graph has an edge of the given type
628 * between the given two nodes.
630 static int graph_has_edge(struct isl_sched_graph *graph,
631 enum isl_edge_type type,
632 struct isl_sched_node *src, struct isl_sched_node *dst)
634 struct isl_sched_edge *edge;
635 int empty;
637 edge = graph_find_edge(graph, type, src, dst);
638 if (!edge)
639 return 0;
641 empty = isl_map_plain_is_empty(edge->map);
642 if (empty < 0)
643 return -1;
645 return !empty;
648 /* Look for any edge with the same src, dst and map fields as "model".
650 * Return the matching edge if one can be found.
651 * Return "model" if no matching edge is found.
652 * Return NULL on error.
654 static struct isl_sched_edge *graph_find_matching_edge(
655 struct isl_sched_graph *graph, struct isl_sched_edge *model)
657 enum isl_edge_type i;
658 struct isl_sched_edge *edge;
660 for (i = isl_edge_first; i <= isl_edge_last; ++i) {
661 int is_equal;
663 edge = graph_find_edge(graph, i, model->src, model->dst);
664 if (!edge)
665 continue;
666 is_equal = isl_map_plain_is_equal(model->map, edge->map);
667 if (is_equal < 0)
668 return NULL;
669 if (is_equal)
670 return edge;
673 return model;
676 /* Remove the given edge from all the edge_tables that refer to it.
678 static void graph_remove_edge(struct isl_sched_graph *graph,
679 struct isl_sched_edge *edge)
681 isl_ctx *ctx = isl_map_get_ctx(edge->map);
682 enum isl_edge_type i;
684 for (i = isl_edge_first; i <= isl_edge_last; ++i) {
685 struct isl_hash_table_entry *entry;
687 entry = graph_find_edge_entry(graph, i, edge->src, edge->dst);
688 if (!entry)
689 continue;
690 if (entry->data != edge)
691 continue;
692 isl_hash_table_remove(ctx, graph->edge_table[i], entry);
696 /* Check whether the dependence graph has any edge
697 * between the given two nodes.
699 static int graph_has_any_edge(struct isl_sched_graph *graph,
700 struct isl_sched_node *src, struct isl_sched_node *dst)
702 enum isl_edge_type i;
703 int r;
705 for (i = isl_edge_first; i <= isl_edge_last; ++i) {
706 r = graph_has_edge(graph, i, src, dst);
707 if (r < 0 || r)
708 return r;
711 return r;
714 /* Check whether the dependence graph has a validity edge
715 * between the given two nodes.
717 * Conditional validity edges are essentially validity edges that
718 * can be ignored if the corresponding condition edges are iteration private.
719 * Here, we are only checking for the presence of validity
720 * edges, so we need to consider the conditional validity edges too.
721 * In particular, this function is used during the detection
722 * of strongly connected components and we cannot ignore
723 * conditional validity edges during this detection.
725 static int graph_has_validity_edge(struct isl_sched_graph *graph,
726 struct isl_sched_node *src, struct isl_sched_node *dst)
728 int r;
730 r = graph_has_edge(graph, isl_edge_validity, src, dst);
731 if (r < 0 || r)
732 return r;
734 return graph_has_edge(graph, isl_edge_conditional_validity, src, dst);
737 static int graph_alloc(isl_ctx *ctx, struct isl_sched_graph *graph,
738 int n_node, int n_edge)
740 int i;
742 graph->n = n_node;
743 graph->n_edge = n_edge;
744 graph->node = isl_calloc_array(ctx, struct isl_sched_node, graph->n);
745 graph->sorted = isl_calloc_array(ctx, int, graph->n);
746 graph->region = isl_alloc_array(ctx, struct isl_region, graph->n);
747 graph->edge = isl_calloc_array(ctx,
748 struct isl_sched_edge, graph->n_edge);
750 graph->intra_hmap = isl_map_to_basic_set_alloc(ctx, 2 * n_edge);
751 graph->inter_hmap = isl_map_to_basic_set_alloc(ctx, 2 * n_edge);
753 if (!graph->node || !graph->region || (graph->n_edge && !graph->edge) ||
754 !graph->sorted)
755 return -1;
757 for(i = 0; i < graph->n; ++i)
758 graph->sorted[i] = i;
760 return 0;
763 static void graph_free(isl_ctx *ctx, struct isl_sched_graph *graph)
765 int i;
767 isl_map_to_basic_set_free(graph->intra_hmap);
768 isl_map_to_basic_set_free(graph->inter_hmap);
770 if (graph->node)
771 for (i = 0; i < graph->n; ++i) {
772 isl_space_free(graph->node[i].space);
773 isl_set_free(graph->node[i].hull);
774 isl_multi_aff_free(graph->node[i].compress);
775 isl_multi_aff_free(graph->node[i].decompress);
776 isl_mat_free(graph->node[i].sched);
777 isl_map_free(graph->node[i].sched_map);
778 isl_mat_free(graph->node[i].cmap);
779 isl_mat_free(graph->node[i].cinv);
780 if (graph->root) {
781 free(graph->node[i].band);
782 free(graph->node[i].band_id);
783 free(graph->node[i].coincident);
786 free(graph->node);
787 free(graph->sorted);
788 if (graph->edge)
789 for (i = 0; i < graph->n_edge; ++i) {
790 isl_map_free(graph->edge[i].map);
791 isl_union_map_free(graph->edge[i].tagged_condition);
792 isl_union_map_free(graph->edge[i].tagged_validity);
794 free(graph->edge);
795 free(graph->region);
796 for (i = 0; i <= isl_edge_last; ++i)
797 isl_hash_table_free(ctx, graph->edge_table[i]);
798 isl_hash_table_free(ctx, graph->node_table);
799 isl_basic_set_free(graph->lp);
802 /* For each "set" on which this function is called, increment
803 * graph->n by one and update graph->maxvar.
805 static int init_n_maxvar(__isl_take isl_set *set, void *user)
807 struct isl_sched_graph *graph = user;
808 int nvar = isl_set_dim(set, isl_dim_set);
810 graph->n++;
811 if (nvar > graph->maxvar)
812 graph->maxvar = nvar;
814 isl_set_free(set);
816 return 0;
819 /* Add the number of basic maps in "map" to *n.
821 static int add_n_basic_map(__isl_take isl_map *map, void *user)
823 int *n = user;
825 *n += isl_map_n_basic_map(map);
826 isl_map_free(map);
828 return 0;
831 /* Compute the number of rows that should be allocated for the schedule.
832 * The graph can be split at most "n - 1" times, there can be at most
833 * one row for each dimension in the iteration domains plus two rows
834 * for each basic map in the dependences (in particular,
835 * we usually have one row, but it may be split by split_scaled),
836 * and there can be one extra row for ordering the statements.
837 * Note that if we have actually split "n - 1" times, then no ordering
838 * is needed, so in principle we could use "graph->n + 2 * graph->maxvar - 1".
839 * It is also practically impossible to exhaust both the number of dependences
840 * and the number of variables.
842 static int compute_max_row(struct isl_sched_graph *graph,
843 __isl_keep isl_schedule_constraints *sc)
845 enum isl_edge_type i;
846 int n_edge;
848 graph->n = 0;
849 graph->maxvar = 0;
850 if (isl_union_set_foreach_set(sc->domain, &init_n_maxvar, graph) < 0)
851 return -1;
852 n_edge = 0;
853 for (i = isl_edge_first; i <= isl_edge_last; ++i)
854 if (isl_union_map_foreach_map(sc->constraint[i],
855 &add_n_basic_map, &n_edge) < 0)
856 return -1;
857 graph->max_row = graph->n + 2 * n_edge + graph->maxvar;
859 return 0;
862 /* Does "bset" have any defining equalities for its set variables?
864 static int has_any_defining_equality(__isl_keep isl_basic_set *bset)
866 int i, n;
868 if (!bset)
869 return -1;
871 n = isl_basic_set_dim(bset, isl_dim_set);
872 for (i = 0; i < n; ++i) {
873 int has;
875 has = isl_basic_set_has_defining_equality(bset, isl_dim_set, i,
876 NULL);
877 if (has < 0 || has)
878 return has;
881 return 0;
884 /* Add a new node to the graph representing the given space.
885 * "nvar" is the (possibly compressed) number of variables and
886 * may be smaller than then number of set variables in "space"
887 * if "compressed" is set.
888 * If "compressed" is set, then "hull" represents the constraints
889 * that were used to derive the compression, while "compress" and
890 * "decompress" map the original space to the compressed space and
891 * vice versa.
892 * If "compressed" is not set, then "hull", "compress" and "decompress"
893 * should be NULL.
895 static int add_node(struct isl_sched_graph *graph, __isl_take isl_space *space,
896 int nvar, int compressed, __isl_take isl_set *hull,
897 __isl_take isl_multi_aff *compress,
898 __isl_take isl_multi_aff *decompress)
900 int nparam;
901 isl_ctx *ctx;
902 isl_mat *sched;
903 int *band, *band_id, *coincident;
905 if (!space)
906 return -1;
908 ctx = isl_space_get_ctx(space);
909 nparam = isl_space_dim(space, isl_dim_param);
910 if (!ctx->opt->schedule_parametric)
911 nparam = 0;
912 sched = isl_mat_alloc(ctx, 0, 1 + nparam + nvar);
913 graph->node[graph->n].space = space;
914 graph->node[graph->n].nvar = nvar;
915 graph->node[graph->n].nparam = nparam;
916 graph->node[graph->n].sched = sched;
917 graph->node[graph->n].sched_map = NULL;
918 band = isl_alloc_array(ctx, int, graph->max_row);
919 graph->node[graph->n].band = band;
920 band_id = isl_calloc_array(ctx, int, graph->max_row);
921 graph->node[graph->n].band_id = band_id;
922 coincident = isl_calloc_array(ctx, int, graph->max_row);
923 graph->node[graph->n].coincident = coincident;
924 graph->node[graph->n].compressed = compressed;
925 graph->node[graph->n].hull = hull;
926 graph->node[graph->n].compress = compress;
927 graph->node[graph->n].decompress = decompress;
928 graph->n++;
930 if (!space || !sched ||
931 (graph->max_row && (!band || !band_id || !coincident)))
932 return -1;
933 if (compressed && (!hull || !compress || !decompress))
934 return -1;
936 return 0;
939 /* Add a new node to the graph representing the given set.
941 * If any of the set variables is defined by an equality, then
942 * we perform variable compression such that we can perform
943 * the scheduling on the compressed domain.
945 static int extract_node(__isl_take isl_set *set, void *user)
947 int nvar;
948 int has_equality;
949 isl_space *space;
950 isl_basic_set *hull;
951 isl_set *hull_set;
952 isl_morph *morph;
953 isl_multi_aff *compress, *decompress;
954 struct isl_sched_graph *graph = user;
956 space = isl_set_get_space(set);
957 hull = isl_set_affine_hull(set);
958 hull = isl_basic_set_remove_divs(hull);
959 nvar = isl_space_dim(space, isl_dim_set);
960 has_equality = has_any_defining_equality(hull);
962 if (has_equality < 0)
963 goto error;
964 if (!has_equality) {
965 isl_basic_set_free(hull);
966 return add_node(graph, space, nvar, 0, NULL, NULL, NULL);
969 morph = isl_basic_set_variable_compression(hull, isl_dim_set);
970 nvar = isl_morph_ran_dim(morph, isl_dim_set);
971 compress = isl_morph_get_var_multi_aff(morph);
972 morph = isl_morph_inverse(morph);
973 decompress = isl_morph_get_var_multi_aff(morph);
974 isl_morph_free(morph);
976 hull_set = isl_set_from_basic_set(hull);
977 return add_node(graph, space, nvar, 1, hull_set, compress, decompress);
978 error:
979 isl_basic_set_free(hull);
980 isl_space_free(space);
981 return -1;
984 struct isl_extract_edge_data {
985 enum isl_edge_type type;
986 struct isl_sched_graph *graph;
989 /* Merge edge2 into edge1, freeing the contents of edge2.
990 * "type" is the type of the schedule constraint from which edge2 was
991 * extracted.
992 * Return 0 on success and -1 on failure.
994 * edge1 and edge2 are assumed to have the same value for the map field.
996 static int merge_edge(enum isl_edge_type type, struct isl_sched_edge *edge1,
997 struct isl_sched_edge *edge2)
999 edge1->validity |= edge2->validity;
1000 edge1->coincidence |= edge2->coincidence;
1001 edge1->proximity |= edge2->proximity;
1002 edge1->condition |= edge2->condition;
1003 edge1->conditional_validity |= edge2->conditional_validity;
1004 isl_map_free(edge2->map);
1006 if (type == isl_edge_condition) {
1007 if (!edge1->tagged_condition)
1008 edge1->tagged_condition = edge2->tagged_condition;
1009 else
1010 edge1->tagged_condition =
1011 isl_union_map_union(edge1->tagged_condition,
1012 edge2->tagged_condition);
1015 if (type == isl_edge_conditional_validity) {
1016 if (!edge1->tagged_validity)
1017 edge1->tagged_validity = edge2->tagged_validity;
1018 else
1019 edge1->tagged_validity =
1020 isl_union_map_union(edge1->tagged_validity,
1021 edge2->tagged_validity);
1024 if (type == isl_edge_condition && !edge1->tagged_condition)
1025 return -1;
1026 if (type == isl_edge_conditional_validity && !edge1->tagged_validity)
1027 return -1;
1029 return 0;
1032 /* Insert dummy tags in domain and range of "map".
1034 * In particular, if "map" is of the form
1036 * A -> B
1038 * then return
1040 * [A -> dummy_tag] -> [B -> dummy_tag]
1042 * where the dummy_tags are identical and equal to any dummy tags
1043 * introduced by any other call to this function.
1045 static __isl_give isl_map *insert_dummy_tags(__isl_take isl_map *map)
1047 static char dummy;
1048 isl_ctx *ctx;
1049 isl_id *id;
1050 isl_space *space;
1051 isl_set *domain, *range;
1053 ctx = isl_map_get_ctx(map);
1055 id = isl_id_alloc(ctx, NULL, &dummy);
1056 space = isl_space_params(isl_map_get_space(map));
1057 space = isl_space_set_from_params(space);
1058 space = isl_space_set_tuple_id(space, isl_dim_set, id);
1059 space = isl_space_map_from_set(space);
1061 domain = isl_map_wrap(map);
1062 range = isl_map_wrap(isl_map_universe(space));
1063 map = isl_map_from_domain_and_range(domain, range);
1064 map = isl_map_zip(map);
1066 return map;
1069 /* Given that at least one of "src" or "dst" is compressed, return
1070 * a map between the spaces of these nodes restricted to the affine
1071 * hull that was used in the compression.
1073 static __isl_give isl_map *extract_hull(struct isl_sched_node *src,
1074 struct isl_sched_node *dst)
1076 isl_set *dom, *ran;
1078 if (src->compressed)
1079 dom = isl_set_copy(src->hull);
1080 else
1081 dom = isl_set_universe(isl_space_copy(src->space));
1082 if (dst->compressed)
1083 ran = isl_set_copy(dst->hull);
1084 else
1085 ran = isl_set_universe(isl_space_copy(dst->space));
1087 return isl_map_from_domain_and_range(dom, ran);
1090 /* Intersect the domains of the nested relations in domain and range
1091 * of "tagged" with "map".
1093 static __isl_give isl_map *map_intersect_domains(__isl_take isl_map *tagged,
1094 __isl_keep isl_map *map)
1096 isl_set *set;
1098 tagged = isl_map_zip(tagged);
1099 set = isl_map_wrap(isl_map_copy(map));
1100 tagged = isl_map_intersect_domain(tagged, set);
1101 tagged = isl_map_zip(tagged);
1102 return tagged;
1105 /* Add a new edge to the graph based on the given map
1106 * and add it to data->graph->edge_table[data->type].
1107 * If a dependence relation of a given type happens to be identical
1108 * to one of the dependence relations of a type that was added before,
1109 * then we don't create a new edge, but instead mark the original edge
1110 * as also representing a dependence of the current type.
1112 * Edges of type isl_edge_condition or isl_edge_conditional_validity
1113 * may be specified as "tagged" dependence relations. That is, "map"
1114 * may contain elements (i -> a) -> (j -> b), where i -> j denotes
1115 * the dependence on iterations and a and b are tags.
1116 * edge->map is set to the relation containing the elements i -> j,
1117 * while edge->tagged_condition and edge->tagged_validity contain
1118 * the union of all the "map" relations
1119 * for which extract_edge is called that result in the same edge->map.
1121 * If the source or the destination node is compressed, then
1122 * intersect both "map" and "tagged" with the constraints that
1123 * were used to construct the compression.
1124 * This ensures that there are no schedule constraints defined
1125 * outside of these domains, while the scheduler no longer has
1126 * any control over those outside parts.
1128 static int extract_edge(__isl_take isl_map *map, void *user)
1130 isl_ctx *ctx = isl_map_get_ctx(map);
1131 struct isl_extract_edge_data *data = user;
1132 struct isl_sched_graph *graph = data->graph;
1133 struct isl_sched_node *src, *dst;
1134 isl_space *dim;
1135 struct isl_sched_edge *edge;
1136 isl_map *tagged = NULL;
1138 if (data->type == isl_edge_condition ||
1139 data->type == isl_edge_conditional_validity) {
1140 if (isl_map_can_zip(map)) {
1141 tagged = isl_map_copy(map);
1142 map = isl_set_unwrap(isl_map_domain(isl_map_zip(map)));
1143 } else {
1144 tagged = insert_dummy_tags(isl_map_copy(map));
1148 dim = isl_space_domain(isl_map_get_space(map));
1149 src = graph_find_node(ctx, graph, dim);
1150 isl_space_free(dim);
1151 dim = isl_space_range(isl_map_get_space(map));
1152 dst = graph_find_node(ctx, graph, dim);
1153 isl_space_free(dim);
1155 if (!src || !dst) {
1156 isl_map_free(map);
1157 isl_map_free(tagged);
1158 return 0;
1161 if (src->compressed || dst->compressed) {
1162 isl_map *hull;
1163 hull = extract_hull(src, dst);
1164 if (tagged)
1165 tagged = map_intersect_domains(tagged, hull);
1166 map = isl_map_intersect(map, hull);
1169 graph->edge[graph->n_edge].src = src;
1170 graph->edge[graph->n_edge].dst = dst;
1171 graph->edge[graph->n_edge].map = map;
1172 graph->edge[graph->n_edge].validity = 0;
1173 graph->edge[graph->n_edge].coincidence = 0;
1174 graph->edge[graph->n_edge].proximity = 0;
1175 graph->edge[graph->n_edge].condition = 0;
1176 graph->edge[graph->n_edge].local = 0;
1177 graph->edge[graph->n_edge].conditional_validity = 0;
1178 graph->edge[graph->n_edge].tagged_condition = NULL;
1179 graph->edge[graph->n_edge].tagged_validity = NULL;
1180 if (data->type == isl_edge_validity)
1181 graph->edge[graph->n_edge].validity = 1;
1182 if (data->type == isl_edge_coincidence)
1183 graph->edge[graph->n_edge].coincidence = 1;
1184 if (data->type == isl_edge_proximity)
1185 graph->edge[graph->n_edge].proximity = 1;
1186 if (data->type == isl_edge_condition) {
1187 graph->edge[graph->n_edge].condition = 1;
1188 graph->edge[graph->n_edge].tagged_condition =
1189 isl_union_map_from_map(tagged);
1191 if (data->type == isl_edge_conditional_validity) {
1192 graph->edge[graph->n_edge].conditional_validity = 1;
1193 graph->edge[graph->n_edge].tagged_validity =
1194 isl_union_map_from_map(tagged);
1197 edge = graph_find_matching_edge(graph, &graph->edge[graph->n_edge]);
1198 if (!edge) {
1199 graph->n_edge++;
1200 return -1;
1202 if (edge == &graph->edge[graph->n_edge])
1203 return graph_edge_table_add(ctx, graph, data->type,
1204 &graph->edge[graph->n_edge++]);
1206 if (merge_edge(data->type, edge, &graph->edge[graph->n_edge]) < 0)
1207 return -1;
1209 return graph_edge_table_add(ctx, graph, data->type, edge);
1212 /* Check whether there is any dependence from node[j] to node[i]
1213 * or from node[i] to node[j].
1215 static int node_follows_weak(int i, int j, void *user)
1217 int f;
1218 struct isl_sched_graph *graph = user;
1220 f = graph_has_any_edge(graph, &graph->node[j], &graph->node[i]);
1221 if (f < 0 || f)
1222 return f;
1223 return graph_has_any_edge(graph, &graph->node[i], &graph->node[j]);
1226 /* Check whether there is a (conditional) validity dependence from node[j]
1227 * to node[i], forcing node[i] to follow node[j].
1229 static int node_follows_strong(int i, int j, void *user)
1231 struct isl_sched_graph *graph = user;
1233 return graph_has_validity_edge(graph, &graph->node[j], &graph->node[i]);
1236 /* Use Tarjan's algorithm for computing the strongly connected components
1237 * in the dependence graph (only validity edges).
1238 * If weak is set, we consider the graph to be undirected and
1239 * we effectively compute the (weakly) connected components.
1240 * Additionally, we also consider other edges when weak is set.
1242 static int detect_ccs(isl_ctx *ctx, struct isl_sched_graph *graph, int weak)
1244 int i, n;
1245 struct isl_tarjan_graph *g = NULL;
1247 g = isl_tarjan_graph_init(ctx, graph->n,
1248 weak ? &node_follows_weak : &node_follows_strong, graph);
1249 if (!g)
1250 return -1;
1252 graph->weak = weak;
1253 graph->scc = 0;
1254 i = 0;
1255 n = graph->n;
1256 while (n) {
1257 while (g->order[i] != -1) {
1258 graph->node[g->order[i]].scc = graph->scc;
1259 --n;
1260 ++i;
1262 ++i;
1263 graph->scc++;
1266 isl_tarjan_graph_free(g);
1268 return 0;
1271 /* Apply Tarjan's algorithm to detect the strongly connected components
1272 * in the dependence graph.
1274 static int detect_sccs(isl_ctx *ctx, struct isl_sched_graph *graph)
1276 return detect_ccs(ctx, graph, 0);
1279 /* Apply Tarjan's algorithm to detect the (weakly) connected components
1280 * in the dependence graph.
1282 static int detect_wccs(isl_ctx *ctx, struct isl_sched_graph *graph)
1284 return detect_ccs(ctx, graph, 1);
1287 static int cmp_scc(const void *a, const void *b, void *data)
1289 struct isl_sched_graph *graph = data;
1290 const int *i1 = a;
1291 const int *i2 = b;
1293 return graph->node[*i1].scc - graph->node[*i2].scc;
1296 /* Sort the elements of graph->sorted according to the corresponding SCCs.
1298 static int sort_sccs(struct isl_sched_graph *graph)
1300 return isl_sort(graph->sorted, graph->n, sizeof(int), &cmp_scc, graph);
1303 /* Given a dependence relation R from "node" to itself,
1304 * construct the set of coefficients of valid constraints for elements
1305 * in that dependence relation.
1306 * In particular, the result contains tuples of coefficients
1307 * c_0, c_n, c_x such that
1309 * c_0 + c_n n + c_x y - c_x x >= 0 for each (x,y) in R
1311 * or, equivalently,
1313 * c_0 + c_n n + c_x d >= 0 for each d in delta R = { y - x | (x,y) in R }
1315 * We choose here to compute the dual of delta R.
1316 * Alternatively, we could have computed the dual of R, resulting
1317 * in a set of tuples c_0, c_n, c_x, c_y, and then
1318 * plugged in (c_0, c_n, c_x, -c_x).
1320 * If "node" has been compressed, then the dependence relation
1321 * is also compressed before the set of coefficients is computed.
1323 static __isl_give isl_basic_set *intra_coefficients(
1324 struct isl_sched_graph *graph, struct isl_sched_node *node,
1325 __isl_take isl_map *map)
1327 isl_set *delta;
1328 isl_map *key;
1329 isl_basic_set *coef;
1331 if (isl_map_to_basic_set_has(graph->intra_hmap, map))
1332 return isl_map_to_basic_set_get(graph->intra_hmap, map);
1334 key = isl_map_copy(map);
1335 if (node->compressed) {
1336 map = isl_map_preimage_domain_multi_aff(map,
1337 isl_multi_aff_copy(node->decompress));
1338 map = isl_map_preimage_range_multi_aff(map,
1339 isl_multi_aff_copy(node->decompress));
1341 delta = isl_set_remove_divs(isl_map_deltas(map));
1342 coef = isl_set_coefficients(delta);
1343 graph->intra_hmap = isl_map_to_basic_set_set(graph->intra_hmap, key,
1344 isl_basic_set_copy(coef));
1346 return coef;
1349 /* Given a dependence relation R, construct the set of coefficients
1350 * of valid constraints for elements in that dependence relation.
1351 * In particular, the result contains tuples of coefficients
1352 * c_0, c_n, c_x, c_y such that
1354 * c_0 + c_n n + c_x x + c_y y >= 0 for each (x,y) in R
1356 * If the source or destination nodes of "edge" have been compressed,
1357 * then the dependence relation is also compressed before
1358 * the set of coefficients is computed.
1360 static __isl_give isl_basic_set *inter_coefficients(
1361 struct isl_sched_graph *graph, struct isl_sched_edge *edge,
1362 __isl_take isl_map *map)
1364 isl_set *set;
1365 isl_map *key;
1366 isl_basic_set *coef;
1368 if (isl_map_to_basic_set_has(graph->inter_hmap, map))
1369 return isl_map_to_basic_set_get(graph->inter_hmap, map);
1371 key = isl_map_copy(map);
1372 if (edge->src->compressed)
1373 map = isl_map_preimage_domain_multi_aff(map,
1374 isl_multi_aff_copy(edge->src->decompress));
1375 if (edge->dst->compressed)
1376 map = isl_map_preimage_range_multi_aff(map,
1377 isl_multi_aff_copy(edge->dst->decompress));
1378 set = isl_map_wrap(isl_map_remove_divs(map));
1379 coef = isl_set_coefficients(set);
1380 graph->inter_hmap = isl_map_to_basic_set_set(graph->inter_hmap, key,
1381 isl_basic_set_copy(coef));
1383 return coef;
1386 /* Add constraints to graph->lp that force validity for the given
1387 * dependence from a node i to itself.
1388 * That is, add constraints that enforce
1390 * (c_i_0 + c_i_n n + c_i_x y) - (c_i_0 + c_i_n n + c_i_x x)
1391 * = c_i_x (y - x) >= 0
1393 * for each (x,y) in R.
1394 * We obtain general constraints on coefficients (c_0, c_n, c_x)
1395 * of valid constraints for (y - x) and then plug in (0, 0, c_i_x^+ - c_i_x^-),
1396 * where c_i_x = c_i_x^+ - c_i_x^-, with c_i_x^+ and c_i_x^- non-negative.
1397 * In graph->lp, the c_i_x^- appear before their c_i_x^+ counterpart.
1399 * Actually, we do not construct constraints for the c_i_x themselves,
1400 * but for the coefficients of c_i_x written as a linear combination
1401 * of the columns in node->cmap.
1403 static int add_intra_validity_constraints(struct isl_sched_graph *graph,
1404 struct isl_sched_edge *edge)
1406 unsigned total;
1407 isl_map *map = isl_map_copy(edge->map);
1408 isl_ctx *ctx = isl_map_get_ctx(map);
1409 isl_space *dim;
1410 isl_dim_map *dim_map;
1411 isl_basic_set *coef;
1412 struct isl_sched_node *node = edge->src;
1414 coef = intra_coefficients(graph, node, map);
1416 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
1418 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
1419 isl_space_dim(dim, isl_dim_set), isl_mat_copy(node->cmap));
1420 if (!coef)
1421 goto error;
1423 total = isl_basic_set_total_dim(graph->lp);
1424 dim_map = isl_dim_map_alloc(ctx, total);
1425 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 1, 2,
1426 isl_space_dim(dim, isl_dim_set), 1,
1427 node->nvar, -1);
1428 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 2, 2,
1429 isl_space_dim(dim, isl_dim_set), 1,
1430 node->nvar, 1);
1431 graph->lp = isl_basic_set_extend_constraints(graph->lp,
1432 coef->n_eq, coef->n_ineq);
1433 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
1434 coef, dim_map);
1435 isl_space_free(dim);
1437 return 0;
1438 error:
1439 isl_space_free(dim);
1440 return -1;
1443 /* Add constraints to graph->lp that force validity for the given
1444 * dependence from node i to node j.
1445 * That is, add constraints that enforce
1447 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x) >= 0
1449 * for each (x,y) in R.
1450 * We obtain general constraints on coefficients (c_0, c_n, c_x, c_y)
1451 * of valid constraints for R and then plug in
1452 * (c_j_0 - c_i_0, c_j_n^+ - c_j_n^- - (c_i_n^+ - c_i_n^-),
1453 * c_j_x^+ - c_j_x^- - (c_i_x^+ - c_i_x^-)),
1454 * where c_* = c_*^+ - c_*^-, with c_*^+ and c_*^- non-negative.
1455 * In graph->lp, the c_*^- appear before their c_*^+ counterpart.
1457 * Actually, we do not construct constraints for the c_*_x themselves,
1458 * but for the coefficients of c_*_x written as a linear combination
1459 * of the columns in node->cmap.
1461 static int add_inter_validity_constraints(struct isl_sched_graph *graph,
1462 struct isl_sched_edge *edge)
1464 unsigned total;
1465 isl_map *map = isl_map_copy(edge->map);
1466 isl_ctx *ctx = isl_map_get_ctx(map);
1467 isl_space *dim;
1468 isl_dim_map *dim_map;
1469 isl_basic_set *coef;
1470 struct isl_sched_node *src = edge->src;
1471 struct isl_sched_node *dst = edge->dst;
1473 coef = inter_coefficients(graph, edge, map);
1475 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
1477 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
1478 isl_space_dim(dim, isl_dim_set), isl_mat_copy(src->cmap));
1479 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
1480 isl_space_dim(dim, isl_dim_set) + src->nvar,
1481 isl_mat_copy(dst->cmap));
1482 if (!coef)
1483 goto error;
1485 total = isl_basic_set_total_dim(graph->lp);
1486 dim_map = isl_dim_map_alloc(ctx, total);
1488 isl_dim_map_range(dim_map, dst->start, 0, 0, 0, 1, 1);
1489 isl_dim_map_range(dim_map, dst->start + 1, 2, 1, 1, dst->nparam, -1);
1490 isl_dim_map_range(dim_map, dst->start + 2, 2, 1, 1, dst->nparam, 1);
1491 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 1, 2,
1492 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
1493 dst->nvar, -1);
1494 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 2, 2,
1495 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
1496 dst->nvar, 1);
1498 isl_dim_map_range(dim_map, src->start, 0, 0, 0, 1, -1);
1499 isl_dim_map_range(dim_map, src->start + 1, 2, 1, 1, src->nparam, 1);
1500 isl_dim_map_range(dim_map, src->start + 2, 2, 1, 1, src->nparam, -1);
1501 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 1, 2,
1502 isl_space_dim(dim, isl_dim_set), 1,
1503 src->nvar, 1);
1504 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 2, 2,
1505 isl_space_dim(dim, isl_dim_set), 1,
1506 src->nvar, -1);
1508 edge->start = graph->lp->n_ineq;
1509 graph->lp = isl_basic_set_extend_constraints(graph->lp,
1510 coef->n_eq, coef->n_ineq);
1511 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
1512 coef, dim_map);
1513 if (!graph->lp)
1514 goto error;
1515 isl_space_free(dim);
1516 edge->end = graph->lp->n_ineq;
1518 return 0;
1519 error:
1520 isl_space_free(dim);
1521 return -1;
1524 /* Add constraints to graph->lp that bound the dependence distance for the given
1525 * dependence from a node i to itself.
1526 * If s = 1, we add the constraint
1528 * c_i_x (y - x) <= m_0 + m_n n
1530 * or
1532 * -c_i_x (y - x) + m_0 + m_n n >= 0
1534 * for each (x,y) in R.
1535 * If s = -1, we add the constraint
1537 * -c_i_x (y - x) <= m_0 + m_n n
1539 * or
1541 * c_i_x (y - x) + m_0 + m_n n >= 0
1543 * for each (x,y) in R.
1544 * We obtain general constraints on coefficients (c_0, c_n, c_x)
1545 * of valid constraints for (y - x) and then plug in (m_0, m_n, -s * c_i_x),
1546 * with each coefficient (except m_0) represented as a pair of non-negative
1547 * coefficients.
1549 * Actually, we do not construct constraints for the c_i_x themselves,
1550 * but for the coefficients of c_i_x written as a linear combination
1551 * of the columns in node->cmap.
1554 * If "local" is set, then we add constraints
1556 * c_i_x (y - x) <= 0
1558 * or
1560 * -c_i_x (y - x) <= 0
1562 * instead, forcing the dependence distance to be (less than or) equal to 0.
1563 * That is, we plug in (0, 0, -s * c_i_x),
1564 * Note that dependences marked local are treated as validity constraints
1565 * by add_all_validity_constraints and therefore also have
1566 * their distances bounded by 0 from below.
1568 static int add_intra_proximity_constraints(struct isl_sched_graph *graph,
1569 struct isl_sched_edge *edge, int s, int local)
1571 unsigned total;
1572 unsigned nparam;
1573 isl_map *map = isl_map_copy(edge->map);
1574 isl_ctx *ctx = isl_map_get_ctx(map);
1575 isl_space *dim;
1576 isl_dim_map *dim_map;
1577 isl_basic_set *coef;
1578 struct isl_sched_node *node = edge->src;
1580 coef = intra_coefficients(graph, node, map);
1582 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
1584 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
1585 isl_space_dim(dim, isl_dim_set), isl_mat_copy(node->cmap));
1586 if (!coef)
1587 goto error;
1589 nparam = isl_space_dim(node->space, isl_dim_param);
1590 total = isl_basic_set_total_dim(graph->lp);
1591 dim_map = isl_dim_map_alloc(ctx, total);
1593 if (!local) {
1594 isl_dim_map_range(dim_map, 1, 0, 0, 0, 1, 1);
1595 isl_dim_map_range(dim_map, 4, 2, 1, 1, nparam, -1);
1596 isl_dim_map_range(dim_map, 5, 2, 1, 1, nparam, 1);
1598 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 1, 2,
1599 isl_space_dim(dim, isl_dim_set), 1,
1600 node->nvar, s);
1601 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 2, 2,
1602 isl_space_dim(dim, isl_dim_set), 1,
1603 node->nvar, -s);
1604 graph->lp = isl_basic_set_extend_constraints(graph->lp,
1605 coef->n_eq, coef->n_ineq);
1606 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
1607 coef, dim_map);
1608 isl_space_free(dim);
1610 return 0;
1611 error:
1612 isl_space_free(dim);
1613 return -1;
1616 /* Add constraints to graph->lp that bound the dependence distance for the given
1617 * dependence from node i to node j.
1618 * If s = 1, we add the constraint
1620 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x)
1621 * <= m_0 + m_n n
1623 * or
1625 * -(c_j_0 + c_j_n n + c_j_x y) + (c_i_0 + c_i_n n + c_i_x x) +
1626 * m_0 + m_n n >= 0
1628 * for each (x,y) in R.
1629 * If s = -1, we add the constraint
1631 * -((c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x))
1632 * <= m_0 + m_n n
1634 * or
1636 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x) +
1637 * m_0 + m_n n >= 0
1639 * for each (x,y) in R.
1640 * We obtain general constraints on coefficients (c_0, c_n, c_x, c_y)
1641 * of valid constraints for R and then plug in
1642 * (m_0 - s*c_j_0 + s*c_i_0, m_n - s*c_j_n + s*c_i_n,
1643 * -s*c_j_x+s*c_i_x)
1644 * with each coefficient (except m_0, c_j_0 and c_i_0)
1645 * represented as a pair of non-negative coefficients.
1647 * Actually, we do not construct constraints for the c_*_x themselves,
1648 * but for the coefficients of c_*_x written as a linear combination
1649 * of the columns in node->cmap.
1652 * If "local" is set, then we add constraints
1654 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x) <= 0
1656 * or
1658 * -((c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x)) <= 0
1660 * instead, forcing the dependence distance to be (less than or) equal to 0.
1661 * That is, we plug in
1662 * (-s*c_j_0 + s*c_i_0, -s*c_j_n + s*c_i_n, -s*c_j_x+s*c_i_x).
1663 * Note that dependences marked local are treated as validity constraints
1664 * by add_all_validity_constraints and therefore also have
1665 * their distances bounded by 0 from below.
1667 static int add_inter_proximity_constraints(struct isl_sched_graph *graph,
1668 struct isl_sched_edge *edge, int s, int local)
1670 unsigned total;
1671 unsigned nparam;
1672 isl_map *map = isl_map_copy(edge->map);
1673 isl_ctx *ctx = isl_map_get_ctx(map);
1674 isl_space *dim;
1675 isl_dim_map *dim_map;
1676 isl_basic_set *coef;
1677 struct isl_sched_node *src = edge->src;
1678 struct isl_sched_node *dst = edge->dst;
1680 coef = inter_coefficients(graph, edge, map);
1682 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
1684 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
1685 isl_space_dim(dim, isl_dim_set), isl_mat_copy(src->cmap));
1686 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
1687 isl_space_dim(dim, isl_dim_set) + src->nvar,
1688 isl_mat_copy(dst->cmap));
1689 if (!coef)
1690 goto error;
1692 nparam = isl_space_dim(src->space, isl_dim_param);
1693 total = isl_basic_set_total_dim(graph->lp);
1694 dim_map = isl_dim_map_alloc(ctx, total);
1696 if (!local) {
1697 isl_dim_map_range(dim_map, 1, 0, 0, 0, 1, 1);
1698 isl_dim_map_range(dim_map, 4, 2, 1, 1, nparam, -1);
1699 isl_dim_map_range(dim_map, 5, 2, 1, 1, nparam, 1);
1702 isl_dim_map_range(dim_map, dst->start, 0, 0, 0, 1, -s);
1703 isl_dim_map_range(dim_map, dst->start + 1, 2, 1, 1, dst->nparam, s);
1704 isl_dim_map_range(dim_map, dst->start + 2, 2, 1, 1, dst->nparam, -s);
1705 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 1, 2,
1706 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
1707 dst->nvar, s);
1708 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 2, 2,
1709 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
1710 dst->nvar, -s);
1712 isl_dim_map_range(dim_map, src->start, 0, 0, 0, 1, s);
1713 isl_dim_map_range(dim_map, src->start + 1, 2, 1, 1, src->nparam, -s);
1714 isl_dim_map_range(dim_map, src->start + 2, 2, 1, 1, src->nparam, s);
1715 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 1, 2,
1716 isl_space_dim(dim, isl_dim_set), 1,
1717 src->nvar, -s);
1718 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 2, 2,
1719 isl_space_dim(dim, isl_dim_set), 1,
1720 src->nvar, s);
1722 graph->lp = isl_basic_set_extend_constraints(graph->lp,
1723 coef->n_eq, coef->n_ineq);
1724 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
1725 coef, dim_map);
1726 isl_space_free(dim);
1728 return 0;
1729 error:
1730 isl_space_free(dim);
1731 return -1;
1734 /* Add all validity constraints to graph->lp.
1736 * An edge that is forced to be local needs to have its dependence
1737 * distances equal to zero. We take care of bounding them by 0 from below
1738 * here. add_all_proximity_constraints takes care of bounding them by 0
1739 * from above.
1741 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
1742 * Otherwise, we ignore them.
1744 static int add_all_validity_constraints(struct isl_sched_graph *graph,
1745 int use_coincidence)
1747 int i;
1749 for (i = 0; i < graph->n_edge; ++i) {
1750 struct isl_sched_edge *edge= &graph->edge[i];
1751 int local;
1753 local = edge->local || (edge->coincidence && use_coincidence);
1754 if (!edge->validity && !local)
1755 continue;
1756 if (edge->src != edge->dst)
1757 continue;
1758 if (add_intra_validity_constraints(graph, edge) < 0)
1759 return -1;
1762 for (i = 0; i < graph->n_edge; ++i) {
1763 struct isl_sched_edge *edge = &graph->edge[i];
1764 int local;
1766 local = edge->local || (edge->coincidence && use_coincidence);
1767 if (!edge->validity && !local)
1768 continue;
1769 if (edge->src == edge->dst)
1770 continue;
1771 if (add_inter_validity_constraints(graph, edge) < 0)
1772 return -1;
1775 return 0;
1778 /* Add constraints to graph->lp that bound the dependence distance
1779 * for all dependence relations.
1780 * If a given proximity dependence is identical to a validity
1781 * dependence, then the dependence distance is already bounded
1782 * from below (by zero), so we only need to bound the distance
1783 * from above. (This includes the case of "local" dependences
1784 * which are treated as validity dependence by add_all_validity_constraints.)
1785 * Otherwise, we need to bound the distance both from above and from below.
1787 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
1788 * Otherwise, we ignore them.
1790 static int add_all_proximity_constraints(struct isl_sched_graph *graph,
1791 int use_coincidence)
1793 int i;
1795 for (i = 0; i < graph->n_edge; ++i) {
1796 struct isl_sched_edge *edge= &graph->edge[i];
1797 int local;
1799 local = edge->local || (edge->coincidence && use_coincidence);
1800 if (!edge->proximity && !local)
1801 continue;
1802 if (edge->src == edge->dst &&
1803 add_intra_proximity_constraints(graph, edge, 1, local) < 0)
1804 return -1;
1805 if (edge->src != edge->dst &&
1806 add_inter_proximity_constraints(graph, edge, 1, local) < 0)
1807 return -1;
1808 if (edge->validity || local)
1809 continue;
1810 if (edge->src == edge->dst &&
1811 add_intra_proximity_constraints(graph, edge, -1, 0) < 0)
1812 return -1;
1813 if (edge->src != edge->dst &&
1814 add_inter_proximity_constraints(graph, edge, -1, 0) < 0)
1815 return -1;
1818 return 0;
1821 /* Compute a basis for the rows in the linear part of the schedule
1822 * and extend this basis to a full basis. The remaining rows
1823 * can then be used to force linear independence from the rows
1824 * in the schedule.
1826 * In particular, given the schedule rows S, we compute
1828 * S = H Q
1829 * S U = H
1831 * with H the Hermite normal form of S. That is, all but the
1832 * first rank columns of H are zero and so each row in S is
1833 * a linear combination of the first rank rows of Q.
1834 * The matrix Q is then transposed because we will write the
1835 * coefficients of the next schedule row as a column vector s
1836 * and express this s as a linear combination s = Q c of the
1837 * computed basis.
1838 * Similarly, the matrix U is transposed such that we can
1839 * compute the coefficients c = U s from a schedule row s.
1841 static int node_update_cmap(struct isl_sched_node *node)
1843 isl_mat *H, *U, *Q;
1844 int n_row = isl_mat_rows(node->sched);
1846 H = isl_mat_sub_alloc(node->sched, 0, n_row,
1847 1 + node->nparam, node->nvar);
1849 H = isl_mat_left_hermite(H, 0, &U, &Q);
1850 isl_mat_free(node->cmap);
1851 isl_mat_free(node->cinv);
1852 node->cmap = isl_mat_transpose(Q);
1853 node->cinv = isl_mat_transpose(U);
1854 node->rank = isl_mat_initial_non_zero_cols(H);
1855 isl_mat_free(H);
1857 if (!node->cmap || !node->cinv || node->rank < 0)
1858 return -1;
1859 return 0;
1862 /* How many times should we count the constraints in "edge"?
1864 * If carry is set, then we are counting the number of
1865 * (validity or conditional validity) constraints that will be added
1866 * in setup_carry_lp and we count each edge exactly once.
1868 * Otherwise, we count as follows
1869 * validity -> 1 (>= 0)
1870 * validity+proximity -> 2 (>= 0 and upper bound)
1871 * proximity -> 2 (lower and upper bound)
1872 * local(+any) -> 2 (>= 0 and <= 0)
1874 * If an edge is only marked conditional_validity then it counts
1875 * as zero since it is only checked afterwards.
1877 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
1878 * Otherwise, we ignore them.
1880 static int edge_multiplicity(struct isl_sched_edge *edge, int carry,
1881 int use_coincidence)
1883 if (carry && !edge->validity && !edge->conditional_validity)
1884 return 0;
1885 if (carry)
1886 return 1;
1887 if (edge->proximity || edge->local)
1888 return 2;
1889 if (use_coincidence && edge->coincidence)
1890 return 2;
1891 if (edge->validity)
1892 return 1;
1893 return 0;
1896 /* Count the number of equality and inequality constraints
1897 * that will be added for the given map.
1899 * "use_coincidence" is set if we should take into account coincidence edges.
1901 static int count_map_constraints(struct isl_sched_graph *graph,
1902 struct isl_sched_edge *edge, __isl_take isl_map *map,
1903 int *n_eq, int *n_ineq, int carry, int use_coincidence)
1905 isl_basic_set *coef;
1906 int f = edge_multiplicity(edge, carry, use_coincidence);
1908 if (f == 0) {
1909 isl_map_free(map);
1910 return 0;
1913 if (edge->src == edge->dst)
1914 coef = intra_coefficients(graph, edge->src, map);
1915 else
1916 coef = inter_coefficients(graph, edge, map);
1917 if (!coef)
1918 return -1;
1919 *n_eq += f * coef->n_eq;
1920 *n_ineq += f * coef->n_ineq;
1921 isl_basic_set_free(coef);
1923 return 0;
1926 /* Count the number of equality and inequality constraints
1927 * that will be added to the main lp problem.
1928 * We count as follows
1929 * validity -> 1 (>= 0)
1930 * validity+proximity -> 2 (>= 0 and upper bound)
1931 * proximity -> 2 (lower and upper bound)
1932 * local(+any) -> 2 (>= 0 and <= 0)
1934 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
1935 * Otherwise, we ignore them.
1937 static int count_constraints(struct isl_sched_graph *graph,
1938 int *n_eq, int *n_ineq, int use_coincidence)
1940 int i;
1942 *n_eq = *n_ineq = 0;
1943 for (i = 0; i < graph->n_edge; ++i) {
1944 struct isl_sched_edge *edge= &graph->edge[i];
1945 isl_map *map = isl_map_copy(edge->map);
1947 if (count_map_constraints(graph, edge, map, n_eq, n_ineq,
1948 0, use_coincidence) < 0)
1949 return -1;
1952 return 0;
1955 /* Count the number of constraints that will be added by
1956 * add_bound_coefficient_constraints and increment *n_eq and *n_ineq
1957 * accordingly.
1959 * In practice, add_bound_coefficient_constraints only adds inequalities.
1961 static int count_bound_coefficient_constraints(isl_ctx *ctx,
1962 struct isl_sched_graph *graph, int *n_eq, int *n_ineq)
1964 int i;
1966 if (ctx->opt->schedule_max_coefficient == -1)
1967 return 0;
1969 for (i = 0; i < graph->n; ++i)
1970 *n_ineq += 2 * graph->node[i].nparam + 2 * graph->node[i].nvar;
1972 return 0;
1975 /* Add constraints that bound the values of the variable and parameter
1976 * coefficients of the schedule.
1978 * The maximal value of the coefficients is defined by the option
1979 * 'schedule_max_coefficient'.
1981 static int add_bound_coefficient_constraints(isl_ctx *ctx,
1982 struct isl_sched_graph *graph)
1984 int i, j, k;
1985 int max_coefficient;
1986 int total;
1988 max_coefficient = ctx->opt->schedule_max_coefficient;
1990 if (max_coefficient == -1)
1991 return 0;
1993 total = isl_basic_set_total_dim(graph->lp);
1995 for (i = 0; i < graph->n; ++i) {
1996 struct isl_sched_node *node = &graph->node[i];
1997 for (j = 0; j < 2 * node->nparam + 2 * node->nvar; ++j) {
1998 int dim;
1999 k = isl_basic_set_alloc_inequality(graph->lp);
2000 if (k < 0)
2001 return -1;
2002 dim = 1 + node->start + 1 + j;
2003 isl_seq_clr(graph->lp->ineq[k], 1 + total);
2004 isl_int_set_si(graph->lp->ineq[k][dim], -1);
2005 isl_int_set_si(graph->lp->ineq[k][0], max_coefficient);
2009 return 0;
2012 /* Construct an ILP problem for finding schedule coefficients
2013 * that result in non-negative, but small dependence distances
2014 * over all dependences.
2015 * In particular, the dependence distances over proximity edges
2016 * are bounded by m_0 + m_n n and we compute schedule coefficients
2017 * with small values (preferably zero) of m_n and m_0.
2019 * All variables of the ILP are non-negative. The actual coefficients
2020 * may be negative, so each coefficient is represented as the difference
2021 * of two non-negative variables. The negative part always appears
2022 * immediately before the positive part.
2023 * Other than that, the variables have the following order
2025 * - sum of positive and negative parts of m_n coefficients
2026 * - m_0
2027 * - sum of positive and negative parts of all c_n coefficients
2028 * (unconstrained when computing non-parametric schedules)
2029 * - sum of positive and negative parts of all c_x coefficients
2030 * - positive and negative parts of m_n coefficients
2031 * - for each node
2032 * - c_i_0
2033 * - positive and negative parts of c_i_n (if parametric)
2034 * - positive and negative parts of c_i_x
2036 * The c_i_x are not represented directly, but through the columns of
2037 * node->cmap. That is, the computed values are for variable t_i_x
2038 * such that c_i_x = Q t_i_x with Q equal to node->cmap.
2040 * The constraints are those from the edges plus two or three equalities
2041 * to express the sums.
2043 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
2044 * Otherwise, we ignore them.
2046 static int setup_lp(isl_ctx *ctx, struct isl_sched_graph *graph,
2047 int use_coincidence)
2049 int i, j;
2050 int k;
2051 unsigned nparam;
2052 unsigned total;
2053 isl_space *dim;
2054 int parametric;
2055 int param_pos;
2056 int n_eq, n_ineq;
2057 int max_constant_term;
2059 max_constant_term = ctx->opt->schedule_max_constant_term;
2061 parametric = ctx->opt->schedule_parametric;
2062 nparam = isl_space_dim(graph->node[0].space, isl_dim_param);
2063 param_pos = 4;
2064 total = param_pos + 2 * nparam;
2065 for (i = 0; i < graph->n; ++i) {
2066 struct isl_sched_node *node = &graph->node[graph->sorted[i]];
2067 if (node_update_cmap(node) < 0)
2068 return -1;
2069 node->start = total;
2070 total += 1 + 2 * (node->nparam + node->nvar);
2073 if (count_constraints(graph, &n_eq, &n_ineq, use_coincidence) < 0)
2074 return -1;
2075 if (count_bound_coefficient_constraints(ctx, graph, &n_eq, &n_ineq) < 0)
2076 return -1;
2078 dim = isl_space_set_alloc(ctx, 0, total);
2079 isl_basic_set_free(graph->lp);
2080 n_eq += 2 + parametric;
2081 if (max_constant_term != -1)
2082 n_ineq += graph->n;
2084 graph->lp = isl_basic_set_alloc_space(dim, 0, n_eq, n_ineq);
2086 k = isl_basic_set_alloc_equality(graph->lp);
2087 if (k < 0)
2088 return -1;
2089 isl_seq_clr(graph->lp->eq[k], 1 + total);
2090 isl_int_set_si(graph->lp->eq[k][1], -1);
2091 for (i = 0; i < 2 * nparam; ++i)
2092 isl_int_set_si(graph->lp->eq[k][1 + param_pos + i], 1);
2094 if (parametric) {
2095 k = isl_basic_set_alloc_equality(graph->lp);
2096 if (k < 0)
2097 return -1;
2098 isl_seq_clr(graph->lp->eq[k], 1 + total);
2099 isl_int_set_si(graph->lp->eq[k][3], -1);
2100 for (i = 0; i < graph->n; ++i) {
2101 int pos = 1 + graph->node[i].start + 1;
2103 for (j = 0; j < 2 * graph->node[i].nparam; ++j)
2104 isl_int_set_si(graph->lp->eq[k][pos + j], 1);
2108 k = isl_basic_set_alloc_equality(graph->lp);
2109 if (k < 0)
2110 return -1;
2111 isl_seq_clr(graph->lp->eq[k], 1 + total);
2112 isl_int_set_si(graph->lp->eq[k][4], -1);
2113 for (i = 0; i < graph->n; ++i) {
2114 struct isl_sched_node *node = &graph->node[i];
2115 int pos = 1 + node->start + 1 + 2 * node->nparam;
2117 for (j = 0; j < 2 * node->nvar; ++j)
2118 isl_int_set_si(graph->lp->eq[k][pos + j], 1);
2121 if (max_constant_term != -1)
2122 for (i = 0; i < graph->n; ++i) {
2123 struct isl_sched_node *node = &graph->node[i];
2124 k = isl_basic_set_alloc_inequality(graph->lp);
2125 if (k < 0)
2126 return -1;
2127 isl_seq_clr(graph->lp->ineq[k], 1 + total);
2128 isl_int_set_si(graph->lp->ineq[k][1 + node->start], -1);
2129 isl_int_set_si(graph->lp->ineq[k][0], max_constant_term);
2132 if (add_bound_coefficient_constraints(ctx, graph) < 0)
2133 return -1;
2134 if (add_all_validity_constraints(graph, use_coincidence) < 0)
2135 return -1;
2136 if (add_all_proximity_constraints(graph, use_coincidence) < 0)
2137 return -1;
2139 return 0;
2142 /* Analyze the conflicting constraint found by
2143 * isl_tab_basic_set_non_trivial_lexmin. If it corresponds to the validity
2144 * constraint of one of the edges between distinct nodes, living, moreover
2145 * in distinct SCCs, then record the source and sink SCC as this may
2146 * be a good place to cut between SCCs.
2148 static int check_conflict(int con, void *user)
2150 int i;
2151 struct isl_sched_graph *graph = user;
2153 if (graph->src_scc >= 0)
2154 return 0;
2156 con -= graph->lp->n_eq;
2158 if (con >= graph->lp->n_ineq)
2159 return 0;
2161 for (i = 0; i < graph->n_edge; ++i) {
2162 if (!graph->edge[i].validity)
2163 continue;
2164 if (graph->edge[i].src == graph->edge[i].dst)
2165 continue;
2166 if (graph->edge[i].src->scc == graph->edge[i].dst->scc)
2167 continue;
2168 if (graph->edge[i].start > con)
2169 continue;
2170 if (graph->edge[i].end <= con)
2171 continue;
2172 graph->src_scc = graph->edge[i].src->scc;
2173 graph->dst_scc = graph->edge[i].dst->scc;
2176 return 0;
2179 /* Check whether the next schedule row of the given node needs to be
2180 * non-trivial. Lower-dimensional domains may have some trivial rows,
2181 * but as soon as the number of remaining required non-trivial rows
2182 * is as large as the number or remaining rows to be computed,
2183 * all remaining rows need to be non-trivial.
2185 static int needs_row(struct isl_sched_graph *graph, struct isl_sched_node *node)
2187 return node->nvar - node->rank >= graph->maxvar - graph->n_row;
2190 /* Solve the ILP problem constructed in setup_lp.
2191 * For each node such that all the remaining rows of its schedule
2192 * need to be non-trivial, we construct a non-triviality region.
2193 * This region imposes that the next row is independent of previous rows.
2194 * In particular the coefficients c_i_x are represented by t_i_x
2195 * variables with c_i_x = Q t_i_x and Q a unimodular matrix such that
2196 * its first columns span the rows of the previously computed part
2197 * of the schedule. The non-triviality region enforces that at least
2198 * one of the remaining components of t_i_x is non-zero, i.e.,
2199 * that the new schedule row depends on at least one of the remaining
2200 * columns of Q.
2202 static __isl_give isl_vec *solve_lp(struct isl_sched_graph *graph)
2204 int i;
2205 isl_vec *sol;
2206 isl_basic_set *lp;
2208 for (i = 0; i < graph->n; ++i) {
2209 struct isl_sched_node *node = &graph->node[i];
2210 int skip = node->rank;
2211 graph->region[i].pos = node->start + 1 + 2*(node->nparam+skip);
2212 if (needs_row(graph, node))
2213 graph->region[i].len = 2 * (node->nvar - skip);
2214 else
2215 graph->region[i].len = 0;
2217 lp = isl_basic_set_copy(graph->lp);
2218 sol = isl_tab_basic_set_non_trivial_lexmin(lp, 2, graph->n,
2219 graph->region, &check_conflict, graph);
2220 return sol;
2223 /* Update the schedules of all nodes based on the given solution
2224 * of the LP problem.
2225 * The new row is added to the current band.
2226 * All possibly negative coefficients are encoded as a difference
2227 * of two non-negative variables, so we need to perform the subtraction
2228 * here. Moreover, if use_cmap is set, then the solution does
2229 * not refer to the actual coefficients c_i_x, but instead to variables
2230 * t_i_x such that c_i_x = Q t_i_x and Q is equal to node->cmap.
2231 * In this case, we then also need to perform this multiplication
2232 * to obtain the values of c_i_x.
2234 * If coincident is set, then the caller guarantees that the new
2235 * row satisfies the coincidence constraints.
2237 static int update_schedule(struct isl_sched_graph *graph,
2238 __isl_take isl_vec *sol, int use_cmap, int coincident)
2240 int i, j;
2241 isl_vec *csol = NULL;
2243 if (!sol)
2244 goto error;
2245 if (sol->size == 0)
2246 isl_die(sol->ctx, isl_error_internal,
2247 "no solution found", goto error);
2248 if (graph->n_total_row >= graph->max_row)
2249 isl_die(sol->ctx, isl_error_internal,
2250 "too many schedule rows", goto error);
2252 for (i = 0; i < graph->n; ++i) {
2253 struct isl_sched_node *node = &graph->node[i];
2254 int pos = node->start;
2255 int row = isl_mat_rows(node->sched);
2257 isl_vec_free(csol);
2258 csol = isl_vec_alloc(sol->ctx, node->nvar);
2259 if (!csol)
2260 goto error;
2262 isl_map_free(node->sched_map);
2263 node->sched_map = NULL;
2264 node->sched = isl_mat_add_rows(node->sched, 1);
2265 if (!node->sched)
2266 goto error;
2267 node->sched = isl_mat_set_element(node->sched, row, 0,
2268 sol->el[1 + pos]);
2269 for (j = 0; j < node->nparam + node->nvar; ++j)
2270 isl_int_sub(sol->el[1 + pos + 1 + 2 * j + 1],
2271 sol->el[1 + pos + 1 + 2 * j + 1],
2272 sol->el[1 + pos + 1 + 2 * j]);
2273 for (j = 0; j < node->nparam; ++j)
2274 node->sched = isl_mat_set_element(node->sched,
2275 row, 1 + j, sol->el[1+pos+1+2*j+1]);
2276 for (j = 0; j < node->nvar; ++j)
2277 isl_int_set(csol->el[j],
2278 sol->el[1+pos+1+2*(node->nparam+j)+1]);
2279 if (use_cmap)
2280 csol = isl_mat_vec_product(isl_mat_copy(node->cmap),
2281 csol);
2282 if (!csol)
2283 goto error;
2284 for (j = 0; j < node->nvar; ++j)
2285 node->sched = isl_mat_set_element(node->sched,
2286 row, 1 + node->nparam + j, csol->el[j]);
2287 node->band[graph->n_total_row] = graph->n_band;
2288 node->coincident[graph->n_total_row] = coincident;
2290 isl_vec_free(sol);
2291 isl_vec_free(csol);
2293 graph->n_row++;
2294 graph->n_total_row++;
2296 return 0;
2297 error:
2298 isl_vec_free(sol);
2299 isl_vec_free(csol);
2300 return -1;
2303 /* Convert row "row" of node->sched into an isl_aff living in "ls"
2304 * and return this isl_aff.
2306 static __isl_give isl_aff *extract_schedule_row(__isl_take isl_local_space *ls,
2307 struct isl_sched_node *node, int row)
2309 int j;
2310 isl_int v;
2311 isl_aff *aff;
2313 isl_int_init(v);
2315 aff = isl_aff_zero_on_domain(ls);
2316 isl_mat_get_element(node->sched, row, 0, &v);
2317 aff = isl_aff_set_constant(aff, v);
2318 for (j = 0; j < node->nparam; ++j) {
2319 isl_mat_get_element(node->sched, row, 1 + j, &v);
2320 aff = isl_aff_set_coefficient(aff, isl_dim_param, j, v);
2322 for (j = 0; j < node->nvar; ++j) {
2323 isl_mat_get_element(node->sched, row, 1 + node->nparam + j, &v);
2324 aff = isl_aff_set_coefficient(aff, isl_dim_in, j, v);
2327 isl_int_clear(v);
2329 return aff;
2332 /* Convert node->sched into a multi_aff and return this multi_aff.
2334 * The result is defined over the uncompressed node domain.
2336 static __isl_give isl_multi_aff *node_extract_schedule_multi_aff(
2337 struct isl_sched_node *node)
2339 int i;
2340 isl_space *space;
2341 isl_local_space *ls;
2342 isl_aff *aff;
2343 isl_multi_aff *ma;
2344 int nrow, ncol;
2346 nrow = isl_mat_rows(node->sched);
2347 ncol = isl_mat_cols(node->sched) - 1;
2348 if (node->compressed)
2349 space = isl_multi_aff_get_domain_space(node->decompress);
2350 else
2351 space = isl_space_copy(node->space);
2352 ls = isl_local_space_from_space(isl_space_copy(space));
2353 space = isl_space_from_domain(space);
2354 space = isl_space_add_dims(space, isl_dim_out, nrow);
2355 ma = isl_multi_aff_zero(space);
2357 for (i = 0; i < nrow; ++i) {
2358 aff = extract_schedule_row(isl_local_space_copy(ls), node, i);
2359 ma = isl_multi_aff_set_aff(ma, i, aff);
2362 isl_local_space_free(ls);
2364 if (node->compressed)
2365 ma = isl_multi_aff_pullback_multi_aff(ma,
2366 isl_multi_aff_copy(node->compress));
2368 return ma;
2371 /* Convert node->sched into a map and return this map.
2373 * The result is cached in node->sched_map, which needs to be released
2374 * whenever node->sched is updated.
2375 * It is defined over the uncompressed node domain.
2377 static __isl_give isl_map *node_extract_schedule(struct isl_sched_node *node)
2379 if (!node->sched_map) {
2380 isl_multi_aff *ma;
2382 ma = node_extract_schedule_multi_aff(node);
2383 node->sched_map = isl_map_from_multi_aff(ma);
2386 return isl_map_copy(node->sched_map);
2389 /* Construct a map that can be used to update a dependence relation
2390 * based on the current schedule.
2391 * That is, construct a map expressing that source and sink
2392 * are executed within the same iteration of the current schedule.
2393 * This map can then be intersected with the dependence relation.
2394 * This is not the most efficient way, but this shouldn't be a critical
2395 * operation.
2397 static __isl_give isl_map *specializer(struct isl_sched_node *src,
2398 struct isl_sched_node *dst)
2400 isl_map *src_sched, *dst_sched;
2402 src_sched = node_extract_schedule(src);
2403 dst_sched = node_extract_schedule(dst);
2404 return isl_map_apply_range(src_sched, isl_map_reverse(dst_sched));
2407 /* Intersect the domains of the nested relations in domain and range
2408 * of "umap" with "map".
2410 static __isl_give isl_union_map *intersect_domains(
2411 __isl_take isl_union_map *umap, __isl_keep isl_map *map)
2413 isl_union_set *uset;
2415 umap = isl_union_map_zip(umap);
2416 uset = isl_union_set_from_set(isl_map_wrap(isl_map_copy(map)));
2417 umap = isl_union_map_intersect_domain(umap, uset);
2418 umap = isl_union_map_zip(umap);
2419 return umap;
2422 /* Update the dependence relation of the given edge based
2423 * on the current schedule.
2424 * If the dependence is carried completely by the current schedule, then
2425 * it is removed from the edge_tables. It is kept in the list of edges
2426 * as otherwise all edge_tables would have to be recomputed.
2428 static int update_edge(struct isl_sched_graph *graph,
2429 struct isl_sched_edge *edge)
2431 isl_map *id;
2433 id = specializer(edge->src, edge->dst);
2434 edge->map = isl_map_intersect(edge->map, isl_map_copy(id));
2435 if (!edge->map)
2436 goto error;
2438 if (edge->tagged_condition) {
2439 edge->tagged_condition =
2440 intersect_domains(edge->tagged_condition, id);
2441 if (!edge->tagged_condition)
2442 goto error;
2444 if (edge->tagged_validity) {
2445 edge->tagged_validity =
2446 intersect_domains(edge->tagged_validity, id);
2447 if (!edge->tagged_validity)
2448 goto error;
2451 isl_map_free(id);
2452 if (isl_map_plain_is_empty(edge->map))
2453 graph_remove_edge(graph, edge);
2455 return 0;
2456 error:
2457 isl_map_free(id);
2458 return -1;
2461 /* Update the dependence relations of all edges based on the current schedule.
2463 static int update_edges(isl_ctx *ctx, struct isl_sched_graph *graph)
2465 int i;
2467 for (i = graph->n_edge - 1; i >= 0; --i) {
2468 if (update_edge(graph, &graph->edge[i]) < 0)
2469 return -1;
2472 return 0;
2475 static void next_band(struct isl_sched_graph *graph)
2477 graph->band_start = graph->n_total_row;
2478 graph->n_band++;
2481 /* Topologically sort statements mapped to the same schedule iteration
2482 * and add a row to the schedule corresponding to this order.
2484 static int sort_statements(isl_ctx *ctx, struct isl_sched_graph *graph)
2486 int i, j;
2488 if (graph->n <= 1)
2489 return 0;
2491 if (update_edges(ctx, graph) < 0)
2492 return -1;
2494 if (graph->n_edge == 0)
2495 return 0;
2497 if (detect_sccs(ctx, graph) < 0)
2498 return -1;
2500 if (graph->n_total_row >= graph->max_row)
2501 isl_die(ctx, isl_error_internal,
2502 "too many schedule rows", return -1);
2504 for (i = 0; i < graph->n; ++i) {
2505 struct isl_sched_node *node = &graph->node[i];
2506 int row = isl_mat_rows(node->sched);
2507 int cols = isl_mat_cols(node->sched);
2509 isl_map_free(node->sched_map);
2510 node->sched_map = NULL;
2511 node->sched = isl_mat_add_rows(node->sched, 1);
2512 if (!node->sched)
2513 return -1;
2514 node->sched = isl_mat_set_element_si(node->sched, row, 0,
2515 node->scc);
2516 for (j = 1; j < cols; ++j)
2517 node->sched = isl_mat_set_element_si(node->sched,
2518 row, j, 0);
2519 node->band[graph->n_total_row] = graph->n_band;
2522 graph->n_total_row++;
2523 next_band(graph);
2525 return 0;
2528 /* Construct an isl_schedule based on the computed schedule stored
2529 * in graph and with parameters specified by dim.
2531 static __isl_give isl_schedule *extract_schedule(struct isl_sched_graph *graph,
2532 __isl_take isl_space *dim)
2534 int i;
2535 isl_ctx *ctx;
2536 isl_schedule *sched = NULL;
2538 if (!dim)
2539 return NULL;
2541 ctx = isl_space_get_ctx(dim);
2542 sched = isl_calloc(ctx, struct isl_schedule,
2543 sizeof(struct isl_schedule) +
2544 (graph->n - 1) *
2545 sizeof(struct isl_schedule_domain_node));
2546 if (!sched)
2547 goto error;
2549 sched->ref = 1;
2550 sched->leaf.ctx = ctx;
2551 isl_ctx_ref(ctx);
2552 sched->n = graph->n;
2553 sched->n_band = graph->n_band;
2554 sched->n_total_row = graph->n_total_row;
2556 for (i = 0; i < sched->n; ++i) {
2557 int r, b;
2558 int *band_end, *band_id, *coincident;
2560 sched->node[i].sched =
2561 node_extract_schedule_multi_aff(&graph->node[i]);
2562 if (!sched->node[i].sched)
2563 goto error;
2565 sched->node[i].n_band = graph->n_band;
2566 if (graph->n_band == 0)
2567 continue;
2569 band_end = isl_alloc_array(ctx, int, graph->n_band);
2570 band_id = isl_alloc_array(ctx, int, graph->n_band);
2571 coincident = isl_alloc_array(ctx, int, graph->n_total_row);
2572 sched->node[i].band_end = band_end;
2573 sched->node[i].band_id = band_id;
2574 sched->node[i].coincident = coincident;
2575 if (!band_end || !band_id || !coincident)
2576 goto error;
2578 for (r = 0; r < graph->n_total_row; ++r)
2579 coincident[r] = graph->node[i].coincident[r];
2580 for (r = b = 0; r < graph->n_total_row; ++r) {
2581 if (graph->node[i].band[r] == b)
2582 continue;
2583 band_end[b++] = r;
2584 if (graph->node[i].band[r] == -1)
2585 break;
2587 if (r == graph->n_total_row)
2588 band_end[b++] = r;
2589 sched->node[i].n_band = b;
2590 for (--b; b >= 0; --b)
2591 band_id[b] = graph->node[i].band_id[b];
2594 sched->dim = dim;
2596 return sched;
2597 error:
2598 isl_space_free(dim);
2599 isl_schedule_free(sched);
2600 return NULL;
2603 /* Copy nodes that satisfy node_pred from the src dependence graph
2604 * to the dst dependence graph.
2606 static int copy_nodes(struct isl_sched_graph *dst, struct isl_sched_graph *src,
2607 int (*node_pred)(struct isl_sched_node *node, int data), int data)
2609 int i;
2611 dst->n = 0;
2612 for (i = 0; i < src->n; ++i) {
2613 int j;
2615 if (!node_pred(&src->node[i], data))
2616 continue;
2618 j = dst->n;
2619 dst->node[j].space = isl_space_copy(src->node[i].space);
2620 dst->node[j].compressed = src->node[i].compressed;
2621 dst->node[j].hull = isl_set_copy(src->node[i].hull);
2622 dst->node[j].compress =
2623 isl_multi_aff_copy(src->node[i].compress);
2624 dst->node[j].decompress =
2625 isl_multi_aff_copy(src->node[i].decompress);
2626 dst->node[j].nvar = src->node[i].nvar;
2627 dst->node[j].nparam = src->node[i].nparam;
2628 dst->node[j].sched = isl_mat_copy(src->node[i].sched);
2629 dst->node[j].sched_map = isl_map_copy(src->node[i].sched_map);
2630 dst->node[j].band = src->node[i].band;
2631 dst->node[j].band_id = src->node[i].band_id;
2632 dst->node[j].coincident = src->node[i].coincident;
2633 dst->n++;
2635 if (!dst->node[j].space || !dst->node[j].sched)
2636 return -1;
2637 if (dst->node[j].compressed &&
2638 (!dst->node[j].hull || !dst->node[j].compress ||
2639 !dst->node[j].decompress))
2640 return -1;
2643 return 0;
2646 /* Copy non-empty edges that satisfy edge_pred from the src dependence graph
2647 * to the dst dependence graph.
2648 * If the source or destination node of the edge is not in the destination
2649 * graph, then it must be a backward proximity edge and it should simply
2650 * be ignored.
2652 static int copy_edges(isl_ctx *ctx, struct isl_sched_graph *dst,
2653 struct isl_sched_graph *src,
2654 int (*edge_pred)(struct isl_sched_edge *edge, int data), int data)
2656 int i;
2657 enum isl_edge_type t;
2659 dst->n_edge = 0;
2660 for (i = 0; i < src->n_edge; ++i) {
2661 struct isl_sched_edge *edge = &src->edge[i];
2662 isl_map *map;
2663 isl_union_map *tagged_condition;
2664 isl_union_map *tagged_validity;
2665 struct isl_sched_node *dst_src, *dst_dst;
2667 if (!edge_pred(edge, data))
2668 continue;
2670 if (isl_map_plain_is_empty(edge->map))
2671 continue;
2673 dst_src = graph_find_node(ctx, dst, edge->src->space);
2674 dst_dst = graph_find_node(ctx, dst, edge->dst->space);
2675 if (!dst_src || !dst_dst) {
2676 if (edge->validity || edge->conditional_validity)
2677 isl_die(ctx, isl_error_internal,
2678 "backward (conditional) validity edge",
2679 return -1);
2680 continue;
2683 map = isl_map_copy(edge->map);
2684 tagged_condition = isl_union_map_copy(edge->tagged_condition);
2685 tagged_validity = isl_union_map_copy(edge->tagged_validity);
2687 dst->edge[dst->n_edge].src = dst_src;
2688 dst->edge[dst->n_edge].dst = dst_dst;
2689 dst->edge[dst->n_edge].map = map;
2690 dst->edge[dst->n_edge].tagged_condition = tagged_condition;
2691 dst->edge[dst->n_edge].tagged_validity = tagged_validity;
2692 dst->edge[dst->n_edge].validity = edge->validity;
2693 dst->edge[dst->n_edge].proximity = edge->proximity;
2694 dst->edge[dst->n_edge].coincidence = edge->coincidence;
2695 dst->edge[dst->n_edge].condition = edge->condition;
2696 dst->edge[dst->n_edge].conditional_validity =
2697 edge->conditional_validity;
2698 dst->n_edge++;
2700 if (edge->tagged_condition && !tagged_condition)
2701 return -1;
2702 if (edge->tagged_validity && !tagged_validity)
2703 return -1;
2705 for (t = isl_edge_first; t <= isl_edge_last; ++t) {
2706 if (edge !=
2707 graph_find_edge(src, t, edge->src, edge->dst))
2708 continue;
2709 if (graph_edge_table_add(ctx, dst, t,
2710 &dst->edge[dst->n_edge - 1]) < 0)
2711 return -1;
2715 return 0;
2718 /* Given a "src" dependence graph that contains the nodes from "dst"
2719 * that satisfy node_pred, copy the schedule computed in "src"
2720 * for those nodes back to "dst".
2722 static int copy_schedule(struct isl_sched_graph *dst,
2723 struct isl_sched_graph *src,
2724 int (*node_pred)(struct isl_sched_node *node, int data), int data)
2726 int i;
2728 src->n = 0;
2729 for (i = 0; i < dst->n; ++i) {
2730 if (!node_pred(&dst->node[i], data))
2731 continue;
2732 isl_mat_free(dst->node[i].sched);
2733 isl_map_free(dst->node[i].sched_map);
2734 dst->node[i].sched = isl_mat_copy(src->node[src->n].sched);
2735 dst->node[i].sched_map =
2736 isl_map_copy(src->node[src->n].sched_map);
2737 src->n++;
2740 dst->max_row = src->max_row;
2741 dst->n_total_row = src->n_total_row;
2742 dst->n_band = src->n_band;
2744 return 0;
2747 /* Compute the maximal number of variables over all nodes.
2748 * This is the maximal number of linearly independent schedule
2749 * rows that we need to compute.
2750 * Just in case we end up in a part of the dependence graph
2751 * with only lower-dimensional domains, we make sure we will
2752 * compute the required amount of extra linearly independent rows.
2754 static int compute_maxvar(struct isl_sched_graph *graph)
2756 int i;
2758 graph->maxvar = 0;
2759 for (i = 0; i < graph->n; ++i) {
2760 struct isl_sched_node *node = &graph->node[i];
2761 int nvar;
2763 if (node_update_cmap(node) < 0)
2764 return -1;
2765 nvar = node->nvar + graph->n_row - node->rank;
2766 if (nvar > graph->maxvar)
2767 graph->maxvar = nvar;
2770 return 0;
2773 static int compute_schedule(isl_ctx *ctx, struct isl_sched_graph *graph);
2774 static int compute_schedule_wcc(isl_ctx *ctx, struct isl_sched_graph *graph);
2776 /* Compute a schedule for a subgraph of "graph". In particular, for
2777 * the graph composed of nodes that satisfy node_pred and edges that
2778 * that satisfy edge_pred. The caller should precompute the number
2779 * of nodes and edges that satisfy these predicates and pass them along
2780 * as "n" and "n_edge".
2781 * If the subgraph is known to consist of a single component, then wcc should
2782 * be set and then we call compute_schedule_wcc on the constructed subgraph.
2783 * Otherwise, we call compute_schedule, which will check whether the subgraph
2784 * is connected.
2786 static int compute_sub_schedule(isl_ctx *ctx,
2787 struct isl_sched_graph *graph, int n, int n_edge,
2788 int (*node_pred)(struct isl_sched_node *node, int data),
2789 int (*edge_pred)(struct isl_sched_edge *edge, int data),
2790 int data, int wcc)
2792 struct isl_sched_graph split = { 0 };
2793 int t;
2795 if (graph_alloc(ctx, &split, n, n_edge) < 0)
2796 goto error;
2797 if (copy_nodes(&split, graph, node_pred, data) < 0)
2798 goto error;
2799 if (graph_init_table(ctx, &split) < 0)
2800 goto error;
2801 for (t = 0; t <= isl_edge_last; ++t)
2802 split.max_edge[t] = graph->max_edge[t];
2803 if (graph_init_edge_tables(ctx, &split) < 0)
2804 goto error;
2805 if (copy_edges(ctx, &split, graph, edge_pred, data) < 0)
2806 goto error;
2807 split.n_row = graph->n_row;
2808 split.max_row = graph->max_row;
2809 split.n_total_row = graph->n_total_row;
2810 split.n_band = graph->n_band;
2811 split.band_start = graph->band_start;
2813 if (wcc && compute_schedule_wcc(ctx, &split) < 0)
2814 goto error;
2815 if (!wcc && compute_schedule(ctx, &split) < 0)
2816 goto error;
2818 copy_schedule(graph, &split, node_pred, data);
2820 graph_free(ctx, &split);
2821 return 0;
2822 error:
2823 graph_free(ctx, &split);
2824 return -1;
2827 static int node_scc_exactly(struct isl_sched_node *node, int scc)
2829 return node->scc == scc;
2832 static int node_scc_at_most(struct isl_sched_node *node, int scc)
2834 return node->scc <= scc;
2837 static int node_scc_at_least(struct isl_sched_node *node, int scc)
2839 return node->scc >= scc;
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);