doc: document isl_schedule_get_ctx
[isl.git] / isl_scheduler.c
blob30aacc9c02cb2557693b62b544e8121458e1eae5
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) * sizeof(struct isl_schedule_node));
2545 if (!sched)
2546 goto error;
2548 sched->ref = 1;
2549 sched->n = graph->n;
2550 sched->n_band = graph->n_band;
2551 sched->n_total_row = graph->n_total_row;
2553 for (i = 0; i < sched->n; ++i) {
2554 int r, b;
2555 int *band_end, *band_id, *coincident;
2557 sched->node[i].sched =
2558 node_extract_schedule_multi_aff(&graph->node[i]);
2559 if (!sched->node[i].sched)
2560 goto error;
2562 sched->node[i].n_band = graph->n_band;
2563 if (graph->n_band == 0)
2564 continue;
2566 band_end = isl_alloc_array(ctx, int, graph->n_band);
2567 band_id = isl_alloc_array(ctx, int, graph->n_band);
2568 coincident = isl_alloc_array(ctx, int, graph->n_total_row);
2569 sched->node[i].band_end = band_end;
2570 sched->node[i].band_id = band_id;
2571 sched->node[i].coincident = coincident;
2572 if (!band_end || !band_id || !coincident)
2573 goto error;
2575 for (r = 0; r < graph->n_total_row; ++r)
2576 coincident[r] = graph->node[i].coincident[r];
2577 for (r = b = 0; r < graph->n_total_row; ++r) {
2578 if (graph->node[i].band[r] == b)
2579 continue;
2580 band_end[b++] = r;
2581 if (graph->node[i].band[r] == -1)
2582 break;
2584 if (r == graph->n_total_row)
2585 band_end[b++] = r;
2586 sched->node[i].n_band = b;
2587 for (--b; b >= 0; --b)
2588 band_id[b] = graph->node[i].band_id[b];
2591 sched->dim = dim;
2593 return sched;
2594 error:
2595 isl_space_free(dim);
2596 isl_schedule_free(sched);
2597 return NULL;
2600 /* Copy nodes that satisfy node_pred from the src dependence graph
2601 * to the dst dependence graph.
2603 static int copy_nodes(struct isl_sched_graph *dst, struct isl_sched_graph *src,
2604 int (*node_pred)(struct isl_sched_node *node, int data), int data)
2606 int i;
2608 dst->n = 0;
2609 for (i = 0; i < src->n; ++i) {
2610 int j;
2612 if (!node_pred(&src->node[i], data))
2613 continue;
2615 j = dst->n;
2616 dst->node[j].space = isl_space_copy(src->node[i].space);
2617 dst->node[j].compressed = src->node[i].compressed;
2618 dst->node[j].hull = isl_set_copy(src->node[i].hull);
2619 dst->node[j].compress =
2620 isl_multi_aff_copy(src->node[i].compress);
2621 dst->node[j].decompress =
2622 isl_multi_aff_copy(src->node[i].decompress);
2623 dst->node[j].nvar = src->node[i].nvar;
2624 dst->node[j].nparam = src->node[i].nparam;
2625 dst->node[j].sched = isl_mat_copy(src->node[i].sched);
2626 dst->node[j].sched_map = isl_map_copy(src->node[i].sched_map);
2627 dst->node[j].band = src->node[i].band;
2628 dst->node[j].band_id = src->node[i].band_id;
2629 dst->node[j].coincident = src->node[i].coincident;
2630 dst->n++;
2632 if (!dst->node[j].space || !dst->node[j].sched)
2633 return -1;
2634 if (dst->node[j].compressed &&
2635 (!dst->node[j].hull || !dst->node[j].compress ||
2636 !dst->node[j].decompress))
2637 return -1;
2640 return 0;
2643 /* Copy non-empty edges that satisfy edge_pred from the src dependence graph
2644 * to the dst dependence graph.
2645 * If the source or destination node of the edge is not in the destination
2646 * graph, then it must be a backward proximity edge and it should simply
2647 * be ignored.
2649 static int copy_edges(isl_ctx *ctx, struct isl_sched_graph *dst,
2650 struct isl_sched_graph *src,
2651 int (*edge_pred)(struct isl_sched_edge *edge, int data), int data)
2653 int i;
2654 enum isl_edge_type t;
2656 dst->n_edge = 0;
2657 for (i = 0; i < src->n_edge; ++i) {
2658 struct isl_sched_edge *edge = &src->edge[i];
2659 isl_map *map;
2660 isl_union_map *tagged_condition;
2661 isl_union_map *tagged_validity;
2662 struct isl_sched_node *dst_src, *dst_dst;
2664 if (!edge_pred(edge, data))
2665 continue;
2667 if (isl_map_plain_is_empty(edge->map))
2668 continue;
2670 dst_src = graph_find_node(ctx, dst, edge->src->space);
2671 dst_dst = graph_find_node(ctx, dst, edge->dst->space);
2672 if (!dst_src || !dst_dst) {
2673 if (edge->validity || edge->conditional_validity)
2674 isl_die(ctx, isl_error_internal,
2675 "backward (conditional) validity edge",
2676 return -1);
2677 continue;
2680 map = isl_map_copy(edge->map);
2681 tagged_condition = isl_union_map_copy(edge->tagged_condition);
2682 tagged_validity = isl_union_map_copy(edge->tagged_validity);
2684 dst->edge[dst->n_edge].src = dst_src;
2685 dst->edge[dst->n_edge].dst = dst_dst;
2686 dst->edge[dst->n_edge].map = map;
2687 dst->edge[dst->n_edge].tagged_condition = tagged_condition;
2688 dst->edge[dst->n_edge].tagged_validity = tagged_validity;
2689 dst->edge[dst->n_edge].validity = edge->validity;
2690 dst->edge[dst->n_edge].proximity = edge->proximity;
2691 dst->edge[dst->n_edge].coincidence = edge->coincidence;
2692 dst->edge[dst->n_edge].condition = edge->condition;
2693 dst->edge[dst->n_edge].conditional_validity =
2694 edge->conditional_validity;
2695 dst->n_edge++;
2697 if (edge->tagged_condition && !tagged_condition)
2698 return -1;
2699 if (edge->tagged_validity && !tagged_validity)
2700 return -1;
2702 for (t = isl_edge_first; t <= isl_edge_last; ++t) {
2703 if (edge !=
2704 graph_find_edge(src, t, edge->src, edge->dst))
2705 continue;
2706 if (graph_edge_table_add(ctx, dst, t,
2707 &dst->edge[dst->n_edge - 1]) < 0)
2708 return -1;
2712 return 0;
2715 /* Given a "src" dependence graph that contains the nodes from "dst"
2716 * that satisfy node_pred, copy the schedule computed in "src"
2717 * for those nodes back to "dst".
2719 static int copy_schedule(struct isl_sched_graph *dst,
2720 struct isl_sched_graph *src,
2721 int (*node_pred)(struct isl_sched_node *node, int data), int data)
2723 int i;
2725 src->n = 0;
2726 for (i = 0; i < dst->n; ++i) {
2727 if (!node_pred(&dst->node[i], data))
2728 continue;
2729 isl_mat_free(dst->node[i].sched);
2730 isl_map_free(dst->node[i].sched_map);
2731 dst->node[i].sched = isl_mat_copy(src->node[src->n].sched);
2732 dst->node[i].sched_map =
2733 isl_map_copy(src->node[src->n].sched_map);
2734 src->n++;
2737 dst->max_row = src->max_row;
2738 dst->n_total_row = src->n_total_row;
2739 dst->n_band = src->n_band;
2741 return 0;
2744 /* Compute the maximal number of variables over all nodes.
2745 * This is the maximal number of linearly independent schedule
2746 * rows that we need to compute.
2747 * Just in case we end up in a part of the dependence graph
2748 * with only lower-dimensional domains, we make sure we will
2749 * compute the required amount of extra linearly independent rows.
2751 static int compute_maxvar(struct isl_sched_graph *graph)
2753 int i;
2755 graph->maxvar = 0;
2756 for (i = 0; i < graph->n; ++i) {
2757 struct isl_sched_node *node = &graph->node[i];
2758 int nvar;
2760 if (node_update_cmap(node) < 0)
2761 return -1;
2762 nvar = node->nvar + graph->n_row - node->rank;
2763 if (nvar > graph->maxvar)
2764 graph->maxvar = nvar;
2767 return 0;
2770 static int compute_schedule(isl_ctx *ctx, struct isl_sched_graph *graph);
2771 static int compute_schedule_wcc(isl_ctx *ctx, struct isl_sched_graph *graph);
2773 /* Compute a schedule for a subgraph of "graph". In particular, for
2774 * the graph composed of nodes that satisfy node_pred and edges that
2775 * that satisfy edge_pred. The caller should precompute the number
2776 * of nodes and edges that satisfy these predicates and pass them along
2777 * as "n" and "n_edge".
2778 * If the subgraph is known to consist of a single component, then wcc should
2779 * be set and then we call compute_schedule_wcc on the constructed subgraph.
2780 * Otherwise, we call compute_schedule, which will check whether the subgraph
2781 * is connected.
2783 static int compute_sub_schedule(isl_ctx *ctx,
2784 struct isl_sched_graph *graph, int n, int n_edge,
2785 int (*node_pred)(struct isl_sched_node *node, int data),
2786 int (*edge_pred)(struct isl_sched_edge *edge, int data),
2787 int data, int wcc)
2789 struct isl_sched_graph split = { 0 };
2790 int t;
2792 if (graph_alloc(ctx, &split, n, n_edge) < 0)
2793 goto error;
2794 if (copy_nodes(&split, graph, node_pred, data) < 0)
2795 goto error;
2796 if (graph_init_table(ctx, &split) < 0)
2797 goto error;
2798 for (t = 0; t <= isl_edge_last; ++t)
2799 split.max_edge[t] = graph->max_edge[t];
2800 if (graph_init_edge_tables(ctx, &split) < 0)
2801 goto error;
2802 if (copy_edges(ctx, &split, graph, edge_pred, data) < 0)
2803 goto error;
2804 split.n_row = graph->n_row;
2805 split.max_row = graph->max_row;
2806 split.n_total_row = graph->n_total_row;
2807 split.n_band = graph->n_band;
2808 split.band_start = graph->band_start;
2810 if (wcc && compute_schedule_wcc(ctx, &split) < 0)
2811 goto error;
2812 if (!wcc && compute_schedule(ctx, &split) < 0)
2813 goto error;
2815 copy_schedule(graph, &split, node_pred, data);
2817 graph_free(ctx, &split);
2818 return 0;
2819 error:
2820 graph_free(ctx, &split);
2821 return -1;
2824 static int node_scc_exactly(struct isl_sched_node *node, int scc)
2826 return node->scc == scc;
2829 static int node_scc_at_most(struct isl_sched_node *node, int scc)
2831 return node->scc <= scc;
2834 static int node_scc_at_least(struct isl_sched_node *node, int scc)
2836 return node->scc >= scc;
2839 static int edge_scc_exactly(struct isl_sched_edge *edge, int scc)
2841 return edge->src->scc == scc && edge->dst->scc == scc;
2844 static int edge_dst_scc_at_most(struct isl_sched_edge *edge, int scc)
2846 return edge->dst->scc <= scc;
2849 static int edge_src_scc_at_least(struct isl_sched_edge *edge, int scc)
2851 return edge->src->scc >= scc;
2854 /* Pad the schedules of all nodes with zero rows such that in the end
2855 * they all have graph->n_total_row rows.
2856 * The extra rows don't belong to any band, so they get assigned band number -1.
2858 static int pad_schedule(struct isl_sched_graph *graph)
2860 int i, j;
2862 for (i = 0; i < graph->n; ++i) {
2863 struct isl_sched_node *node = &graph->node[i];
2864 int row = isl_mat_rows(node->sched);
2865 if (graph->n_total_row > row) {
2866 isl_map_free(node->sched_map);
2867 node->sched_map = NULL;
2869 node->sched = isl_mat_add_zero_rows(node->sched,
2870 graph->n_total_row - row);
2871 if (!node->sched)
2872 return -1;
2873 for (j = row; j < graph->n_total_row; ++j)
2874 node->band[j] = -1;
2877 return 0;
2880 /* Reset the current band by dropping all its schedule rows.
2882 static int reset_band(struct isl_sched_graph *graph)
2884 int i;
2885 int drop;
2887 drop = graph->n_total_row - graph->band_start;
2888 graph->n_total_row -= drop;
2889 graph->n_row -= drop;
2891 for (i = 0; i < graph->n; ++i) {
2892 struct isl_sched_node *node = &graph->node[i];
2894 isl_map_free(node->sched_map);
2895 node->sched_map = NULL;
2897 node->sched = isl_mat_drop_rows(node->sched,
2898 graph->band_start, drop);
2900 if (!node->sched)
2901 return -1;
2904 return 0;
2907 /* Split the current graph into two parts and compute a schedule for each
2908 * part individually. In particular, one part consists of all SCCs up
2909 * to and including graph->src_scc, while the other part contains the other
2910 * SCCS.
2912 * The split is enforced in the schedule by constant rows with two different
2913 * values (0 and 1). These constant rows replace the previously computed rows
2914 * in the current band.
2915 * It would be possible to reuse them as the first rows in the next
2916 * band, but recomputing them may result in better rows as we are looking
2917 * at a smaller part of the dependence graph.
2919 * Since we do not enforce coincidence, we conservatively mark the
2920 * splitting row as not coincident.
2922 * The band_id of the second group is set to n, where n is the number
2923 * of nodes in the first group. This ensures that the band_ids over
2924 * the two groups remain disjoint, even if either or both of the two
2925 * groups contain independent components.
2927 static int compute_split_schedule(isl_ctx *ctx, struct isl_sched_graph *graph)
2929 int i, j, n, e1, e2;
2930 int n_total_row, orig_total_row;
2931 int n_band, orig_band;
2933 if (graph->n_total_row >= graph->max_row)
2934 isl_die(ctx, isl_error_internal,
2935 "too many schedule rows", return -1);
2937 if (reset_band(graph) < 0)
2938 return -1;
2940 n = 0;
2941 for (i = 0; i < graph->n; ++i) {
2942 struct isl_sched_node *node = &graph->node[i];
2943 int row = isl_mat_rows(node->sched);
2944 int cols = isl_mat_cols(node->sched);
2945 int before = node->scc <= graph->src_scc;
2947 if (before)
2948 n++;
2950 isl_map_free(node->sched_map);
2951 node->sched_map = NULL;
2952 node->sched = isl_mat_add_rows(node->sched, 1);
2953 if (!node->sched)
2954 return -1;
2955 node->sched = isl_mat_set_element_si(node->sched, row, 0,
2956 !before);
2957 for (j = 1; j < cols; ++j)
2958 node->sched = isl_mat_set_element_si(node->sched,
2959 row, j, 0);
2960 node->band[graph->n_total_row] = graph->n_band;
2961 node->coincident[graph->n_total_row] = 0;
2964 e1 = e2 = 0;
2965 for (i = 0; i < graph->n_edge; ++i) {
2966 if (graph->edge[i].dst->scc <= graph->src_scc)
2967 e1++;
2968 if (graph->edge[i].src->scc > graph->src_scc)
2969 e2++;
2972 graph->n_total_row++;
2973 next_band(graph);
2975 for (i = 0; i < graph->n; ++i) {
2976 struct isl_sched_node *node = &graph->node[i];
2977 if (node->scc > graph->src_scc)
2978 node->band_id[graph->n_band] = n;
2981 orig_total_row = graph->n_total_row;
2982 orig_band = graph->n_band;
2983 if (compute_sub_schedule(ctx, graph, n, e1,
2984 &node_scc_at_most, &edge_dst_scc_at_most,
2985 graph->src_scc, 0) < 0)
2986 return -1;
2987 n_total_row = graph->n_total_row;
2988 graph->n_total_row = orig_total_row;
2989 n_band = graph->n_band;
2990 graph->n_band = orig_band;
2991 if (compute_sub_schedule(ctx, graph, graph->n - n, e2,
2992 &node_scc_at_least, &edge_src_scc_at_least,
2993 graph->src_scc + 1, 0) < 0)
2994 return -1;
2995 if (n_total_row > graph->n_total_row)
2996 graph->n_total_row = n_total_row;
2997 if (n_band > graph->n_band)
2998 graph->n_band = n_band;
3000 return pad_schedule(graph);
3003 /* Compute the next band of the schedule after updating the dependence
3004 * relations based on the the current schedule.
3006 static int compute_next_band(isl_ctx *ctx, struct isl_sched_graph *graph)
3008 if (update_edges(ctx, graph) < 0)
3009 return -1;
3010 next_band(graph);
3012 return compute_schedule(ctx, graph);
3015 /* Add constraints to graph->lp that force the dependence "map" (which
3016 * is part of the dependence relation of "edge")
3017 * to be respected and attempt to carry it, where the edge is one from
3018 * a node j to itself. "pos" is the sequence number of the given map.
3019 * That is, add constraints that enforce
3021 * (c_j_0 + c_j_n n + c_j_x y) - (c_j_0 + c_j_n n + c_j_x x)
3022 * = c_j_x (y - x) >= e_i
3024 * for each (x,y) in R.
3025 * We obtain general constraints on coefficients (c_0, c_n, c_x)
3026 * of valid constraints for (y - x) and then plug in (-e_i, 0, c_j_x),
3027 * with each coefficient in c_j_x represented as a pair of non-negative
3028 * coefficients.
3030 static int add_intra_constraints(struct isl_sched_graph *graph,
3031 struct isl_sched_edge *edge, __isl_take isl_map *map, int pos)
3033 unsigned total;
3034 isl_ctx *ctx = isl_map_get_ctx(map);
3035 isl_space *dim;
3036 isl_dim_map *dim_map;
3037 isl_basic_set *coef;
3038 struct isl_sched_node *node = edge->src;
3040 coef = intra_coefficients(graph, node, map);
3041 if (!coef)
3042 return -1;
3044 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
3046 total = isl_basic_set_total_dim(graph->lp);
3047 dim_map = isl_dim_map_alloc(ctx, total);
3048 isl_dim_map_range(dim_map, 3 + pos, 0, 0, 0, 1, -1);
3049 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 1, 2,
3050 isl_space_dim(dim, isl_dim_set), 1,
3051 node->nvar, -1);
3052 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 2, 2,
3053 isl_space_dim(dim, isl_dim_set), 1,
3054 node->nvar, 1);
3055 graph->lp = isl_basic_set_extend_constraints(graph->lp,
3056 coef->n_eq, coef->n_ineq);
3057 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
3058 coef, dim_map);
3059 isl_space_free(dim);
3061 return 0;
3064 /* Add constraints to graph->lp that force the dependence "map" (which
3065 * is part of the dependence relation of "edge")
3066 * to be respected and attempt to carry it, where the edge is one from
3067 * node j to node k. "pos" is the sequence number of the given map.
3068 * That is, add constraints that enforce
3070 * (c_k_0 + c_k_n n + c_k_x y) - (c_j_0 + c_j_n n + c_j_x x) >= e_i
3072 * for each (x,y) in R.
3073 * We obtain general constraints on coefficients (c_0, c_n, c_x)
3074 * of valid constraints for R and then plug in
3075 * (-e_i + c_k_0 - c_j_0, c_k_n - c_j_n, c_k_x - c_j_x)
3076 * with each coefficient (except e_i, c_k_0 and c_j_0)
3077 * represented as a pair of non-negative coefficients.
3079 static int add_inter_constraints(struct isl_sched_graph *graph,
3080 struct isl_sched_edge *edge, __isl_take isl_map *map, int pos)
3082 unsigned total;
3083 isl_ctx *ctx = isl_map_get_ctx(map);
3084 isl_space *dim;
3085 isl_dim_map *dim_map;
3086 isl_basic_set *coef;
3087 struct isl_sched_node *src = edge->src;
3088 struct isl_sched_node *dst = edge->dst;
3090 coef = inter_coefficients(graph, edge, map);
3091 if (!coef)
3092 return -1;
3094 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
3096 total = isl_basic_set_total_dim(graph->lp);
3097 dim_map = isl_dim_map_alloc(ctx, total);
3099 isl_dim_map_range(dim_map, 3 + pos, 0, 0, 0, 1, -1);
3101 isl_dim_map_range(dim_map, dst->start, 0, 0, 0, 1, 1);
3102 isl_dim_map_range(dim_map, dst->start + 1, 2, 1, 1, dst->nparam, -1);
3103 isl_dim_map_range(dim_map, dst->start + 2, 2, 1, 1, dst->nparam, 1);
3104 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 1, 2,
3105 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
3106 dst->nvar, -1);
3107 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 2, 2,
3108 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
3109 dst->nvar, 1);
3111 isl_dim_map_range(dim_map, src->start, 0, 0, 0, 1, -1);
3112 isl_dim_map_range(dim_map, src->start + 1, 2, 1, 1, src->nparam, 1);
3113 isl_dim_map_range(dim_map, src->start + 2, 2, 1, 1, src->nparam, -1);
3114 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 1, 2,
3115 isl_space_dim(dim, isl_dim_set), 1,
3116 src->nvar, 1);
3117 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 2, 2,
3118 isl_space_dim(dim, isl_dim_set), 1,
3119 src->nvar, -1);
3121 graph->lp = isl_basic_set_extend_constraints(graph->lp,
3122 coef->n_eq, coef->n_ineq);
3123 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
3124 coef, dim_map);
3125 isl_space_free(dim);
3127 return 0;
3130 /* Add constraints to graph->lp that force all (conditional) validity
3131 * dependences to be respected and attempt to carry them.
3133 static int add_all_constraints(struct isl_sched_graph *graph)
3135 int i, j;
3136 int pos;
3138 pos = 0;
3139 for (i = 0; i < graph->n_edge; ++i) {
3140 struct isl_sched_edge *edge= &graph->edge[i];
3142 if (!edge->validity && !edge->conditional_validity)
3143 continue;
3145 for (j = 0; j < edge->map->n; ++j) {
3146 isl_basic_map *bmap;
3147 isl_map *map;
3149 bmap = isl_basic_map_copy(edge->map->p[j]);
3150 map = isl_map_from_basic_map(bmap);
3152 if (edge->src == edge->dst &&
3153 add_intra_constraints(graph, edge, map, pos) < 0)
3154 return -1;
3155 if (edge->src != edge->dst &&
3156 add_inter_constraints(graph, edge, map, pos) < 0)
3157 return -1;
3158 ++pos;
3162 return 0;
3165 /* Count the number of equality and inequality constraints
3166 * that will be added to the carry_lp problem.
3167 * We count each edge exactly once.
3169 static int count_all_constraints(struct isl_sched_graph *graph,
3170 int *n_eq, int *n_ineq)
3172 int i, j;
3174 *n_eq = *n_ineq = 0;
3175 for (i = 0; i < graph->n_edge; ++i) {
3176 struct isl_sched_edge *edge= &graph->edge[i];
3177 for (j = 0; j < edge->map->n; ++j) {
3178 isl_basic_map *bmap;
3179 isl_map *map;
3181 bmap = isl_basic_map_copy(edge->map->p[j]);
3182 map = isl_map_from_basic_map(bmap);
3184 if (count_map_constraints(graph, edge, map,
3185 n_eq, n_ineq, 1, 0) < 0)
3186 return -1;
3190 return 0;
3193 /* Construct an LP problem for finding schedule coefficients
3194 * such that the schedule carries as many dependences as possible.
3195 * In particular, for each dependence i, we bound the dependence distance
3196 * from below by e_i, with 0 <= e_i <= 1 and then maximize the sum
3197 * of all e_i's. Dependence with e_i = 0 in the solution are simply
3198 * respected, while those with e_i > 0 (in practice e_i = 1) are carried.
3199 * Note that if the dependence relation is a union of basic maps,
3200 * then we have to consider each basic map individually as it may only
3201 * be possible to carry the dependences expressed by some of those
3202 * basic maps and not all off them.
3203 * Below, we consider each of those basic maps as a separate "edge".
3205 * All variables of the LP are non-negative. The actual coefficients
3206 * may be negative, so each coefficient is represented as the difference
3207 * of two non-negative variables. The negative part always appears
3208 * immediately before the positive part.
3209 * Other than that, the variables have the following order
3211 * - sum of (1 - e_i) over all edges
3212 * - sum of positive and negative parts of all c_n coefficients
3213 * (unconstrained when computing non-parametric schedules)
3214 * - sum of positive and negative parts of all c_x coefficients
3215 * - for each edge
3216 * - e_i
3217 * - for each node
3218 * - c_i_0
3219 * - positive and negative parts of c_i_n (if parametric)
3220 * - positive and negative parts of c_i_x
3222 * The constraints are those from the (validity) edges plus three equalities
3223 * to express the sums and n_edge inequalities to express e_i <= 1.
3225 static int setup_carry_lp(isl_ctx *ctx, struct isl_sched_graph *graph)
3227 int i, j;
3228 int k;
3229 isl_space *dim;
3230 unsigned total;
3231 int n_eq, n_ineq;
3232 int n_edge;
3234 n_edge = 0;
3235 for (i = 0; i < graph->n_edge; ++i)
3236 n_edge += graph->edge[i].map->n;
3238 total = 3 + n_edge;
3239 for (i = 0; i < graph->n; ++i) {
3240 struct isl_sched_node *node = &graph->node[graph->sorted[i]];
3241 node->start = total;
3242 total += 1 + 2 * (node->nparam + node->nvar);
3245 if (count_all_constraints(graph, &n_eq, &n_ineq) < 0)
3246 return -1;
3247 if (count_bound_coefficient_constraints(ctx, graph, &n_eq, &n_ineq) < 0)
3248 return -1;
3250 dim = isl_space_set_alloc(ctx, 0, total);
3251 isl_basic_set_free(graph->lp);
3252 n_eq += 3;
3253 n_ineq += n_edge;
3254 graph->lp = isl_basic_set_alloc_space(dim, 0, n_eq, n_ineq);
3255 graph->lp = isl_basic_set_set_rational(graph->lp);
3257 k = isl_basic_set_alloc_equality(graph->lp);
3258 if (k < 0)
3259 return -1;
3260 isl_seq_clr(graph->lp->eq[k], 1 + total);
3261 isl_int_set_si(graph->lp->eq[k][0], -n_edge);
3262 isl_int_set_si(graph->lp->eq[k][1], 1);
3263 for (i = 0; i < n_edge; ++i)
3264 isl_int_set_si(graph->lp->eq[k][4 + i], 1);
3266 k = isl_basic_set_alloc_equality(graph->lp);
3267 if (k < 0)
3268 return -1;
3269 isl_seq_clr(graph->lp->eq[k], 1 + total);
3270 isl_int_set_si(graph->lp->eq[k][2], -1);
3271 for (i = 0; i < graph->n; ++i) {
3272 int pos = 1 + graph->node[i].start + 1;
3274 for (j = 0; j < 2 * graph->node[i].nparam; ++j)
3275 isl_int_set_si(graph->lp->eq[k][pos + j], 1);
3278 k = isl_basic_set_alloc_equality(graph->lp);
3279 if (k < 0)
3280 return -1;
3281 isl_seq_clr(graph->lp->eq[k], 1 + total);
3282 isl_int_set_si(graph->lp->eq[k][3], -1);
3283 for (i = 0; i < graph->n; ++i) {
3284 struct isl_sched_node *node = &graph->node[i];
3285 int pos = 1 + node->start + 1 + 2 * node->nparam;
3287 for (j = 0; j < 2 * node->nvar; ++j)
3288 isl_int_set_si(graph->lp->eq[k][pos + j], 1);
3291 for (i = 0; i < n_edge; ++i) {
3292 k = isl_basic_set_alloc_inequality(graph->lp);
3293 if (k < 0)
3294 return -1;
3295 isl_seq_clr(graph->lp->ineq[k], 1 + total);
3296 isl_int_set_si(graph->lp->ineq[k][4 + i], -1);
3297 isl_int_set_si(graph->lp->ineq[k][0], 1);
3300 if (add_bound_coefficient_constraints(ctx, graph) < 0)
3301 return -1;
3302 if (add_all_constraints(graph) < 0)
3303 return -1;
3305 return 0;
3308 static int compute_component_schedule(isl_ctx *ctx,
3309 struct isl_sched_graph *graph, int wcc);
3311 /* Comparison function for sorting the statements based on
3312 * the corresponding value in "r".
3314 static int smaller_value(const void *a, const void *b, void *data)
3316 isl_vec *r = data;
3317 const int *i1 = a;
3318 const int *i2 = b;
3320 return isl_int_cmp(r->el[*i1], r->el[*i2]);
3323 /* If the schedule_split_scaled option is set and if the linear
3324 * parts of the scheduling rows for all nodes in the graphs have
3325 * a non-trivial common divisor, then split off the remainder of the
3326 * constant term modulo this common divisor from the linear part.
3327 * Otherwise, continue with the construction of the schedule.
3329 * If a non-trivial common divisor is found, then
3330 * the linear part is reduced and the remainder is enforced
3331 * by a piecewise constant schedule based on the order of these remainders.
3332 * In particular, we assign an scc index based on the remainder and
3333 * then rely on compute_component_schedule to insert the schedule row and
3334 * to continue the schedule construction on each part.
3336 static int split_scaled(isl_ctx *ctx, struct isl_sched_graph *graph)
3338 int i;
3339 int row;
3340 int scc;
3341 isl_int gcd, gcd_i;
3342 isl_vec *r;
3343 int *order;
3345 if (!ctx->opt->schedule_split_scaled)
3346 return compute_next_band(ctx, graph);
3347 if (graph->n <= 1)
3348 return compute_next_band(ctx, graph);
3350 isl_int_init(gcd);
3351 isl_int_init(gcd_i);
3353 isl_int_set_si(gcd, 0);
3355 row = isl_mat_rows(graph->node[0].sched) - 1;
3357 for (i = 0; i < graph->n; ++i) {
3358 struct isl_sched_node *node = &graph->node[i];
3359 int cols = isl_mat_cols(node->sched);
3361 isl_seq_gcd(node->sched->row[row] + 1, cols - 1, &gcd_i);
3362 isl_int_gcd(gcd, gcd, gcd_i);
3365 isl_int_clear(gcd_i);
3367 if (isl_int_cmp_si(gcd, 1) <= 0) {
3368 isl_int_clear(gcd);
3369 return compute_next_band(ctx, graph);
3372 r = isl_vec_alloc(ctx, graph->n);
3373 order = isl_calloc_array(ctx, int, graph->n);
3374 if (!r || !order)
3375 goto error;
3377 for (i = 0; i < graph->n; ++i) {
3378 struct isl_sched_node *node = &graph->node[i];
3380 order[i] = i;
3381 isl_int_fdiv_r(r->el[i], node->sched->row[row][0], gcd);
3382 isl_int_fdiv_q(node->sched->row[row][0],
3383 node->sched->row[row][0], gcd);
3384 isl_int_mul(node->sched->row[row][0],
3385 node->sched->row[row][0], gcd);
3386 node->sched = isl_mat_scale_down_row(node->sched, row, gcd);
3387 if (!node->sched)
3388 goto error;
3391 if (isl_sort(order, graph->n, sizeof(order[0]), &smaller_value, r) < 0)
3392 goto error;
3394 scc = 0;
3395 for (i = 0; i < graph->n; ++i) {
3396 if (i > 0 && isl_int_ne(r->el[order[i - 1]], r->el[order[i]]))
3397 ++scc;
3398 graph->node[order[i]].scc = scc;
3400 graph->scc = ++scc;
3401 graph->weak = 0;
3403 isl_int_clear(gcd);
3404 isl_vec_free(r);
3405 free(order);
3407 if (update_edges(ctx, graph) < 0)
3408 return -1;
3409 next_band(graph);
3411 return compute_component_schedule(ctx, graph, 0);
3412 error:
3413 isl_int_clear(gcd);
3414 return -1;
3417 /* Is the schedule row "sol" trivial on node "node"?
3418 * That is, is the solution zero on the dimensions orthogonal to
3419 * the previously found solutions?
3420 * Return 1 if the solution is trivial, 0 if it is not and -1 on error.
3422 * Each coefficient is represented as the difference between
3423 * two non-negative values in "sol". "sol" has been computed
3424 * in terms of the original iterators (i.e., without use of cmap).
3425 * We construct the schedule row s and write it as a linear
3426 * combination of (linear combinations of) previously computed schedule rows.
3427 * s = Q c or c = U s.
3428 * If the final entries of c are all zero, then the solution is trivial.
3430 static int is_trivial(struct isl_sched_node *node, __isl_keep isl_vec *sol)
3432 int i;
3433 int pos;
3434 int trivial;
3435 isl_ctx *ctx;
3436 isl_vec *node_sol;
3438 if (!sol)
3439 return -1;
3440 if (node->nvar == node->rank)
3441 return 0;
3443 ctx = isl_vec_get_ctx(sol);
3444 node_sol = isl_vec_alloc(ctx, node->nvar);
3445 if (!node_sol)
3446 return -1;
3448 pos = 1 + node->start + 1 + 2 * node->nparam;
3450 for (i = 0; i < node->nvar; ++i)
3451 isl_int_sub(node_sol->el[i],
3452 sol->el[pos + 2 * i + 1], sol->el[pos + 2 * i]);
3454 node_sol = isl_mat_vec_product(isl_mat_copy(node->cinv), node_sol);
3456 if (!node_sol)
3457 return -1;
3459 trivial = isl_seq_first_non_zero(node_sol->el + node->rank,
3460 node->nvar - node->rank) == -1;
3462 isl_vec_free(node_sol);
3464 return trivial;
3467 /* Is the schedule row "sol" trivial on any node where it should
3468 * not be trivial?
3469 * "sol" has been computed in terms of the original iterators
3470 * (i.e., without use of cmap).
3471 * Return 1 if any solution is trivial, 0 if they are not and -1 on error.
3473 static int is_any_trivial(struct isl_sched_graph *graph,
3474 __isl_keep isl_vec *sol)
3476 int i;
3478 for (i = 0; i < graph->n; ++i) {
3479 struct isl_sched_node *node = &graph->node[i];
3480 int trivial;
3482 if (!needs_row(graph, node))
3483 continue;
3484 trivial = is_trivial(node, sol);
3485 if (trivial < 0 || trivial)
3486 return trivial;
3489 return 0;
3492 /* Construct a schedule row for each node such that as many dependences
3493 * as possible are carried and then continue with the next band.
3495 * If the computed schedule row turns out to be trivial on one or
3496 * more nodes where it should not be trivial, then we throw it away
3497 * and try again on each component separately.
3499 * If there is only one component, then we accept the schedule row anyway,
3500 * but we do not consider it as a complete row and therefore do not
3501 * increment graph->n_row. Note that the ranks of the nodes that
3502 * do get a non-trivial schedule part will get updated regardless and
3503 * graph->maxvar is computed based on these ranks. The test for
3504 * whether more schedule rows are required in compute_schedule_wcc
3505 * is therefore not affected.
3507 * Continue with the construction of the schedule in split_scaled
3508 * after optionally checking for non-trivial common divisors.
3510 static int carry_dependences(isl_ctx *ctx, struct isl_sched_graph *graph)
3512 int i;
3513 int n_edge;
3514 int trivial;
3515 isl_vec *sol;
3516 isl_basic_set *lp;
3518 n_edge = 0;
3519 for (i = 0; i < graph->n_edge; ++i)
3520 n_edge += graph->edge[i].map->n;
3522 if (setup_carry_lp(ctx, graph) < 0)
3523 return -1;
3525 lp = isl_basic_set_copy(graph->lp);
3526 sol = isl_tab_basic_set_non_neg_lexmin(lp);
3527 if (!sol)
3528 return -1;
3530 if (sol->size == 0) {
3531 isl_vec_free(sol);
3532 isl_die(ctx, isl_error_internal,
3533 "error in schedule construction", return -1);
3536 isl_int_divexact(sol->el[1], sol->el[1], sol->el[0]);
3537 if (isl_int_cmp_si(sol->el[1], n_edge) >= 0) {
3538 isl_vec_free(sol);
3539 isl_die(ctx, isl_error_unknown,
3540 "unable to carry dependences", return -1);
3543 trivial = is_any_trivial(graph, sol);
3544 if (trivial < 0) {
3545 sol = isl_vec_free(sol);
3546 } else if (trivial && graph->scc > 1) {
3547 isl_vec_free(sol);
3548 return compute_component_schedule(ctx, graph, 1);
3551 if (update_schedule(graph, sol, 0, 0) < 0)
3552 return -1;
3553 if (trivial)
3554 graph->n_row--;
3556 return split_scaled(ctx, graph);
3559 /* Are there any (non-empty) (conditional) validity edges in the graph?
3561 static int has_validity_edges(struct isl_sched_graph *graph)
3563 int i;
3565 for (i = 0; i < graph->n_edge; ++i) {
3566 int empty;
3568 empty = isl_map_plain_is_empty(graph->edge[i].map);
3569 if (empty < 0)
3570 return -1;
3571 if (empty)
3572 continue;
3573 if (graph->edge[i].validity ||
3574 graph->edge[i].conditional_validity)
3575 return 1;
3578 return 0;
3581 /* Should we apply a Feautrier step?
3582 * That is, did the user request the Feautrier algorithm and are
3583 * there any validity dependences (left)?
3585 static int need_feautrier_step(isl_ctx *ctx, struct isl_sched_graph *graph)
3587 if (ctx->opt->schedule_algorithm != ISL_SCHEDULE_ALGORITHM_FEAUTRIER)
3588 return 0;
3590 return has_validity_edges(graph);
3593 /* Compute a schedule for a connected dependence graph using Feautrier's
3594 * multi-dimensional scheduling algorithm.
3595 * The original algorithm is described in [1].
3596 * The main idea is to minimize the number of scheduling dimensions, by
3597 * trying to satisfy as many dependences as possible per scheduling dimension.
3599 * [1] P. Feautrier, Some Efficient Solutions to the Affine Scheduling
3600 * Problem, Part II: Multi-Dimensional Time.
3601 * In Intl. Journal of Parallel Programming, 1992.
3603 static int compute_schedule_wcc_feautrier(isl_ctx *ctx,
3604 struct isl_sched_graph *graph)
3606 return carry_dependences(ctx, graph);
3609 /* Turn off the "local" bit on all (condition) edges.
3611 static void clear_local_edges(struct isl_sched_graph *graph)
3613 int i;
3615 for (i = 0; i < graph->n_edge; ++i)
3616 if (graph->edge[i].condition)
3617 graph->edge[i].local = 0;
3620 /* Does "graph" have both condition and conditional validity edges?
3622 static int need_condition_check(struct isl_sched_graph *graph)
3624 int i;
3625 int any_condition = 0;
3626 int any_conditional_validity = 0;
3628 for (i = 0; i < graph->n_edge; ++i) {
3629 if (graph->edge[i].condition)
3630 any_condition = 1;
3631 if (graph->edge[i].conditional_validity)
3632 any_conditional_validity = 1;
3635 return any_condition && any_conditional_validity;
3638 /* Does "graph" contain any coincidence edge?
3640 static int has_any_coincidence(struct isl_sched_graph *graph)
3642 int i;
3644 for (i = 0; i < graph->n_edge; ++i)
3645 if (graph->edge[i].coincidence)
3646 return 1;
3648 return 0;
3651 /* Extract the final schedule row as a map with the iteration domain
3652 * of "node" as domain.
3654 static __isl_give isl_map *final_row(struct isl_sched_node *node)
3656 isl_local_space *ls;
3657 isl_aff *aff;
3658 int row;
3660 row = isl_mat_rows(node->sched) - 1;
3661 ls = isl_local_space_from_space(isl_space_copy(node->space));
3662 aff = extract_schedule_row(ls, node, row);
3663 return isl_map_from_aff(aff);
3666 /* Is the conditional validity dependence in the edge with index "edge_index"
3667 * violated by the latest (i.e., final) row of the schedule?
3668 * That is, is i scheduled after j
3669 * for any conditional validity dependence i -> j?
3671 static int is_violated(struct isl_sched_graph *graph, int edge_index)
3673 isl_map *src_sched, *dst_sched, *map;
3674 struct isl_sched_edge *edge = &graph->edge[edge_index];
3675 int empty;
3677 src_sched = final_row(edge->src);
3678 dst_sched = final_row(edge->dst);
3679 map = isl_map_copy(edge->map);
3680 map = isl_map_apply_domain(map, src_sched);
3681 map = isl_map_apply_range(map, dst_sched);
3682 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_out, 0);
3683 empty = isl_map_is_empty(map);
3684 isl_map_free(map);
3686 if (empty < 0)
3687 return -1;
3689 return !empty;
3692 /* Does the domain of "umap" intersect "uset"?
3694 static int domain_intersects(__isl_keep isl_union_map *umap,
3695 __isl_keep isl_union_set *uset)
3697 int empty;
3699 umap = isl_union_map_copy(umap);
3700 umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(uset));
3701 empty = isl_union_map_is_empty(umap);
3702 isl_union_map_free(umap);
3704 return empty < 0 ? -1 : !empty;
3707 /* Does the range of "umap" intersect "uset"?
3709 static int range_intersects(__isl_keep isl_union_map *umap,
3710 __isl_keep isl_union_set *uset)
3712 int empty;
3714 umap = isl_union_map_copy(umap);
3715 umap = isl_union_map_intersect_range(umap, isl_union_set_copy(uset));
3716 empty = isl_union_map_is_empty(umap);
3717 isl_union_map_free(umap);
3719 return empty < 0 ? -1 : !empty;
3722 /* Are the condition dependences of "edge" local with respect to
3723 * the current schedule?
3725 * That is, are domain and range of the condition dependences mapped
3726 * to the same point?
3728 * In other words, is the condition false?
3730 static int is_condition_false(struct isl_sched_edge *edge)
3732 isl_union_map *umap;
3733 isl_map *map, *sched, *test;
3734 int local;
3736 umap = isl_union_map_copy(edge->tagged_condition);
3737 umap = isl_union_map_zip(umap);
3738 umap = isl_union_set_unwrap(isl_union_map_domain(umap));
3739 map = isl_map_from_union_map(umap);
3741 sched = node_extract_schedule(edge->src);
3742 map = isl_map_apply_domain(map, sched);
3743 sched = node_extract_schedule(edge->dst);
3744 map = isl_map_apply_range(map, sched);
3746 test = isl_map_identity(isl_map_get_space(map));
3747 local = isl_map_is_subset(map, test);
3748 isl_map_free(map);
3749 isl_map_free(test);
3751 return local;
3754 /* Does "graph" have any satisfied condition edges that
3755 * are adjacent to the conditional validity constraint with
3756 * domain "conditional_source" and range "conditional_sink"?
3758 * A satisfied condition is one that is not local.
3759 * If a condition was forced to be local already (i.e., marked as local)
3760 * then there is no need to check if it is in fact local.
3762 * Additionally, mark all adjacent condition edges found as local.
3764 static int has_adjacent_true_conditions(struct isl_sched_graph *graph,
3765 __isl_keep isl_union_set *conditional_source,
3766 __isl_keep isl_union_set *conditional_sink)
3768 int i;
3769 int any = 0;
3771 for (i = 0; i < graph->n_edge; ++i) {
3772 int adjacent, local;
3773 isl_union_map *condition;
3775 if (!graph->edge[i].condition)
3776 continue;
3777 if (graph->edge[i].local)
3778 continue;
3780 condition = graph->edge[i].tagged_condition;
3781 adjacent = domain_intersects(condition, conditional_sink);
3782 if (adjacent >= 0 && !adjacent)
3783 adjacent = range_intersects(condition,
3784 conditional_source);
3785 if (adjacent < 0)
3786 return -1;
3787 if (!adjacent)
3788 continue;
3790 graph->edge[i].local = 1;
3792 local = is_condition_false(&graph->edge[i]);
3793 if (local < 0)
3794 return -1;
3795 if (!local)
3796 any = 1;
3799 return any;
3802 /* Are there any violated conditional validity dependences with
3803 * adjacent condition dependences that are not local with respect
3804 * to the current schedule?
3805 * That is, is the conditional validity constraint violated?
3807 * Additionally, mark all those adjacent condition dependences as local.
3808 * We also mark those adjacent condition dependences that were not marked
3809 * as local before, but just happened to be local already. This ensures
3810 * that they remain local if the schedule is recomputed.
3812 * We first collect domain and range of all violated conditional validity
3813 * dependences and then check if there are any adjacent non-local
3814 * condition dependences.
3816 static int has_violated_conditional_constraint(isl_ctx *ctx,
3817 struct isl_sched_graph *graph)
3819 int i;
3820 int any = 0;
3821 isl_union_set *source, *sink;
3823 source = isl_union_set_empty(isl_space_params_alloc(ctx, 0));
3824 sink = isl_union_set_empty(isl_space_params_alloc(ctx, 0));
3825 for (i = 0; i < graph->n_edge; ++i) {
3826 isl_union_set *uset;
3827 isl_union_map *umap;
3828 int violated;
3830 if (!graph->edge[i].conditional_validity)
3831 continue;
3833 violated = is_violated(graph, i);
3834 if (violated < 0)
3835 goto error;
3836 if (!violated)
3837 continue;
3839 any = 1;
3841 umap = isl_union_map_copy(graph->edge[i].tagged_validity);
3842 uset = isl_union_map_domain(umap);
3843 source = isl_union_set_union(source, uset);
3844 source = isl_union_set_coalesce(source);
3846 umap = isl_union_map_copy(graph->edge[i].tagged_validity);
3847 uset = isl_union_map_range(umap);
3848 sink = isl_union_set_union(sink, uset);
3849 sink = isl_union_set_coalesce(sink);
3852 if (any)
3853 any = has_adjacent_true_conditions(graph, source, sink);
3855 isl_union_set_free(source);
3856 isl_union_set_free(sink);
3857 return any;
3858 error:
3859 isl_union_set_free(source);
3860 isl_union_set_free(sink);
3861 return -1;
3864 /* Compute a schedule for a connected dependence graph.
3865 * We try to find a sequence of as many schedule rows as possible that result
3866 * in non-negative dependence distances (independent of the previous rows
3867 * in the sequence, i.e., such that the sequence is tilable), with as
3868 * many of the initial rows as possible satisfying the coincidence constraints.
3869 * If we can't find any more rows we either
3870 * - split between SCCs and start over (assuming we found an interesting
3871 * pair of SCCs between which to split)
3872 * - continue with the next band (assuming the current band has at least
3873 * one row)
3874 * - try to carry as many dependences as possible and continue with the next
3875 * band
3877 * If Feautrier's algorithm is selected, we first recursively try to satisfy
3878 * as many validity dependences as possible. When all validity dependences
3879 * are satisfied we extend the schedule to a full-dimensional schedule.
3881 * If we manage to complete the schedule, we finish off by topologically
3882 * sorting the statements based on the remaining dependences.
3884 * If ctx->opt->schedule_outer_coincidence is set, then we force the
3885 * outermost dimension to satisfy the coincidence constraints. If this
3886 * turns out to be impossible, we fall back on the general scheme above
3887 * and try to carry as many dependences as possible.
3889 * If "graph" contains both condition and conditional validity dependences,
3890 * then we need to check that that the conditional schedule constraint
3891 * is satisfied, i.e., there are no violated conditional validity dependences
3892 * that are adjacent to any non-local condition dependences.
3893 * If there are, then we mark all those adjacent condition dependences
3894 * as local and recompute the current band. Those dependences that
3895 * are marked local will then be forced to be local.
3896 * The initial computation is performed with no dependences marked as local.
3897 * If we are lucky, then there will be no violated conditional validity
3898 * dependences adjacent to any non-local condition dependences.
3899 * Otherwise, we mark some additional condition dependences as local and
3900 * recompute. We continue this process until there are no violations left or
3901 * until we are no longer able to compute a schedule.
3902 * Since there are only a finite number of dependences,
3903 * there will only be a finite number of iterations.
3905 static int compute_schedule_wcc(isl_ctx *ctx, struct isl_sched_graph *graph)
3907 int has_coincidence;
3908 int use_coincidence;
3909 int force_coincidence = 0;
3910 int check_conditional;
3912 if (detect_sccs(ctx, graph) < 0)
3913 return -1;
3914 if (sort_sccs(graph) < 0)
3915 return -1;
3917 if (compute_maxvar(graph) < 0)
3918 return -1;
3920 if (need_feautrier_step(ctx, graph))
3921 return compute_schedule_wcc_feautrier(ctx, graph);
3923 clear_local_edges(graph);
3924 check_conditional = need_condition_check(graph);
3925 has_coincidence = has_any_coincidence(graph);
3927 if (ctx->opt->schedule_outer_coincidence)
3928 force_coincidence = 1;
3930 use_coincidence = has_coincidence;
3931 while (graph->n_row < graph->maxvar) {
3932 isl_vec *sol;
3933 int violated;
3934 int coincident;
3936 graph->src_scc = -1;
3937 graph->dst_scc = -1;
3939 if (setup_lp(ctx, graph, use_coincidence) < 0)
3940 return -1;
3941 sol = solve_lp(graph);
3942 if (!sol)
3943 return -1;
3944 if (sol->size == 0) {
3945 int empty = graph->n_total_row == graph->band_start;
3947 isl_vec_free(sol);
3948 if (use_coincidence && (!force_coincidence || !empty)) {
3949 use_coincidence = 0;
3950 continue;
3952 if (!ctx->opt->schedule_maximize_band_depth && !empty)
3953 return compute_next_band(ctx, graph);
3954 if (graph->src_scc >= 0)
3955 return compute_split_schedule(ctx, graph);
3956 if (!empty)
3957 return compute_next_band(ctx, graph);
3958 return carry_dependences(ctx, graph);
3960 coincident = !has_coincidence || use_coincidence;
3961 if (update_schedule(graph, sol, 1, coincident) < 0)
3962 return -1;
3964 if (!check_conditional)
3965 continue;
3966 violated = has_violated_conditional_constraint(ctx, graph);
3967 if (violated < 0)
3968 return -1;
3969 if (!violated)
3970 continue;
3971 if (reset_band(graph) < 0)
3972 return -1;
3973 use_coincidence = has_coincidence;
3976 if (graph->n_total_row > graph->band_start)
3977 next_band(graph);
3978 return sort_statements(ctx, graph);
3981 /* Add a row to the schedules that separates the SCCs and move
3982 * to the next band.
3984 static int split_on_scc(isl_ctx *ctx, struct isl_sched_graph *graph)
3986 int i;
3988 if (graph->n_total_row >= graph->max_row)
3989 isl_die(ctx, isl_error_internal,
3990 "too many schedule rows", return -1);
3992 for (i = 0; i < graph->n; ++i) {
3993 struct isl_sched_node *node = &graph->node[i];
3994 int row = isl_mat_rows(node->sched);
3996 isl_map_free(node->sched_map);
3997 node->sched_map = NULL;
3998 node->sched = isl_mat_add_zero_rows(node->sched, 1);
3999 node->sched = isl_mat_set_element_si(node->sched, row, 0,
4000 node->scc);
4001 if (!node->sched)
4002 return -1;
4003 node->band[graph->n_total_row] = graph->n_band;
4006 graph->n_total_row++;
4007 next_band(graph);
4009 return 0;
4012 /* Compute a schedule for each group of nodes identified by node->scc
4013 * separately and then combine the results.
4014 * If "wcc" is set then each of these groups belongs to a single
4015 * weakly connected component in the dependence graph so that
4016 * there is no need for compute_sub_schedule to look for weakly
4017 * connected components.
4019 * An extra schedule row is added first to separate the groups
4020 * unless the groups represent weakly connected components
4021 * (graph->weak is set) and the option schedule_separate_components
4022 * is not set.
4024 * The band_id is adjusted such that each component has a separate id.
4025 * Note that the band_id may have already been set to a value different
4026 * from zero by compute_split_schedule.
4028 static int compute_component_schedule(isl_ctx *ctx,
4029 struct isl_sched_graph *graph, int wcc)
4031 int component, i;
4032 int n, n_edge;
4033 int n_total_row, orig_total_row;
4034 int n_band, orig_band;
4036 if (!graph->weak || ctx->opt->schedule_separate_components)
4037 if (split_on_scc(ctx, graph) < 0)
4038 return -1;
4040 n_total_row = 0;
4041 orig_total_row = graph->n_total_row;
4042 n_band = 0;
4043 orig_band = graph->n_band;
4044 for (i = 0; i < graph->n; ++i)
4045 graph->node[i].band_id[graph->n_band] += graph->node[i].scc;
4046 for (component = 0; component < graph->scc; ++component) {
4047 n = 0;
4048 for (i = 0; i < graph->n; ++i)
4049 if (graph->node[i].scc == component)
4050 n++;
4051 n_edge = 0;
4052 for (i = 0; i < graph->n_edge; ++i)
4053 if (graph->edge[i].src->scc == component &&
4054 graph->edge[i].dst->scc == component)
4055 n_edge++;
4057 if (compute_sub_schedule(ctx, graph, n, n_edge,
4058 &node_scc_exactly,
4059 &edge_scc_exactly, component, wcc) < 0)
4060 return -1;
4061 if (graph->n_total_row > n_total_row)
4062 n_total_row = graph->n_total_row;
4063 graph->n_total_row = orig_total_row;
4064 if (graph->n_band > n_band)
4065 n_band = graph->n_band;
4066 graph->n_band = orig_band;
4069 graph->n_total_row = n_total_row;
4070 graph->n_band = n_band;
4072 return pad_schedule(graph);
4075 /* Compute a schedule for the given dependence graph.
4076 * We first check if the graph is connected (through validity and conditional
4077 * validity dependences) and, if not, compute a schedule
4078 * for each component separately.
4079 * If schedule_fuse is set to minimal fusion, then we check for strongly
4080 * connected components instead and compute a separate schedule for
4081 * each such strongly connected component.
4083 static int compute_schedule(isl_ctx *ctx, struct isl_sched_graph *graph)
4085 if (ctx->opt->schedule_fuse == ISL_SCHEDULE_FUSE_MIN) {
4086 if (detect_sccs(ctx, graph) < 0)
4087 return -1;
4088 } else {
4089 if (detect_wccs(ctx, graph) < 0)
4090 return -1;
4093 if (graph->scc > 1)
4094 return compute_component_schedule(ctx, graph, 1);
4096 return compute_schedule_wcc(ctx, graph);
4099 /* Compute a schedule on sc->domain that respects the given schedule
4100 * constraints.
4102 * In particular, the schedule respects all the validity dependences.
4103 * If the default isl scheduling algorithm is used, it tries to minimize
4104 * the dependence distances over the proximity dependences.
4105 * If Feautrier's scheduling algorithm is used, the proximity dependence
4106 * distances are only minimized during the extension to a full-dimensional
4107 * schedule.
4109 * If there are any condition and conditional validity dependences,
4110 * then the conditional validity dependences may be violated inside
4111 * a tilable band, provided they have no adjacent non-local
4112 * condition dependences.
4114 __isl_give isl_schedule *isl_schedule_constraints_compute_schedule(
4115 __isl_take isl_schedule_constraints *sc)
4117 isl_ctx *ctx = isl_schedule_constraints_get_ctx(sc);
4118 struct isl_sched_graph graph = { 0 };
4119 isl_schedule *sched;
4120 struct isl_extract_edge_data data;
4121 enum isl_edge_type i;
4123 sc = isl_schedule_constraints_align_params(sc);
4124 if (!sc)
4125 return NULL;
4127 graph.n = isl_union_set_n_set(sc->domain);
4128 if (graph.n == 0)
4129 goto empty;
4130 if (graph_alloc(ctx, &graph, graph.n,
4131 isl_schedule_constraints_n_map(sc)) < 0)
4132 goto error;
4133 if (compute_max_row(&graph, sc) < 0)
4134 goto error;
4135 graph.root = 1;
4136 graph.n = 0;
4137 if (isl_union_set_foreach_set(sc->domain, &extract_node, &graph) < 0)
4138 goto error;
4139 if (graph_init_table(ctx, &graph) < 0)
4140 goto error;
4141 for (i = isl_edge_first; i <= isl_edge_last; ++i)
4142 graph.max_edge[i] = isl_union_map_n_map(sc->constraint[i]);
4143 if (graph_init_edge_tables(ctx, &graph) < 0)
4144 goto error;
4145 graph.n_edge = 0;
4146 data.graph = &graph;
4147 for (i = isl_edge_first; i <= isl_edge_last; ++i) {
4148 data.type = i;
4149 if (isl_union_map_foreach_map(sc->constraint[i],
4150 &extract_edge, &data) < 0)
4151 goto error;
4154 if (compute_schedule(ctx, &graph) < 0)
4155 goto error;
4157 empty:
4158 sched = extract_schedule(&graph, isl_union_set_get_space(sc->domain));
4160 graph_free(ctx, &graph);
4161 isl_schedule_constraints_free(sc);
4163 return sched;
4164 error:
4165 graph_free(ctx, &graph);
4166 isl_schedule_constraints_free(sc);
4167 return NULL;
4170 /* Compute a schedule for the given union of domains that respects
4171 * all the validity dependences and minimizes
4172 * the dependence distances over the proximity dependences.
4174 * This function is kept for backward compatibility.
4176 __isl_give isl_schedule *isl_union_set_compute_schedule(
4177 __isl_take isl_union_set *domain,
4178 __isl_take isl_union_map *validity,
4179 __isl_take isl_union_map *proximity)
4181 isl_schedule_constraints *sc;
4183 sc = isl_schedule_constraints_on_domain(domain);
4184 sc = isl_schedule_constraints_set_validity(sc, validity);
4185 sc = isl_schedule_constraints_set_proximity(sc, proximity);
4187 return isl_schedule_constraints_compute_schedule(sc);