isl_scheduler.c: move sort_statements down
[isl.git] / isl_scheduler.c
blobe6aff25f3a87fd31fee41fc2a1af76a12ba2fbce
1 /*
2 * Copyright 2011 INRIA Saclay
3 * Copyright 2012-2014 Ecole Normale Superieure
4 * Copyright 2015 Sven Verdoolaege
6 * Use of this software is governed by the MIT license
8 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
9 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
10 * 91893 Orsay, France
11 * and Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
14 #include <isl_ctx_private.h>
15 #include <isl_map_private.h>
16 #include <isl_space_private.h>
17 #include <isl_aff_private.h>
18 #include <isl/hash.h>
19 #include <isl/constraint.h>
20 #include <isl/schedule.h>
21 #include <isl_mat_private.h>
22 #include <isl_vec_private.h>
23 #include <isl/set.h>
24 #include <isl_seq.h>
25 #include <isl_tab.h>
26 #include <isl_dim_map.h>
27 #include <isl/map_to_basic_set.h>
28 #include <isl_sort.h>
29 #include <isl_schedule_private.h>
30 #include <isl_options_private.h>
31 #include <isl_tarjan.h>
32 #include <isl_morph.h>
35 * The scheduling algorithm implemented in this file was inspired by
36 * Bondhugula et al., "Automatic Transformations for Communication-Minimized
37 * Parallelization and Locality Optimization in the Polyhedral Model".
40 enum isl_edge_type {
41 isl_edge_validity = 0,
42 isl_edge_first = isl_edge_validity,
43 isl_edge_coincidence,
44 isl_edge_condition,
45 isl_edge_conditional_validity,
46 isl_edge_proximity,
47 isl_edge_last = isl_edge_proximity
50 /* The constraints that need to be satisfied by a schedule on "domain".
52 * "validity" constraints map domain elements i to domain elements
53 * that should be scheduled after i. (Hard constraint)
54 * "proximity" constraints map domain elements i to domains elements
55 * that should be scheduled as early as possible after i (or before i).
56 * (Soft constraint)
58 * "condition" and "conditional_validity" constraints map possibly "tagged"
59 * domain elements i -> s to "tagged" domain elements j -> t.
60 * The elements of the "conditional_validity" constraints, but without the
61 * tags (i.e., the elements i -> j) are treated as validity constraints,
62 * except that during the construction of a tilable band,
63 * the elements of the "conditional_validity" constraints may be violated
64 * provided that all adjacent elements of the "condition" constraints
65 * are local within the band.
66 * A dependence is local within a band if domain and range are mapped
67 * to the same schedule point by the band.
69 struct isl_schedule_constraints {
70 isl_union_set *domain;
72 isl_union_map *constraint[isl_edge_last + 1];
75 __isl_give isl_schedule_constraints *isl_schedule_constraints_copy(
76 __isl_keep isl_schedule_constraints *sc)
78 isl_ctx *ctx;
79 isl_schedule_constraints *sc_copy;
80 enum isl_edge_type i;
82 ctx = isl_union_set_get_ctx(sc->domain);
83 sc_copy = isl_calloc_type(ctx, struct isl_schedule_constraints);
84 if (!sc_copy)
85 return NULL;
87 sc_copy->domain = isl_union_set_copy(sc->domain);
88 if (!sc_copy->domain)
89 return isl_schedule_constraints_free(sc_copy);
91 for (i = isl_edge_first; i <= isl_edge_last; ++i) {
92 sc_copy->constraint[i] = isl_union_map_copy(sc->constraint[i]);
93 if (!sc_copy->constraint[i])
94 return isl_schedule_constraints_free(sc_copy);
97 return sc_copy;
101 /* Construct an isl_schedule_constraints object for computing a schedule
102 * on "domain". The initial object does not impose any constraints.
104 __isl_give isl_schedule_constraints *isl_schedule_constraints_on_domain(
105 __isl_take isl_union_set *domain)
107 isl_ctx *ctx;
108 isl_space *space;
109 isl_schedule_constraints *sc;
110 isl_union_map *empty;
111 enum isl_edge_type i;
113 if (!domain)
114 return NULL;
116 ctx = isl_union_set_get_ctx(domain);
117 sc = isl_calloc_type(ctx, struct isl_schedule_constraints);
118 if (!sc)
119 goto error;
121 space = isl_union_set_get_space(domain);
122 sc->domain = domain;
123 empty = isl_union_map_empty(space);
124 for (i = isl_edge_first; i <= isl_edge_last; ++i) {
125 sc->constraint[i] = isl_union_map_copy(empty);
126 if (!sc->constraint[i])
127 sc->domain = isl_union_set_free(sc->domain);
129 isl_union_map_free(empty);
131 if (!sc->domain)
132 return isl_schedule_constraints_free(sc);
134 return sc;
135 error:
136 isl_union_set_free(domain);
137 return NULL;
140 /* Replace the validity constraints of "sc" by "validity".
142 __isl_give isl_schedule_constraints *isl_schedule_constraints_set_validity(
143 __isl_take isl_schedule_constraints *sc,
144 __isl_take isl_union_map *validity)
146 if (!sc || !validity)
147 goto error;
149 isl_union_map_free(sc->constraint[isl_edge_validity]);
150 sc->constraint[isl_edge_validity] = validity;
152 return sc;
153 error:
154 isl_schedule_constraints_free(sc);
155 isl_union_map_free(validity);
156 return NULL;
159 /* Replace the coincidence constraints of "sc" by "coincidence".
161 __isl_give isl_schedule_constraints *isl_schedule_constraints_set_coincidence(
162 __isl_take isl_schedule_constraints *sc,
163 __isl_take isl_union_map *coincidence)
165 if (!sc || !coincidence)
166 goto error;
168 isl_union_map_free(sc->constraint[isl_edge_coincidence]);
169 sc->constraint[isl_edge_coincidence] = coincidence;
171 return sc;
172 error:
173 isl_schedule_constraints_free(sc);
174 isl_union_map_free(coincidence);
175 return NULL;
178 /* Replace the proximity constraints of "sc" by "proximity".
180 __isl_give isl_schedule_constraints *isl_schedule_constraints_set_proximity(
181 __isl_take isl_schedule_constraints *sc,
182 __isl_take isl_union_map *proximity)
184 if (!sc || !proximity)
185 goto error;
187 isl_union_map_free(sc->constraint[isl_edge_proximity]);
188 sc->constraint[isl_edge_proximity] = proximity;
190 return sc;
191 error:
192 isl_schedule_constraints_free(sc);
193 isl_union_map_free(proximity);
194 return NULL;
197 /* Replace the conditional validity constraints of "sc" by "condition"
198 * and "validity".
200 __isl_give isl_schedule_constraints *
201 isl_schedule_constraints_set_conditional_validity(
202 __isl_take isl_schedule_constraints *sc,
203 __isl_take isl_union_map *condition,
204 __isl_take isl_union_map *validity)
206 if (!sc || !condition || !validity)
207 goto error;
209 isl_union_map_free(sc->constraint[isl_edge_condition]);
210 sc->constraint[isl_edge_condition] = condition;
211 isl_union_map_free(sc->constraint[isl_edge_conditional_validity]);
212 sc->constraint[isl_edge_conditional_validity] = validity;
214 return sc;
215 error:
216 isl_schedule_constraints_free(sc);
217 isl_union_map_free(condition);
218 isl_union_map_free(validity);
219 return NULL;
222 __isl_null isl_schedule_constraints *isl_schedule_constraints_free(
223 __isl_take isl_schedule_constraints *sc)
225 enum isl_edge_type i;
227 if (!sc)
228 return NULL;
230 isl_union_set_free(sc->domain);
231 for (i = isl_edge_first; i <= isl_edge_last; ++i)
232 isl_union_map_free(sc->constraint[i]);
234 free(sc);
236 return NULL;
239 isl_ctx *isl_schedule_constraints_get_ctx(
240 __isl_keep isl_schedule_constraints *sc)
242 return sc ? isl_union_set_get_ctx(sc->domain) : NULL;
245 void isl_schedule_constraints_dump(__isl_keep isl_schedule_constraints *sc)
247 if (!sc)
248 return;
250 fprintf(stderr, "domain: ");
251 isl_union_set_dump(sc->domain);
252 fprintf(stderr, "validity: ");
253 isl_union_map_dump(sc->constraint[isl_edge_validity]);
254 fprintf(stderr, "proximity: ");
255 isl_union_map_dump(sc->constraint[isl_edge_proximity]);
256 fprintf(stderr, "coincidence: ");
257 isl_union_map_dump(sc->constraint[isl_edge_coincidence]);
258 fprintf(stderr, "condition: ");
259 isl_union_map_dump(sc->constraint[isl_edge_condition]);
260 fprintf(stderr, "conditional_validity: ");
261 isl_union_map_dump(sc->constraint[isl_edge_conditional_validity]);
264 /* Align the parameters of the fields of "sc".
266 static __isl_give isl_schedule_constraints *
267 isl_schedule_constraints_align_params(__isl_take isl_schedule_constraints *sc)
269 isl_space *space;
270 enum isl_edge_type i;
272 if (!sc)
273 return NULL;
275 space = isl_union_set_get_space(sc->domain);
276 for (i = isl_edge_first; i <= isl_edge_last; ++i)
277 space = isl_space_align_params(space,
278 isl_union_map_get_space(sc->constraint[i]));
280 for (i = isl_edge_first; i <= isl_edge_last; ++i) {
281 sc->constraint[i] = isl_union_map_align_params(
282 sc->constraint[i], isl_space_copy(space));
283 if (!sc->constraint[i])
284 space = isl_space_free(space);
286 sc->domain = isl_union_set_align_params(sc->domain, space);
287 if (!sc->domain)
288 return isl_schedule_constraints_free(sc);
290 return sc;
293 /* Return the total number of isl_maps in the constraints of "sc".
295 static __isl_give int isl_schedule_constraints_n_map(
296 __isl_keep isl_schedule_constraints *sc)
298 enum isl_edge_type i;
299 int n = 0;
301 for (i = isl_edge_first; i <= isl_edge_last; ++i)
302 n += isl_union_map_n_map(sc->constraint[i]);
304 return n;
307 /* Internal information about a node that is used during the construction
308 * of a schedule.
309 * space represents the space in which the domain lives
310 * sched is a matrix representation of the schedule being constructed
311 * for this node; if compressed is set, then this schedule is
312 * defined over the compressed domain space
313 * sched_map is an isl_map representation of the same (partial) schedule
314 * sched_map may be NULL; if compressed is set, then this map
315 * is defined over the uncompressed domain space
316 * rank is the number of linearly independent rows in the linear part
317 * of sched
318 * the columns of cmap represent a change of basis for the schedule
319 * coefficients; the first rank columns span the linear part of
320 * the schedule rows
321 * cinv is the inverse of cmap.
322 * start is the first variable in the LP problem in the sequences that
323 * represents the schedule coefficients of this node
324 * nvar is the dimension of the domain
325 * nparam is the number of parameters or 0 if we are not constructing
326 * a parametric schedule
328 * If compressed is set, then hull represents the constraints
329 * that were used to derive the compression, while compress and
330 * decompress map the original space to the compressed space and
331 * vice versa.
333 * scc is the index of SCC (or WCC) this node belongs to
335 * band contains the band index for each of the rows of the schedule.
336 * band_id is used to differentiate between separate bands at the same
337 * level within the same parent band, i.e., bands that are separated
338 * by the parent band or bands that are independent of each other.
339 * coincident contains a boolean for each of the rows of the schedule,
340 * indicating whether the corresponding scheduling dimension satisfies
341 * the coincidence constraints in the sense that the corresponding
342 * dependence distances are zero.
344 struct isl_sched_node {
345 isl_space *space;
346 int compressed;
347 isl_set *hull;
348 isl_multi_aff *compress;
349 isl_multi_aff *decompress;
350 isl_mat *sched;
351 isl_map *sched_map;
352 int rank;
353 isl_mat *cmap;
354 isl_mat *cinv;
355 int start;
356 int nvar;
357 int nparam;
359 int scc;
361 int *band;
362 int *band_id;
363 int *coincident;
366 static int node_has_space(const void *entry, const void *val)
368 struct isl_sched_node *node = (struct isl_sched_node *)entry;
369 isl_space *dim = (isl_space *)val;
371 return isl_space_is_equal(node->space, dim);
374 /* An edge in the dependence graph. An edge may be used to
375 * ensure validity of the generated schedule, to minimize the dependence
376 * distance or both
378 * map is the dependence relation, with i -> j in the map if j depends on i
379 * tagged_condition and tagged_validity contain the union of all tagged
380 * condition or conditional validity dependence relations that
381 * specialize the dependence relation "map"; that is,
382 * if (i -> a) -> (j -> b) is an element of "tagged_condition"
383 * or "tagged_validity", then i -> j is an element of "map".
384 * If these fields are NULL, then they represent the empty relation.
385 * src is the source node
386 * dst is the sink node
387 * validity is set if the edge is used to ensure correctness
388 * coincidence is used to enforce zero dependence distances
389 * proximity is set if the edge is used to minimize dependence distances
390 * condition is set if the edge represents a condition
391 * for a conditional validity schedule constraint
392 * local can only be set for condition edges and indicates that
393 * the dependence distance over the edge should be zero
394 * conditional_validity is set if the edge is used to conditionally
395 * ensure correctness
397 * For validity edges, start and end mark the sequence of inequality
398 * constraints in the LP problem that encode the validity constraint
399 * corresponding to this edge.
401 struct isl_sched_edge {
402 isl_map *map;
403 isl_union_map *tagged_condition;
404 isl_union_map *tagged_validity;
406 struct isl_sched_node *src;
407 struct isl_sched_node *dst;
409 unsigned validity : 1;
410 unsigned coincidence : 1;
411 unsigned proximity : 1;
412 unsigned local : 1;
413 unsigned condition : 1;
414 unsigned conditional_validity : 1;
416 int start;
417 int end;
420 /* Internal information about the dependence graph used during
421 * the construction of the schedule.
423 * intra_hmap is a cache, mapping dependence relations to their dual,
424 * for dependences from a node to itself
425 * inter_hmap is a cache, mapping dependence relations to their dual,
426 * for dependences between distinct nodes
427 * if compression is involved then the key for these maps
428 * it the original, uncompressed dependence relation, while
429 * the value is the dual of the compressed dependence relation.
431 * n is the number of nodes
432 * node is the list of nodes
433 * maxvar is the maximal number of variables over all nodes
434 * max_row is the allocated number of rows in the schedule
435 * n_row is the current (maximal) number of linearly independent
436 * rows in the node schedules
437 * n_total_row is the current number of rows in the node schedules
438 * n_band is the current number of completed bands
439 * band_start is the starting row in the node schedules of the current band
440 * root is set if this graph is the original dependence graph,
441 * without any splitting
443 * sorted contains a list of node indices sorted according to the
444 * SCC to which a node belongs
446 * n_edge is the number of edges
447 * edge is the list of edges
448 * max_edge contains the maximal number of edges of each type;
449 * in particular, it contains the number of edges in the inital graph.
450 * edge_table contains pointers into the edge array, hashed on the source
451 * and sink spaces; there is one such table for each type;
452 * a given edge may be referenced from more than one table
453 * if the corresponding relation appears in more than of the
454 * sets of dependences
456 * node_table contains pointers into the node array, hashed on the space
458 * region contains a list of variable sequences that should be non-trivial
460 * lp contains the (I)LP problem used to obtain new schedule rows
462 * src_scc and dst_scc are the source and sink SCCs of an edge with
463 * conflicting constraints
465 * scc represents the number of components
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;
501 /* Initialize node_table based on the list of nodes.
503 static int graph_init_table(isl_ctx *ctx, struct isl_sched_graph *graph)
505 int i;
507 graph->node_table = isl_hash_table_alloc(ctx, graph->n);
508 if (!graph->node_table)
509 return -1;
511 for (i = 0; i < graph->n; ++i) {
512 struct isl_hash_table_entry *entry;
513 uint32_t hash;
515 hash = isl_space_get_hash(graph->node[i].space);
516 entry = isl_hash_table_find(ctx, graph->node_table, hash,
517 &node_has_space,
518 graph->node[i].space, 1);
519 if (!entry)
520 return -1;
521 entry->data = &graph->node[i];
524 return 0;
527 /* Return a pointer to the node that lives within the given space,
528 * or NULL if there is no such node.
530 static struct isl_sched_node *graph_find_node(isl_ctx *ctx,
531 struct isl_sched_graph *graph, __isl_keep isl_space *dim)
533 struct isl_hash_table_entry *entry;
534 uint32_t hash;
536 hash = isl_space_get_hash(dim);
537 entry = isl_hash_table_find(ctx, graph->node_table, hash,
538 &node_has_space, dim, 0);
540 return entry ? entry->data : NULL;
543 static int edge_has_src_and_dst(const void *entry, const void *val)
545 const struct isl_sched_edge *edge = entry;
546 const struct isl_sched_edge *temp = val;
548 return edge->src == temp->src && edge->dst == temp->dst;
551 /* Add the given edge to graph->edge_table[type].
553 static int graph_edge_table_add(isl_ctx *ctx, struct isl_sched_graph *graph,
554 enum isl_edge_type type, struct isl_sched_edge *edge)
556 struct isl_hash_table_entry *entry;
557 uint32_t hash;
559 hash = isl_hash_init();
560 hash = isl_hash_builtin(hash, edge->src);
561 hash = isl_hash_builtin(hash, edge->dst);
562 entry = isl_hash_table_find(ctx, graph->edge_table[type], hash,
563 &edge_has_src_and_dst, edge, 1);
564 if (!entry)
565 return -1;
566 entry->data = edge;
568 return 0;
571 /* Allocate the edge_tables based on the maximal number of edges of
572 * each type.
574 static int graph_init_edge_tables(isl_ctx *ctx, struct isl_sched_graph *graph)
576 int i;
578 for (i = 0; i <= isl_edge_last; ++i) {
579 graph->edge_table[i] = isl_hash_table_alloc(ctx,
580 graph->max_edge[i]);
581 if (!graph->edge_table[i])
582 return -1;
585 return 0;
588 /* If graph->edge_table[type] contains an edge from the given source
589 * to the given destination, then return the hash table entry of this edge.
590 * Otherwise, return NULL.
592 static struct isl_hash_table_entry *graph_find_edge_entry(
593 struct isl_sched_graph *graph,
594 enum isl_edge_type type,
595 struct isl_sched_node *src, struct isl_sched_node *dst)
597 isl_ctx *ctx = isl_space_get_ctx(src->space);
598 uint32_t hash;
599 struct isl_sched_edge temp = { .src = src, .dst = dst };
601 hash = isl_hash_init();
602 hash = isl_hash_builtin(hash, temp.src);
603 hash = isl_hash_builtin(hash, temp.dst);
604 return isl_hash_table_find(ctx, graph->edge_table[type], hash,
605 &edge_has_src_and_dst, &temp, 0);
609 /* If graph->edge_table[type] contains an edge from the given source
610 * to the given destination, then return this edge.
611 * Otherwise, return NULL.
613 static struct isl_sched_edge *graph_find_edge(struct isl_sched_graph *graph,
614 enum isl_edge_type type,
615 struct isl_sched_node *src, struct isl_sched_node *dst)
617 struct isl_hash_table_entry *entry;
619 entry = graph_find_edge_entry(graph, type, src, dst);
620 if (!entry)
621 return NULL;
623 return entry->data;
626 /* Check whether the dependence graph has an edge of the given type
627 * between the given two nodes.
629 static int graph_has_edge(struct isl_sched_graph *graph,
630 enum isl_edge_type type,
631 struct isl_sched_node *src, struct isl_sched_node *dst)
633 struct isl_sched_edge *edge;
634 int empty;
636 edge = graph_find_edge(graph, type, src, dst);
637 if (!edge)
638 return 0;
640 empty = isl_map_plain_is_empty(edge->map);
641 if (empty < 0)
642 return -1;
644 return !empty;
647 /* Look for any edge with the same src, dst and map fields as "model".
649 * Return the matching edge if one can be found.
650 * Return "model" if no matching edge is found.
651 * Return NULL on error.
653 static struct isl_sched_edge *graph_find_matching_edge(
654 struct isl_sched_graph *graph, struct isl_sched_edge *model)
656 enum isl_edge_type i;
657 struct isl_sched_edge *edge;
659 for (i = isl_edge_first; i <= isl_edge_last; ++i) {
660 int is_equal;
662 edge = graph_find_edge(graph, i, model->src, model->dst);
663 if (!edge)
664 continue;
665 is_equal = isl_map_plain_is_equal(model->map, edge->map);
666 if (is_equal < 0)
667 return NULL;
668 if (is_equal)
669 return edge;
672 return model;
675 /* Remove the given edge from all the edge_tables that refer to it.
677 static void graph_remove_edge(struct isl_sched_graph *graph,
678 struct isl_sched_edge *edge)
680 isl_ctx *ctx = isl_map_get_ctx(edge->map);
681 enum isl_edge_type i;
683 for (i = isl_edge_first; i <= isl_edge_last; ++i) {
684 struct isl_hash_table_entry *entry;
686 entry = graph_find_edge_entry(graph, i, edge->src, edge->dst);
687 if (!entry)
688 continue;
689 if (entry->data != edge)
690 continue;
691 isl_hash_table_remove(ctx, graph->edge_table[i], entry);
695 /* Check whether the dependence graph has any edge
696 * between the given two nodes.
698 static int graph_has_any_edge(struct isl_sched_graph *graph,
699 struct isl_sched_node *src, struct isl_sched_node *dst)
701 enum isl_edge_type i;
702 int r;
704 for (i = isl_edge_first; i <= isl_edge_last; ++i) {
705 r = graph_has_edge(graph, i, src, dst);
706 if (r < 0 || r)
707 return r;
710 return r;
713 /* Check whether the dependence graph has a validity edge
714 * between the given two nodes.
716 * Conditional validity edges are essentially validity edges that
717 * can be ignored if the corresponding condition edges are iteration private.
718 * Here, we are only checking for the presence of validity
719 * edges, so we need to consider the conditional validity edges too.
720 * In particular, this function is used during the detection
721 * of strongly connected components and we cannot ignore
722 * conditional validity edges during this detection.
724 static int graph_has_validity_edge(struct isl_sched_graph *graph,
725 struct isl_sched_node *src, struct isl_sched_node *dst)
727 int r;
729 r = graph_has_edge(graph, isl_edge_validity, src, dst);
730 if (r < 0 || r)
731 return r;
733 return graph_has_edge(graph, isl_edge_conditional_validity, src, dst);
736 static int graph_alloc(isl_ctx *ctx, struct isl_sched_graph *graph,
737 int n_node, int n_edge)
739 int i;
741 graph->n = n_node;
742 graph->n_edge = n_edge;
743 graph->node = isl_calloc_array(ctx, struct isl_sched_node, graph->n);
744 graph->sorted = isl_calloc_array(ctx, int, graph->n);
745 graph->region = isl_alloc_array(ctx, struct isl_region, graph->n);
746 graph->edge = isl_calloc_array(ctx,
747 struct isl_sched_edge, graph->n_edge);
749 graph->intra_hmap = isl_map_to_basic_set_alloc(ctx, 2 * n_edge);
750 graph->inter_hmap = isl_map_to_basic_set_alloc(ctx, 2 * n_edge);
752 if (!graph->node || !graph->region || (graph->n_edge && !graph->edge) ||
753 !graph->sorted)
754 return -1;
756 for(i = 0; i < graph->n; ++i)
757 graph->sorted[i] = i;
759 return 0;
762 static void graph_free(isl_ctx *ctx, struct isl_sched_graph *graph)
764 int i;
766 isl_map_to_basic_set_free(graph->intra_hmap);
767 isl_map_to_basic_set_free(graph->inter_hmap);
769 if (graph->node)
770 for (i = 0; i < graph->n; ++i) {
771 isl_space_free(graph->node[i].space);
772 isl_set_free(graph->node[i].hull);
773 isl_multi_aff_free(graph->node[i].compress);
774 isl_multi_aff_free(graph->node[i].decompress);
775 isl_mat_free(graph->node[i].sched);
776 isl_map_free(graph->node[i].sched_map);
777 isl_mat_free(graph->node[i].cmap);
778 isl_mat_free(graph->node[i].cinv);
779 if (graph->root) {
780 free(graph->node[i].band);
781 free(graph->node[i].band_id);
782 free(graph->node[i].coincident);
785 free(graph->node);
786 free(graph->sorted);
787 if (graph->edge)
788 for (i = 0; i < graph->n_edge; ++i) {
789 isl_map_free(graph->edge[i].map);
790 isl_union_map_free(graph->edge[i].tagged_condition);
791 isl_union_map_free(graph->edge[i].tagged_validity);
793 free(graph->edge);
794 free(graph->region);
795 for (i = 0; i <= isl_edge_last; ++i)
796 isl_hash_table_free(ctx, graph->edge_table[i]);
797 isl_hash_table_free(ctx, graph->node_table);
798 isl_basic_set_free(graph->lp);
801 /* For each "set" on which this function is called, increment
802 * graph->n by one and update graph->maxvar.
804 static int init_n_maxvar(__isl_take isl_set *set, void *user)
806 struct isl_sched_graph *graph = user;
807 int nvar = isl_set_dim(set, isl_dim_set);
809 graph->n++;
810 if (nvar > graph->maxvar)
811 graph->maxvar = nvar;
813 isl_set_free(set);
815 return 0;
818 /* Add the number of basic maps in "map" to *n.
820 static int add_n_basic_map(__isl_take isl_map *map, void *user)
822 int *n = user;
824 *n += isl_map_n_basic_map(map);
825 isl_map_free(map);
827 return 0;
830 /* Compute the number of rows that should be allocated for the schedule.
831 * The graph can be split at most "n - 1" times, there can be at most
832 * one row for each dimension in the iteration domains plus two rows
833 * for each basic map in the dependences (in particular,
834 * we usually have one row, but it may be split by split_scaled),
835 * and there can be one extra row for ordering the statements.
836 * Note that if we have actually split "n - 1" times, then no ordering
837 * is needed, so in principle we could use "graph->n + 2 * graph->maxvar - 1".
838 * It is also practically impossible to exhaust both the number of dependences
839 * and the number of variables.
841 static int compute_max_row(struct isl_sched_graph *graph,
842 __isl_keep isl_schedule_constraints *sc)
844 enum isl_edge_type i;
845 int n_edge;
847 graph->n = 0;
848 graph->maxvar = 0;
849 if (isl_union_set_foreach_set(sc->domain, &init_n_maxvar, graph) < 0)
850 return -1;
851 n_edge = 0;
852 for (i = isl_edge_first; i <= isl_edge_last; ++i)
853 if (isl_union_map_foreach_map(sc->constraint[i],
854 &add_n_basic_map, &n_edge) < 0)
855 return -1;
856 graph->max_row = graph->n + 2 * n_edge + graph->maxvar;
858 return 0;
861 /* Does "bset" have any defining equalities for its set variables?
863 static int has_any_defining_equality(__isl_keep isl_basic_set *bset)
865 int i, n;
867 if (!bset)
868 return -1;
870 n = isl_basic_set_dim(bset, isl_dim_set);
871 for (i = 0; i < n; ++i) {
872 int has;
874 has = isl_basic_set_has_defining_equality(bset, isl_dim_set, i,
875 NULL);
876 if (has < 0 || has)
877 return has;
880 return 0;
883 /* Add a new node to the graph representing the given space.
884 * "nvar" is the (possibly compressed) number of variables and
885 * may be smaller than then number of set variables in "space"
886 * if "compressed" is set.
887 * If "compressed" is set, then "hull" represents the constraints
888 * that were used to derive the compression, while "compress" and
889 * "decompress" map the original space to the compressed space and
890 * vice versa.
891 * If "compressed" is not set, then "hull", "compress" and "decompress"
892 * should be NULL.
894 static int add_node(struct isl_sched_graph *graph, __isl_take isl_space *space,
895 int nvar, int compressed, __isl_take isl_set *hull,
896 __isl_take isl_multi_aff *compress,
897 __isl_take isl_multi_aff *decompress)
899 int nparam;
900 isl_ctx *ctx;
901 isl_mat *sched;
902 int *band, *band_id, *coincident;
904 if (!space)
905 return -1;
907 ctx = isl_space_get_ctx(space);
908 nparam = isl_space_dim(space, isl_dim_param);
909 if (!ctx->opt->schedule_parametric)
910 nparam = 0;
911 sched = isl_mat_alloc(ctx, 0, 1 + nparam + nvar);
912 graph->node[graph->n].space = space;
913 graph->node[graph->n].nvar = nvar;
914 graph->node[graph->n].nparam = nparam;
915 graph->node[graph->n].sched = sched;
916 graph->node[graph->n].sched_map = NULL;
917 band = isl_alloc_array(ctx, int, graph->max_row);
918 graph->node[graph->n].band = band;
919 band_id = isl_calloc_array(ctx, int, graph->max_row);
920 graph->node[graph->n].band_id = band_id;
921 coincident = isl_calloc_array(ctx, int, graph->max_row);
922 graph->node[graph->n].coincident = coincident;
923 graph->node[graph->n].compressed = compressed;
924 graph->node[graph->n].hull = hull;
925 graph->node[graph->n].compress = compress;
926 graph->node[graph->n].decompress = decompress;
927 graph->n++;
929 if (!space || !sched ||
930 (graph->max_row && (!band || !band_id || !coincident)))
931 return -1;
932 if (compressed && (!hull || !compress || !decompress))
933 return -1;
935 return 0;
938 /* Add a new node to the graph representing the given set.
940 * If any of the set variables is defined by an equality, then
941 * we perform variable compression such that we can perform
942 * the scheduling on the compressed domain.
944 static int extract_node(__isl_take isl_set *set, void *user)
946 int nvar;
947 int has_equality;
948 isl_space *space;
949 isl_basic_set *hull;
950 isl_set *hull_set;
951 isl_morph *morph;
952 isl_multi_aff *compress, *decompress;
953 struct isl_sched_graph *graph = user;
955 space = isl_set_get_space(set);
956 hull = isl_set_affine_hull(set);
957 hull = isl_basic_set_remove_divs(hull);
958 nvar = isl_space_dim(space, isl_dim_set);
959 has_equality = has_any_defining_equality(hull);
961 if (has_equality < 0)
962 goto error;
963 if (!has_equality) {
964 isl_basic_set_free(hull);
965 return add_node(graph, space, nvar, 0, NULL, NULL, NULL);
968 morph = isl_basic_set_variable_compression(hull, isl_dim_set);
969 nvar = isl_morph_ran_dim(morph, isl_dim_set);
970 compress = isl_morph_get_var_multi_aff(morph);
971 morph = isl_morph_inverse(morph);
972 decompress = isl_morph_get_var_multi_aff(morph);
973 isl_morph_free(morph);
975 hull_set = isl_set_from_basic_set(hull);
976 return add_node(graph, space, nvar, 1, hull_set, compress, decompress);
977 error:
978 isl_basic_set_free(hull);
979 isl_space_free(space);
980 return -1;
983 struct isl_extract_edge_data {
984 enum isl_edge_type type;
985 struct isl_sched_graph *graph;
988 /* Merge edge2 into edge1, freeing the contents of edge2.
989 * "type" is the type of the schedule constraint from which edge2 was
990 * extracted.
991 * Return 0 on success and -1 on failure.
993 * edge1 and edge2 are assumed to have the same value for the map field.
995 static int merge_edge(enum isl_edge_type type, struct isl_sched_edge *edge1,
996 struct isl_sched_edge *edge2)
998 edge1->validity |= edge2->validity;
999 edge1->coincidence |= edge2->coincidence;
1000 edge1->proximity |= edge2->proximity;
1001 edge1->condition |= edge2->condition;
1002 edge1->conditional_validity |= edge2->conditional_validity;
1003 isl_map_free(edge2->map);
1005 if (type == isl_edge_condition) {
1006 if (!edge1->tagged_condition)
1007 edge1->tagged_condition = edge2->tagged_condition;
1008 else
1009 edge1->tagged_condition =
1010 isl_union_map_union(edge1->tagged_condition,
1011 edge2->tagged_condition);
1014 if (type == isl_edge_conditional_validity) {
1015 if (!edge1->tagged_validity)
1016 edge1->tagged_validity = edge2->tagged_validity;
1017 else
1018 edge1->tagged_validity =
1019 isl_union_map_union(edge1->tagged_validity,
1020 edge2->tagged_validity);
1023 if (type == isl_edge_condition && !edge1->tagged_condition)
1024 return -1;
1025 if (type == isl_edge_conditional_validity && !edge1->tagged_validity)
1026 return -1;
1028 return 0;
1031 /* Insert dummy tags in domain and range of "map".
1033 * In particular, if "map" is of the form
1035 * A -> B
1037 * then return
1039 * [A -> dummy_tag] -> [B -> dummy_tag]
1041 * where the dummy_tags are identical and equal to any dummy tags
1042 * introduced by any other call to this function.
1044 static __isl_give isl_map *insert_dummy_tags(__isl_take isl_map *map)
1046 static char dummy;
1047 isl_ctx *ctx;
1048 isl_id *id;
1049 isl_space *space;
1050 isl_set *domain, *range;
1052 ctx = isl_map_get_ctx(map);
1054 id = isl_id_alloc(ctx, NULL, &dummy);
1055 space = isl_space_params(isl_map_get_space(map));
1056 space = isl_space_set_from_params(space);
1057 space = isl_space_set_tuple_id(space, isl_dim_set, id);
1058 space = isl_space_map_from_set(space);
1060 domain = isl_map_wrap(map);
1061 range = isl_map_wrap(isl_map_universe(space));
1062 map = isl_map_from_domain_and_range(domain, range);
1063 map = isl_map_zip(map);
1065 return map;
1068 /* Given that at least one of "src" or "dst" is compressed, return
1069 * a map between the spaces of these nodes restricted to the affine
1070 * hull that was used in the compression.
1072 static __isl_give isl_map *extract_hull(struct isl_sched_node *src,
1073 struct isl_sched_node *dst)
1075 isl_set *dom, *ran;
1077 if (src->compressed)
1078 dom = isl_set_copy(src->hull);
1079 else
1080 dom = isl_set_universe(isl_space_copy(src->space));
1081 if (dst->compressed)
1082 ran = isl_set_copy(dst->hull);
1083 else
1084 ran = isl_set_universe(isl_space_copy(dst->space));
1086 return isl_map_from_domain_and_range(dom, ran);
1089 /* Intersect the domains of the nested relations in domain and range
1090 * of "tagged" with "map".
1092 static __isl_give isl_map *map_intersect_domains(__isl_take isl_map *tagged,
1093 __isl_keep isl_map *map)
1095 isl_set *set;
1097 tagged = isl_map_zip(tagged);
1098 set = isl_map_wrap(isl_map_copy(map));
1099 tagged = isl_map_intersect_domain(tagged, set);
1100 tagged = isl_map_zip(tagged);
1101 return tagged;
1104 /* Add a new edge to the graph based on the given map
1105 * and add it to data->graph->edge_table[data->type].
1106 * If a dependence relation of a given type happens to be identical
1107 * to one of the dependence relations of a type that was added before,
1108 * then we don't create a new edge, but instead mark the original edge
1109 * as also representing a dependence of the current type.
1111 * Edges of type isl_edge_condition or isl_edge_conditional_validity
1112 * may be specified as "tagged" dependence relations. That is, "map"
1113 * may contain elements (i -> a) -> (j -> b), where i -> j denotes
1114 * the dependence on iterations and a and b are tags.
1115 * edge->map is set to the relation containing the elements i -> j,
1116 * while edge->tagged_condition and edge->tagged_validity contain
1117 * the union of all the "map" relations
1118 * for which extract_edge is called that result in the same edge->map.
1120 * If the source or the destination node is compressed, then
1121 * intersect both "map" and "tagged" with the constraints that
1122 * were used to construct the compression.
1123 * This ensures that there are no schedule constraints defined
1124 * outside of these domains, while the scheduler no longer has
1125 * any control over those outside parts.
1127 static int extract_edge(__isl_take isl_map *map, void *user)
1129 isl_ctx *ctx = isl_map_get_ctx(map);
1130 struct isl_extract_edge_data *data = user;
1131 struct isl_sched_graph *graph = data->graph;
1132 struct isl_sched_node *src, *dst;
1133 isl_space *dim;
1134 struct isl_sched_edge *edge;
1135 isl_map *tagged = NULL;
1137 if (data->type == isl_edge_condition ||
1138 data->type == isl_edge_conditional_validity) {
1139 if (isl_map_can_zip(map)) {
1140 tagged = isl_map_copy(map);
1141 map = isl_set_unwrap(isl_map_domain(isl_map_zip(map)));
1142 } else {
1143 tagged = insert_dummy_tags(isl_map_copy(map));
1147 dim = isl_space_domain(isl_map_get_space(map));
1148 src = graph_find_node(ctx, graph, dim);
1149 isl_space_free(dim);
1150 dim = isl_space_range(isl_map_get_space(map));
1151 dst = graph_find_node(ctx, graph, dim);
1152 isl_space_free(dim);
1154 if (!src || !dst) {
1155 isl_map_free(map);
1156 isl_map_free(tagged);
1157 return 0;
1160 if (src->compressed || dst->compressed) {
1161 isl_map *hull;
1162 hull = extract_hull(src, dst);
1163 if (tagged)
1164 tagged = map_intersect_domains(tagged, hull);
1165 map = isl_map_intersect(map, hull);
1168 graph->edge[graph->n_edge].src = src;
1169 graph->edge[graph->n_edge].dst = dst;
1170 graph->edge[graph->n_edge].map = map;
1171 graph->edge[graph->n_edge].validity = 0;
1172 graph->edge[graph->n_edge].coincidence = 0;
1173 graph->edge[graph->n_edge].proximity = 0;
1174 graph->edge[graph->n_edge].condition = 0;
1175 graph->edge[graph->n_edge].local = 0;
1176 graph->edge[graph->n_edge].conditional_validity = 0;
1177 graph->edge[graph->n_edge].tagged_condition = NULL;
1178 graph->edge[graph->n_edge].tagged_validity = NULL;
1179 if (data->type == isl_edge_validity)
1180 graph->edge[graph->n_edge].validity = 1;
1181 if (data->type == isl_edge_coincidence)
1182 graph->edge[graph->n_edge].coincidence = 1;
1183 if (data->type == isl_edge_proximity)
1184 graph->edge[graph->n_edge].proximity = 1;
1185 if (data->type == isl_edge_condition) {
1186 graph->edge[graph->n_edge].condition = 1;
1187 graph->edge[graph->n_edge].tagged_condition =
1188 isl_union_map_from_map(tagged);
1190 if (data->type == isl_edge_conditional_validity) {
1191 graph->edge[graph->n_edge].conditional_validity = 1;
1192 graph->edge[graph->n_edge].tagged_validity =
1193 isl_union_map_from_map(tagged);
1196 edge = graph_find_matching_edge(graph, &graph->edge[graph->n_edge]);
1197 if (!edge) {
1198 graph->n_edge++;
1199 return -1;
1201 if (edge == &graph->edge[graph->n_edge])
1202 return graph_edge_table_add(ctx, graph, data->type,
1203 &graph->edge[graph->n_edge++]);
1205 if (merge_edge(data->type, edge, &graph->edge[graph->n_edge]) < 0)
1206 return -1;
1208 return graph_edge_table_add(ctx, graph, data->type, edge);
1211 /* Check whether there is any dependence from node[j] to node[i]
1212 * or from node[i] to node[j].
1214 static int node_follows_weak(int i, int j, void *user)
1216 int f;
1217 struct isl_sched_graph *graph = user;
1219 f = graph_has_any_edge(graph, &graph->node[j], &graph->node[i]);
1220 if (f < 0 || f)
1221 return f;
1222 return graph_has_any_edge(graph, &graph->node[i], &graph->node[j]);
1225 /* Check whether there is a (conditional) validity dependence from node[j]
1226 * to node[i], forcing node[i] to follow node[j].
1228 static int node_follows_strong(int i, int j, void *user)
1230 struct isl_sched_graph *graph = user;
1232 return graph_has_validity_edge(graph, &graph->node[j], &graph->node[i]);
1235 /* Use Tarjan's algorithm for computing the strongly connected components
1236 * in the dependence graph (only validity edges).
1237 * If weak is set, we consider the graph to be undirected and
1238 * we effectively compute the (weakly) connected components.
1239 * Additionally, we also consider other edges when weak is set.
1241 static int detect_ccs(isl_ctx *ctx, struct isl_sched_graph *graph, int weak)
1243 int i, n;
1244 struct isl_tarjan_graph *g = NULL;
1246 g = isl_tarjan_graph_init(ctx, graph->n,
1247 weak ? &node_follows_weak : &node_follows_strong, graph);
1248 if (!g)
1249 return -1;
1251 graph->scc = 0;
1252 i = 0;
1253 n = graph->n;
1254 while (n) {
1255 while (g->order[i] != -1) {
1256 graph->node[g->order[i]].scc = graph->scc;
1257 --n;
1258 ++i;
1260 ++i;
1261 graph->scc++;
1264 isl_tarjan_graph_free(g);
1266 return 0;
1269 /* Apply Tarjan's algorithm to detect the strongly connected components
1270 * in the dependence graph.
1272 static int detect_sccs(isl_ctx *ctx, struct isl_sched_graph *graph)
1274 return detect_ccs(ctx, graph, 0);
1277 /* Apply Tarjan's algorithm to detect the (weakly) connected components
1278 * in the dependence graph.
1280 static int detect_wccs(isl_ctx *ctx, struct isl_sched_graph *graph)
1282 return detect_ccs(ctx, graph, 1);
1285 static int cmp_scc(const void *a, const void *b, void *data)
1287 struct isl_sched_graph *graph = data;
1288 const int *i1 = a;
1289 const int *i2 = b;
1291 return graph->node[*i1].scc - graph->node[*i2].scc;
1294 /* Sort the elements of graph->sorted according to the corresponding SCCs.
1296 static int sort_sccs(struct isl_sched_graph *graph)
1298 return isl_sort(graph->sorted, graph->n, sizeof(int), &cmp_scc, graph);
1301 /* Given a dependence relation R from "node" to itself,
1302 * construct the set of coefficients of valid constraints for elements
1303 * in that dependence relation.
1304 * In particular, the result contains tuples of coefficients
1305 * c_0, c_n, c_x such that
1307 * c_0 + c_n n + c_x y - c_x x >= 0 for each (x,y) in R
1309 * or, equivalently,
1311 * c_0 + c_n n + c_x d >= 0 for each d in delta R = { y - x | (x,y) in R }
1313 * We choose here to compute the dual of delta R.
1314 * Alternatively, we could have computed the dual of R, resulting
1315 * in a set of tuples c_0, c_n, c_x, c_y, and then
1316 * plugged in (c_0, c_n, c_x, -c_x).
1318 * If "node" has been compressed, then the dependence relation
1319 * is also compressed before the set of coefficients is computed.
1321 static __isl_give isl_basic_set *intra_coefficients(
1322 struct isl_sched_graph *graph, struct isl_sched_node *node,
1323 __isl_take isl_map *map)
1325 isl_set *delta;
1326 isl_map *key;
1327 isl_basic_set *coef;
1329 if (isl_map_to_basic_set_has(graph->intra_hmap, map))
1330 return isl_map_to_basic_set_get(graph->intra_hmap, map);
1332 key = isl_map_copy(map);
1333 if (node->compressed) {
1334 map = isl_map_preimage_domain_multi_aff(map,
1335 isl_multi_aff_copy(node->decompress));
1336 map = isl_map_preimage_range_multi_aff(map,
1337 isl_multi_aff_copy(node->decompress));
1339 delta = isl_set_remove_divs(isl_map_deltas(map));
1340 coef = isl_set_coefficients(delta);
1341 graph->intra_hmap = isl_map_to_basic_set_set(graph->intra_hmap, key,
1342 isl_basic_set_copy(coef));
1344 return coef;
1347 /* Given a dependence relation R, construct the set of coefficients
1348 * of valid constraints for elements in that dependence relation.
1349 * In particular, the result contains tuples of coefficients
1350 * c_0, c_n, c_x, c_y such that
1352 * c_0 + c_n n + c_x x + c_y y >= 0 for each (x,y) in R
1354 * If the source or destination nodes of "edge" have been compressed,
1355 * then the dependence relation is also compressed before
1356 * the set of coefficients is computed.
1358 static __isl_give isl_basic_set *inter_coefficients(
1359 struct isl_sched_graph *graph, struct isl_sched_edge *edge,
1360 __isl_take isl_map *map)
1362 isl_set *set;
1363 isl_map *key;
1364 isl_basic_set *coef;
1366 if (isl_map_to_basic_set_has(graph->inter_hmap, map))
1367 return isl_map_to_basic_set_get(graph->inter_hmap, map);
1369 key = isl_map_copy(map);
1370 if (edge->src->compressed)
1371 map = isl_map_preimage_domain_multi_aff(map,
1372 isl_multi_aff_copy(edge->src->decompress));
1373 if (edge->dst->compressed)
1374 map = isl_map_preimage_range_multi_aff(map,
1375 isl_multi_aff_copy(edge->dst->decompress));
1376 set = isl_map_wrap(isl_map_remove_divs(map));
1377 coef = isl_set_coefficients(set);
1378 graph->inter_hmap = isl_map_to_basic_set_set(graph->inter_hmap, key,
1379 isl_basic_set_copy(coef));
1381 return coef;
1384 /* Add constraints to graph->lp that force validity for the given
1385 * dependence from a node i to itself.
1386 * That is, add constraints that enforce
1388 * (c_i_0 + c_i_n n + c_i_x y) - (c_i_0 + c_i_n n + c_i_x x)
1389 * = c_i_x (y - x) >= 0
1391 * for each (x,y) in R.
1392 * We obtain general constraints on coefficients (c_0, c_n, c_x)
1393 * of valid constraints for (y - x) and then plug in (0, 0, c_i_x^+ - c_i_x^-),
1394 * where c_i_x = c_i_x^+ - c_i_x^-, with c_i_x^+ and c_i_x^- non-negative.
1395 * In graph->lp, the c_i_x^- appear before their c_i_x^+ counterpart.
1397 * Actually, we do not construct constraints for the c_i_x themselves,
1398 * but for the coefficients of c_i_x written as a linear combination
1399 * of the columns in node->cmap.
1401 static int add_intra_validity_constraints(struct isl_sched_graph *graph,
1402 struct isl_sched_edge *edge)
1404 unsigned total;
1405 isl_map *map = isl_map_copy(edge->map);
1406 isl_ctx *ctx = isl_map_get_ctx(map);
1407 isl_space *dim;
1408 isl_dim_map *dim_map;
1409 isl_basic_set *coef;
1410 struct isl_sched_node *node = edge->src;
1412 coef = intra_coefficients(graph, node, map);
1414 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
1416 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
1417 isl_space_dim(dim, isl_dim_set), isl_mat_copy(node->cmap));
1418 if (!coef)
1419 goto error;
1421 total = isl_basic_set_total_dim(graph->lp);
1422 dim_map = isl_dim_map_alloc(ctx, total);
1423 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 1, 2,
1424 isl_space_dim(dim, isl_dim_set), 1,
1425 node->nvar, -1);
1426 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 2, 2,
1427 isl_space_dim(dim, isl_dim_set), 1,
1428 node->nvar, 1);
1429 graph->lp = isl_basic_set_extend_constraints(graph->lp,
1430 coef->n_eq, coef->n_ineq);
1431 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
1432 coef, dim_map);
1433 isl_space_free(dim);
1435 return 0;
1436 error:
1437 isl_space_free(dim);
1438 return -1;
1441 /* Add constraints to graph->lp that force validity for the given
1442 * dependence from node i to node j.
1443 * That is, add constraints that enforce
1445 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x) >= 0
1447 * for each (x,y) in R.
1448 * We obtain general constraints on coefficients (c_0, c_n, c_x, c_y)
1449 * of valid constraints for R and then plug in
1450 * (c_j_0 - c_i_0, c_j_n^+ - c_j_n^- - (c_i_n^+ - c_i_n^-),
1451 * c_j_x^+ - c_j_x^- - (c_i_x^+ - c_i_x^-)),
1452 * where c_* = c_*^+ - c_*^-, with c_*^+ and c_*^- non-negative.
1453 * In graph->lp, the c_*^- appear before their c_*^+ counterpart.
1455 * Actually, we do not construct constraints for the c_*_x themselves,
1456 * but for the coefficients of c_*_x written as a linear combination
1457 * of the columns in node->cmap.
1459 static int add_inter_validity_constraints(struct isl_sched_graph *graph,
1460 struct isl_sched_edge *edge)
1462 unsigned total;
1463 isl_map *map = isl_map_copy(edge->map);
1464 isl_ctx *ctx = isl_map_get_ctx(map);
1465 isl_space *dim;
1466 isl_dim_map *dim_map;
1467 isl_basic_set *coef;
1468 struct isl_sched_node *src = edge->src;
1469 struct isl_sched_node *dst = edge->dst;
1471 coef = inter_coefficients(graph, edge, map);
1473 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
1475 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
1476 isl_space_dim(dim, isl_dim_set), isl_mat_copy(src->cmap));
1477 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
1478 isl_space_dim(dim, isl_dim_set) + src->nvar,
1479 isl_mat_copy(dst->cmap));
1480 if (!coef)
1481 goto error;
1483 total = isl_basic_set_total_dim(graph->lp);
1484 dim_map = isl_dim_map_alloc(ctx, total);
1486 isl_dim_map_range(dim_map, dst->start, 0, 0, 0, 1, 1);
1487 isl_dim_map_range(dim_map, dst->start + 1, 2, 1, 1, dst->nparam, -1);
1488 isl_dim_map_range(dim_map, dst->start + 2, 2, 1, 1, dst->nparam, 1);
1489 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 1, 2,
1490 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
1491 dst->nvar, -1);
1492 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 2, 2,
1493 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
1494 dst->nvar, 1);
1496 isl_dim_map_range(dim_map, src->start, 0, 0, 0, 1, -1);
1497 isl_dim_map_range(dim_map, src->start + 1, 2, 1, 1, src->nparam, 1);
1498 isl_dim_map_range(dim_map, src->start + 2, 2, 1, 1, src->nparam, -1);
1499 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 1, 2,
1500 isl_space_dim(dim, isl_dim_set), 1,
1501 src->nvar, 1);
1502 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 2, 2,
1503 isl_space_dim(dim, isl_dim_set), 1,
1504 src->nvar, -1);
1506 edge->start = graph->lp->n_ineq;
1507 graph->lp = isl_basic_set_extend_constraints(graph->lp,
1508 coef->n_eq, coef->n_ineq);
1509 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
1510 coef, dim_map);
1511 if (!graph->lp)
1512 goto error;
1513 isl_space_free(dim);
1514 edge->end = graph->lp->n_ineq;
1516 return 0;
1517 error:
1518 isl_space_free(dim);
1519 return -1;
1522 /* Add constraints to graph->lp that bound the dependence distance for the given
1523 * dependence from a node i to itself.
1524 * If s = 1, we add the constraint
1526 * c_i_x (y - x) <= m_0 + m_n n
1528 * or
1530 * -c_i_x (y - x) + m_0 + m_n n >= 0
1532 * for each (x,y) in R.
1533 * If s = -1, we add the constraint
1535 * -c_i_x (y - x) <= m_0 + m_n n
1537 * or
1539 * c_i_x (y - x) + m_0 + m_n n >= 0
1541 * for each (x,y) in R.
1542 * We obtain general constraints on coefficients (c_0, c_n, c_x)
1543 * of valid constraints for (y - x) and then plug in (m_0, m_n, -s * c_i_x),
1544 * with each coefficient (except m_0) represented as a pair of non-negative
1545 * coefficients.
1547 * Actually, we do not construct constraints for the c_i_x themselves,
1548 * but for the coefficients of c_i_x written as a linear combination
1549 * of the columns in node->cmap.
1552 * If "local" is set, then we add constraints
1554 * c_i_x (y - x) <= 0
1556 * or
1558 * -c_i_x (y - x) <= 0
1560 * instead, forcing the dependence distance to be (less than or) equal to 0.
1561 * That is, we plug in (0, 0, -s * c_i_x),
1562 * Note that dependences marked local are treated as validity constraints
1563 * by add_all_validity_constraints and therefore also have
1564 * their distances bounded by 0 from below.
1566 static int add_intra_proximity_constraints(struct isl_sched_graph *graph,
1567 struct isl_sched_edge *edge, int s, int local)
1569 unsigned total;
1570 unsigned nparam;
1571 isl_map *map = isl_map_copy(edge->map);
1572 isl_ctx *ctx = isl_map_get_ctx(map);
1573 isl_space *dim;
1574 isl_dim_map *dim_map;
1575 isl_basic_set *coef;
1576 struct isl_sched_node *node = edge->src;
1578 coef = intra_coefficients(graph, node, map);
1580 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
1582 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
1583 isl_space_dim(dim, isl_dim_set), isl_mat_copy(node->cmap));
1584 if (!coef)
1585 goto error;
1587 nparam = isl_space_dim(node->space, isl_dim_param);
1588 total = isl_basic_set_total_dim(graph->lp);
1589 dim_map = isl_dim_map_alloc(ctx, total);
1591 if (!local) {
1592 isl_dim_map_range(dim_map, 1, 0, 0, 0, 1, 1);
1593 isl_dim_map_range(dim_map, 4, 2, 1, 1, nparam, -1);
1594 isl_dim_map_range(dim_map, 5, 2, 1, 1, nparam, 1);
1596 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 1, 2,
1597 isl_space_dim(dim, isl_dim_set), 1,
1598 node->nvar, s);
1599 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 2, 2,
1600 isl_space_dim(dim, isl_dim_set), 1,
1601 node->nvar, -s);
1602 graph->lp = isl_basic_set_extend_constraints(graph->lp,
1603 coef->n_eq, coef->n_ineq);
1604 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
1605 coef, dim_map);
1606 isl_space_free(dim);
1608 return 0;
1609 error:
1610 isl_space_free(dim);
1611 return -1;
1614 /* Add constraints to graph->lp that bound the dependence distance for the given
1615 * dependence from node i to node j.
1616 * If s = 1, we add the constraint
1618 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x)
1619 * <= m_0 + m_n n
1621 * or
1623 * -(c_j_0 + c_j_n n + c_j_x y) + (c_i_0 + c_i_n n + c_i_x x) +
1624 * m_0 + m_n n >= 0
1626 * for each (x,y) in R.
1627 * If s = -1, we add the constraint
1629 * -((c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x))
1630 * <= m_0 + m_n n
1632 * or
1634 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x) +
1635 * m_0 + m_n n >= 0
1637 * for each (x,y) in R.
1638 * We obtain general constraints on coefficients (c_0, c_n, c_x, c_y)
1639 * of valid constraints for R and then plug in
1640 * (m_0 - s*c_j_0 + s*c_i_0, m_n - s*c_j_n + s*c_i_n,
1641 * -s*c_j_x+s*c_i_x)
1642 * with each coefficient (except m_0, c_j_0 and c_i_0)
1643 * represented as a pair of non-negative coefficients.
1645 * Actually, we do not construct constraints for the c_*_x themselves,
1646 * but for the coefficients of c_*_x written as a linear combination
1647 * of the columns in node->cmap.
1650 * If "local" is set, then we add constraints
1652 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x) <= 0
1654 * or
1656 * -((c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x)) <= 0
1658 * instead, forcing the dependence distance to be (less than or) equal to 0.
1659 * That is, we plug in
1660 * (-s*c_j_0 + s*c_i_0, -s*c_j_n + s*c_i_n, -s*c_j_x+s*c_i_x).
1661 * Note that dependences marked local are treated as validity constraints
1662 * by add_all_validity_constraints and therefore also have
1663 * their distances bounded by 0 from below.
1665 static int add_inter_proximity_constraints(struct isl_sched_graph *graph,
1666 struct isl_sched_edge *edge, int s, int local)
1668 unsigned total;
1669 unsigned nparam;
1670 isl_map *map = isl_map_copy(edge->map);
1671 isl_ctx *ctx = isl_map_get_ctx(map);
1672 isl_space *dim;
1673 isl_dim_map *dim_map;
1674 isl_basic_set *coef;
1675 struct isl_sched_node *src = edge->src;
1676 struct isl_sched_node *dst = edge->dst;
1678 coef = inter_coefficients(graph, edge, map);
1680 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
1682 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
1683 isl_space_dim(dim, isl_dim_set), isl_mat_copy(src->cmap));
1684 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
1685 isl_space_dim(dim, isl_dim_set) + src->nvar,
1686 isl_mat_copy(dst->cmap));
1687 if (!coef)
1688 goto error;
1690 nparam = isl_space_dim(src->space, isl_dim_param);
1691 total = isl_basic_set_total_dim(graph->lp);
1692 dim_map = isl_dim_map_alloc(ctx, total);
1694 if (!local) {
1695 isl_dim_map_range(dim_map, 1, 0, 0, 0, 1, 1);
1696 isl_dim_map_range(dim_map, 4, 2, 1, 1, nparam, -1);
1697 isl_dim_map_range(dim_map, 5, 2, 1, 1, nparam, 1);
1700 isl_dim_map_range(dim_map, dst->start, 0, 0, 0, 1, -s);
1701 isl_dim_map_range(dim_map, dst->start + 1, 2, 1, 1, dst->nparam, s);
1702 isl_dim_map_range(dim_map, dst->start + 2, 2, 1, 1, dst->nparam, -s);
1703 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 1, 2,
1704 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
1705 dst->nvar, s);
1706 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 2, 2,
1707 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
1708 dst->nvar, -s);
1710 isl_dim_map_range(dim_map, src->start, 0, 0, 0, 1, s);
1711 isl_dim_map_range(dim_map, src->start + 1, 2, 1, 1, src->nparam, -s);
1712 isl_dim_map_range(dim_map, src->start + 2, 2, 1, 1, src->nparam, s);
1713 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 1, 2,
1714 isl_space_dim(dim, isl_dim_set), 1,
1715 src->nvar, -s);
1716 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 2, 2,
1717 isl_space_dim(dim, isl_dim_set), 1,
1718 src->nvar, s);
1720 graph->lp = isl_basic_set_extend_constraints(graph->lp,
1721 coef->n_eq, coef->n_ineq);
1722 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
1723 coef, dim_map);
1724 isl_space_free(dim);
1726 return 0;
1727 error:
1728 isl_space_free(dim);
1729 return -1;
1732 /* Add all validity constraints to graph->lp.
1734 * An edge that is forced to be local needs to have its dependence
1735 * distances equal to zero. We take care of bounding them by 0 from below
1736 * here. add_all_proximity_constraints takes care of bounding them by 0
1737 * from above.
1739 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
1740 * Otherwise, we ignore them.
1742 static int add_all_validity_constraints(struct isl_sched_graph *graph,
1743 int use_coincidence)
1745 int i;
1747 for (i = 0; i < graph->n_edge; ++i) {
1748 struct isl_sched_edge *edge= &graph->edge[i];
1749 int local;
1751 local = edge->local || (edge->coincidence && use_coincidence);
1752 if (!edge->validity && !local)
1753 continue;
1754 if (edge->src != edge->dst)
1755 continue;
1756 if (add_intra_validity_constraints(graph, edge) < 0)
1757 return -1;
1760 for (i = 0; i < graph->n_edge; ++i) {
1761 struct isl_sched_edge *edge = &graph->edge[i];
1762 int local;
1764 local = edge->local || (edge->coincidence && use_coincidence);
1765 if (!edge->validity && !local)
1766 continue;
1767 if (edge->src == edge->dst)
1768 continue;
1769 if (add_inter_validity_constraints(graph, edge) < 0)
1770 return -1;
1773 return 0;
1776 /* Add constraints to graph->lp that bound the dependence distance
1777 * for all dependence relations.
1778 * If a given proximity dependence is identical to a validity
1779 * dependence, then the dependence distance is already bounded
1780 * from below (by zero), so we only need to bound the distance
1781 * from above. (This includes the case of "local" dependences
1782 * which are treated as validity dependence by add_all_validity_constraints.)
1783 * Otherwise, we need to bound the distance both from above and from below.
1785 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
1786 * Otherwise, we ignore them.
1788 static int add_all_proximity_constraints(struct isl_sched_graph *graph,
1789 int use_coincidence)
1791 int i;
1793 for (i = 0; i < graph->n_edge; ++i) {
1794 struct isl_sched_edge *edge= &graph->edge[i];
1795 int local;
1797 local = edge->local || (edge->coincidence && use_coincidence);
1798 if (!edge->proximity && !local)
1799 continue;
1800 if (edge->src == edge->dst &&
1801 add_intra_proximity_constraints(graph, edge, 1, local) < 0)
1802 return -1;
1803 if (edge->src != edge->dst &&
1804 add_inter_proximity_constraints(graph, edge, 1, local) < 0)
1805 return -1;
1806 if (edge->validity || local)
1807 continue;
1808 if (edge->src == edge->dst &&
1809 add_intra_proximity_constraints(graph, edge, -1, 0) < 0)
1810 return -1;
1811 if (edge->src != edge->dst &&
1812 add_inter_proximity_constraints(graph, edge, -1, 0) < 0)
1813 return -1;
1816 return 0;
1819 /* Compute a basis for the rows in the linear part of the schedule
1820 * and extend this basis to a full basis. The remaining rows
1821 * can then be used to force linear independence from the rows
1822 * in the schedule.
1824 * In particular, given the schedule rows S, we compute
1826 * S = H Q
1827 * S U = H
1829 * with H the Hermite normal form of S. That is, all but the
1830 * first rank columns of H are zero and so each row in S is
1831 * a linear combination of the first rank rows of Q.
1832 * The matrix Q is then transposed because we will write the
1833 * coefficients of the next schedule row as a column vector s
1834 * and express this s as a linear combination s = Q c of the
1835 * computed basis.
1836 * Similarly, the matrix U is transposed such that we can
1837 * compute the coefficients c = U s from a schedule row s.
1839 static int node_update_cmap(struct isl_sched_node *node)
1841 isl_mat *H, *U, *Q;
1842 int n_row = isl_mat_rows(node->sched);
1844 H = isl_mat_sub_alloc(node->sched, 0, n_row,
1845 1 + node->nparam, node->nvar);
1847 H = isl_mat_left_hermite(H, 0, &U, &Q);
1848 isl_mat_free(node->cmap);
1849 isl_mat_free(node->cinv);
1850 node->cmap = isl_mat_transpose(Q);
1851 node->cinv = isl_mat_transpose(U);
1852 node->rank = isl_mat_initial_non_zero_cols(H);
1853 isl_mat_free(H);
1855 if (!node->cmap || !node->cinv || node->rank < 0)
1856 return -1;
1857 return 0;
1860 /* How many times should we count the constraints in "edge"?
1862 * If carry is set, then we are counting the number of
1863 * (validity or conditional validity) constraints that will be added
1864 * in setup_carry_lp and we count each edge exactly once.
1866 * Otherwise, we count as follows
1867 * validity -> 1 (>= 0)
1868 * validity+proximity -> 2 (>= 0 and upper bound)
1869 * proximity -> 2 (lower and upper bound)
1870 * local(+any) -> 2 (>= 0 and <= 0)
1872 * If an edge is only marked conditional_validity then it counts
1873 * as zero since it is only checked afterwards.
1875 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
1876 * Otherwise, we ignore them.
1878 static int edge_multiplicity(struct isl_sched_edge *edge, int carry,
1879 int use_coincidence)
1881 if (carry && !edge->validity && !edge->conditional_validity)
1882 return 0;
1883 if (carry)
1884 return 1;
1885 if (edge->proximity || edge->local)
1886 return 2;
1887 if (use_coincidence && edge->coincidence)
1888 return 2;
1889 if (edge->validity)
1890 return 1;
1891 return 0;
1894 /* Count the number of equality and inequality constraints
1895 * that will be added for the given map.
1897 * "use_coincidence" is set if we should take into account coincidence edges.
1899 static int count_map_constraints(struct isl_sched_graph *graph,
1900 struct isl_sched_edge *edge, __isl_take isl_map *map,
1901 int *n_eq, int *n_ineq, int carry, int use_coincidence)
1903 isl_basic_set *coef;
1904 int f = edge_multiplicity(edge, carry, use_coincidence);
1906 if (f == 0) {
1907 isl_map_free(map);
1908 return 0;
1911 if (edge->src == edge->dst)
1912 coef = intra_coefficients(graph, edge->src, map);
1913 else
1914 coef = inter_coefficients(graph, edge, map);
1915 if (!coef)
1916 return -1;
1917 *n_eq += f * coef->n_eq;
1918 *n_ineq += f * coef->n_ineq;
1919 isl_basic_set_free(coef);
1921 return 0;
1924 /* Count the number of equality and inequality constraints
1925 * that will be added to the main lp problem.
1926 * We count as follows
1927 * validity -> 1 (>= 0)
1928 * validity+proximity -> 2 (>= 0 and upper bound)
1929 * proximity -> 2 (lower and upper bound)
1930 * local(+any) -> 2 (>= 0 and <= 0)
1932 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
1933 * Otherwise, we ignore them.
1935 static int count_constraints(struct isl_sched_graph *graph,
1936 int *n_eq, int *n_ineq, int use_coincidence)
1938 int i;
1940 *n_eq = *n_ineq = 0;
1941 for (i = 0; i < graph->n_edge; ++i) {
1942 struct isl_sched_edge *edge= &graph->edge[i];
1943 isl_map *map = isl_map_copy(edge->map);
1945 if (count_map_constraints(graph, edge, map, n_eq, n_ineq,
1946 0, use_coincidence) < 0)
1947 return -1;
1950 return 0;
1953 /* Count the number of constraints that will be added by
1954 * add_bound_coefficient_constraints and increment *n_eq and *n_ineq
1955 * accordingly.
1957 * In practice, add_bound_coefficient_constraints only adds inequalities.
1959 static int count_bound_coefficient_constraints(isl_ctx *ctx,
1960 struct isl_sched_graph *graph, int *n_eq, int *n_ineq)
1962 int i;
1964 if (ctx->opt->schedule_max_coefficient == -1)
1965 return 0;
1967 for (i = 0; i < graph->n; ++i)
1968 *n_ineq += 2 * graph->node[i].nparam + 2 * graph->node[i].nvar;
1970 return 0;
1973 /* Add constraints that bound the values of the variable and parameter
1974 * coefficients of the schedule.
1976 * The maximal value of the coefficients is defined by the option
1977 * 'schedule_max_coefficient'.
1979 static int add_bound_coefficient_constraints(isl_ctx *ctx,
1980 struct isl_sched_graph *graph)
1982 int i, j, k;
1983 int max_coefficient;
1984 int total;
1986 max_coefficient = ctx->opt->schedule_max_coefficient;
1988 if (max_coefficient == -1)
1989 return 0;
1991 total = isl_basic_set_total_dim(graph->lp);
1993 for (i = 0; i < graph->n; ++i) {
1994 struct isl_sched_node *node = &graph->node[i];
1995 for (j = 0; j < 2 * node->nparam + 2 * node->nvar; ++j) {
1996 int dim;
1997 k = isl_basic_set_alloc_inequality(graph->lp);
1998 if (k < 0)
1999 return -1;
2000 dim = 1 + node->start + 1 + j;
2001 isl_seq_clr(graph->lp->ineq[k], 1 + total);
2002 isl_int_set_si(graph->lp->ineq[k][dim], -1);
2003 isl_int_set_si(graph->lp->ineq[k][0], max_coefficient);
2007 return 0;
2010 /* Construct an ILP problem for finding schedule coefficients
2011 * that result in non-negative, but small dependence distances
2012 * over all dependences.
2013 * In particular, the dependence distances over proximity edges
2014 * are bounded by m_0 + m_n n and we compute schedule coefficients
2015 * with small values (preferably zero) of m_n and m_0.
2017 * All variables of the ILP are non-negative. The actual coefficients
2018 * may be negative, so each coefficient is represented as the difference
2019 * of two non-negative variables. The negative part always appears
2020 * immediately before the positive part.
2021 * Other than that, the variables have the following order
2023 * - sum of positive and negative parts of m_n coefficients
2024 * - m_0
2025 * - sum of positive and negative parts of all c_n coefficients
2026 * (unconstrained when computing non-parametric schedules)
2027 * - sum of positive and negative parts of all c_x coefficients
2028 * - positive and negative parts of m_n coefficients
2029 * - for each node
2030 * - c_i_0
2031 * - positive and negative parts of c_i_n (if parametric)
2032 * - positive and negative parts of c_i_x
2034 * The c_i_x are not represented directly, but through the columns of
2035 * node->cmap. That is, the computed values are for variable t_i_x
2036 * such that c_i_x = Q t_i_x with Q equal to node->cmap.
2038 * The constraints are those from the edges plus two or three equalities
2039 * to express the sums.
2041 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
2042 * Otherwise, we ignore them.
2044 static int setup_lp(isl_ctx *ctx, struct isl_sched_graph *graph,
2045 int use_coincidence)
2047 int i, j;
2048 int k;
2049 unsigned nparam;
2050 unsigned total;
2051 isl_space *dim;
2052 int parametric;
2053 int param_pos;
2054 int n_eq, n_ineq;
2055 int max_constant_term;
2057 max_constant_term = ctx->opt->schedule_max_constant_term;
2059 parametric = ctx->opt->schedule_parametric;
2060 nparam = isl_space_dim(graph->node[0].space, isl_dim_param);
2061 param_pos = 4;
2062 total = param_pos + 2 * nparam;
2063 for (i = 0; i < graph->n; ++i) {
2064 struct isl_sched_node *node = &graph->node[graph->sorted[i]];
2065 if (node_update_cmap(node) < 0)
2066 return -1;
2067 node->start = total;
2068 total += 1 + 2 * (node->nparam + node->nvar);
2071 if (count_constraints(graph, &n_eq, &n_ineq, use_coincidence) < 0)
2072 return -1;
2073 if (count_bound_coefficient_constraints(ctx, graph, &n_eq, &n_ineq) < 0)
2074 return -1;
2076 dim = isl_space_set_alloc(ctx, 0, total);
2077 isl_basic_set_free(graph->lp);
2078 n_eq += 2 + parametric;
2079 if (max_constant_term != -1)
2080 n_ineq += graph->n;
2082 graph->lp = isl_basic_set_alloc_space(dim, 0, n_eq, n_ineq);
2084 k = isl_basic_set_alloc_equality(graph->lp);
2085 if (k < 0)
2086 return -1;
2087 isl_seq_clr(graph->lp->eq[k], 1 + total);
2088 isl_int_set_si(graph->lp->eq[k][1], -1);
2089 for (i = 0; i < 2 * nparam; ++i)
2090 isl_int_set_si(graph->lp->eq[k][1 + param_pos + i], 1);
2092 if (parametric) {
2093 k = isl_basic_set_alloc_equality(graph->lp);
2094 if (k < 0)
2095 return -1;
2096 isl_seq_clr(graph->lp->eq[k], 1 + total);
2097 isl_int_set_si(graph->lp->eq[k][3], -1);
2098 for (i = 0; i < graph->n; ++i) {
2099 int pos = 1 + graph->node[i].start + 1;
2101 for (j = 0; j < 2 * graph->node[i].nparam; ++j)
2102 isl_int_set_si(graph->lp->eq[k][pos + j], 1);
2106 k = isl_basic_set_alloc_equality(graph->lp);
2107 if (k < 0)
2108 return -1;
2109 isl_seq_clr(graph->lp->eq[k], 1 + total);
2110 isl_int_set_si(graph->lp->eq[k][4], -1);
2111 for (i = 0; i < graph->n; ++i) {
2112 struct isl_sched_node *node = &graph->node[i];
2113 int pos = 1 + node->start + 1 + 2 * node->nparam;
2115 for (j = 0; j < 2 * node->nvar; ++j)
2116 isl_int_set_si(graph->lp->eq[k][pos + j], 1);
2119 if (max_constant_term != -1)
2120 for (i = 0; i < graph->n; ++i) {
2121 struct isl_sched_node *node = &graph->node[i];
2122 k = isl_basic_set_alloc_inequality(graph->lp);
2123 if (k < 0)
2124 return -1;
2125 isl_seq_clr(graph->lp->ineq[k], 1 + total);
2126 isl_int_set_si(graph->lp->ineq[k][1 + node->start], -1);
2127 isl_int_set_si(graph->lp->ineq[k][0], max_constant_term);
2130 if (add_bound_coefficient_constraints(ctx, graph) < 0)
2131 return -1;
2132 if (add_all_validity_constraints(graph, use_coincidence) < 0)
2133 return -1;
2134 if (add_all_proximity_constraints(graph, use_coincidence) < 0)
2135 return -1;
2137 return 0;
2140 /* Analyze the conflicting constraint found by
2141 * isl_tab_basic_set_non_trivial_lexmin. If it corresponds to the validity
2142 * constraint of one of the edges between distinct nodes, living, moreover
2143 * in distinct SCCs, then record the source and sink SCC as this may
2144 * be a good place to cut between SCCs.
2146 static int check_conflict(int con, void *user)
2148 int i;
2149 struct isl_sched_graph *graph = user;
2151 if (graph->src_scc >= 0)
2152 return 0;
2154 con -= graph->lp->n_eq;
2156 if (con >= graph->lp->n_ineq)
2157 return 0;
2159 for (i = 0; i < graph->n_edge; ++i) {
2160 if (!graph->edge[i].validity)
2161 continue;
2162 if (graph->edge[i].src == graph->edge[i].dst)
2163 continue;
2164 if (graph->edge[i].src->scc == graph->edge[i].dst->scc)
2165 continue;
2166 if (graph->edge[i].start > con)
2167 continue;
2168 if (graph->edge[i].end <= con)
2169 continue;
2170 graph->src_scc = graph->edge[i].src->scc;
2171 graph->dst_scc = graph->edge[i].dst->scc;
2174 return 0;
2177 /* Check whether the next schedule row of the given node needs to be
2178 * non-trivial. Lower-dimensional domains may have some trivial rows,
2179 * but as soon as the number of remaining required non-trivial rows
2180 * is as large as the number or remaining rows to be computed,
2181 * all remaining rows need to be non-trivial.
2183 static int needs_row(struct isl_sched_graph *graph, struct isl_sched_node *node)
2185 return node->nvar - node->rank >= graph->maxvar - graph->n_row;
2188 /* Solve the ILP problem constructed in setup_lp.
2189 * For each node such that all the remaining rows of its schedule
2190 * need to be non-trivial, we construct a non-triviality region.
2191 * This region imposes that the next row is independent of previous rows.
2192 * In particular the coefficients c_i_x are represented by t_i_x
2193 * variables with c_i_x = Q t_i_x and Q a unimodular matrix such that
2194 * its first columns span the rows of the previously computed part
2195 * of the schedule. The non-triviality region enforces that at least
2196 * one of the remaining components of t_i_x is non-zero, i.e.,
2197 * that the new schedule row depends on at least one of the remaining
2198 * columns of Q.
2200 static __isl_give isl_vec *solve_lp(struct isl_sched_graph *graph)
2202 int i;
2203 isl_vec *sol;
2204 isl_basic_set *lp;
2206 for (i = 0; i < graph->n; ++i) {
2207 struct isl_sched_node *node = &graph->node[i];
2208 int skip = node->rank;
2209 graph->region[i].pos = node->start + 1 + 2*(node->nparam+skip);
2210 if (needs_row(graph, node))
2211 graph->region[i].len = 2 * (node->nvar - skip);
2212 else
2213 graph->region[i].len = 0;
2215 lp = isl_basic_set_copy(graph->lp);
2216 sol = isl_tab_basic_set_non_trivial_lexmin(lp, 2, graph->n,
2217 graph->region, &check_conflict, graph);
2218 return sol;
2221 /* Update the schedules of all nodes based on the given solution
2222 * of the LP problem.
2223 * The new row is added to the current band.
2224 * All possibly negative coefficients are encoded as a difference
2225 * of two non-negative variables, so we need to perform the subtraction
2226 * here. Moreover, if use_cmap is set, then the solution does
2227 * not refer to the actual coefficients c_i_x, but instead to variables
2228 * t_i_x such that c_i_x = Q t_i_x and Q is equal to node->cmap.
2229 * In this case, we then also need to perform this multiplication
2230 * to obtain the values of c_i_x.
2232 * If coincident is set, then the caller guarantees that the new
2233 * row satisfies the coincidence constraints.
2235 static int update_schedule(struct isl_sched_graph *graph,
2236 __isl_take isl_vec *sol, int use_cmap, int coincident)
2238 int i, j;
2239 isl_vec *csol = NULL;
2241 if (!sol)
2242 goto error;
2243 if (sol->size == 0)
2244 isl_die(sol->ctx, isl_error_internal,
2245 "no solution found", goto error);
2246 if (graph->n_total_row >= graph->max_row)
2247 isl_die(sol->ctx, isl_error_internal,
2248 "too many schedule rows", goto error);
2250 for (i = 0; i < graph->n; ++i) {
2251 struct isl_sched_node *node = &graph->node[i];
2252 int pos = node->start;
2253 int row = isl_mat_rows(node->sched);
2255 isl_vec_free(csol);
2256 csol = isl_vec_alloc(sol->ctx, node->nvar);
2257 if (!csol)
2258 goto error;
2260 isl_map_free(node->sched_map);
2261 node->sched_map = NULL;
2262 node->sched = isl_mat_add_rows(node->sched, 1);
2263 if (!node->sched)
2264 goto error;
2265 node->sched = isl_mat_set_element(node->sched, row, 0,
2266 sol->el[1 + pos]);
2267 for (j = 0; j < node->nparam + node->nvar; ++j)
2268 isl_int_sub(sol->el[1 + pos + 1 + 2 * j + 1],
2269 sol->el[1 + pos + 1 + 2 * j + 1],
2270 sol->el[1 + pos + 1 + 2 * j]);
2271 for (j = 0; j < node->nparam; ++j)
2272 node->sched = isl_mat_set_element(node->sched,
2273 row, 1 + j, sol->el[1+pos+1+2*j+1]);
2274 for (j = 0; j < node->nvar; ++j)
2275 isl_int_set(csol->el[j],
2276 sol->el[1+pos+1+2*(node->nparam+j)+1]);
2277 if (use_cmap)
2278 csol = isl_mat_vec_product(isl_mat_copy(node->cmap),
2279 csol);
2280 if (!csol)
2281 goto error;
2282 for (j = 0; j < node->nvar; ++j)
2283 node->sched = isl_mat_set_element(node->sched,
2284 row, 1 + node->nparam + j, csol->el[j]);
2285 node->band[graph->n_total_row] = graph->n_band;
2286 node->coincident[graph->n_total_row] = coincident;
2288 isl_vec_free(sol);
2289 isl_vec_free(csol);
2291 graph->n_row++;
2292 graph->n_total_row++;
2294 return 0;
2295 error:
2296 isl_vec_free(sol);
2297 isl_vec_free(csol);
2298 return -1;
2301 /* Convert row "row" of node->sched into an isl_aff living in "ls"
2302 * and return this isl_aff.
2304 static __isl_give isl_aff *extract_schedule_row(__isl_take isl_local_space *ls,
2305 struct isl_sched_node *node, int row)
2307 int j;
2308 isl_int v;
2309 isl_aff *aff;
2311 isl_int_init(v);
2313 aff = isl_aff_zero_on_domain(ls);
2314 isl_mat_get_element(node->sched, row, 0, &v);
2315 aff = isl_aff_set_constant(aff, v);
2316 for (j = 0; j < node->nparam; ++j) {
2317 isl_mat_get_element(node->sched, row, 1 + j, &v);
2318 aff = isl_aff_set_coefficient(aff, isl_dim_param, j, v);
2320 for (j = 0; j < node->nvar; ++j) {
2321 isl_mat_get_element(node->sched, row, 1 + node->nparam + j, &v);
2322 aff = isl_aff_set_coefficient(aff, isl_dim_in, j, v);
2325 isl_int_clear(v);
2327 return aff;
2330 /* Convert node->sched into a multi_aff and return this multi_aff.
2332 * The result is defined over the uncompressed node domain.
2334 static __isl_give isl_multi_aff *node_extract_schedule_multi_aff(
2335 struct isl_sched_node *node)
2337 int i;
2338 isl_space *space;
2339 isl_local_space *ls;
2340 isl_aff *aff;
2341 isl_multi_aff *ma;
2342 int nrow, ncol;
2344 nrow = isl_mat_rows(node->sched);
2345 ncol = isl_mat_cols(node->sched) - 1;
2346 if (node->compressed)
2347 space = isl_multi_aff_get_domain_space(node->decompress);
2348 else
2349 space = isl_space_copy(node->space);
2350 ls = isl_local_space_from_space(isl_space_copy(space));
2351 space = isl_space_from_domain(space);
2352 space = isl_space_add_dims(space, isl_dim_out, nrow);
2353 ma = isl_multi_aff_zero(space);
2355 for (i = 0; i < nrow; ++i) {
2356 aff = extract_schedule_row(isl_local_space_copy(ls), node, i);
2357 ma = isl_multi_aff_set_aff(ma, i, aff);
2360 isl_local_space_free(ls);
2362 if (node->compressed)
2363 ma = isl_multi_aff_pullback_multi_aff(ma,
2364 isl_multi_aff_copy(node->compress));
2366 return ma;
2369 /* Convert node->sched into a map and return this map.
2371 * The result is cached in node->sched_map, which needs to be released
2372 * whenever node->sched is updated.
2373 * It is defined over the uncompressed node domain.
2375 static __isl_give isl_map *node_extract_schedule(struct isl_sched_node *node)
2377 if (!node->sched_map) {
2378 isl_multi_aff *ma;
2380 ma = node_extract_schedule_multi_aff(node);
2381 node->sched_map = isl_map_from_multi_aff(ma);
2384 return isl_map_copy(node->sched_map);
2387 /* Construct a map that can be used to update a dependence relation
2388 * based on the current schedule.
2389 * That is, construct a map expressing that source and sink
2390 * are executed within the same iteration of the current schedule.
2391 * This map can then be intersected with the dependence relation.
2392 * This is not the most efficient way, but this shouldn't be a critical
2393 * operation.
2395 static __isl_give isl_map *specializer(struct isl_sched_node *src,
2396 struct isl_sched_node *dst)
2398 isl_map *src_sched, *dst_sched;
2400 src_sched = node_extract_schedule(src);
2401 dst_sched = node_extract_schedule(dst);
2402 return isl_map_apply_range(src_sched, isl_map_reverse(dst_sched));
2405 /* Intersect the domains of the nested relations in domain and range
2406 * of "umap" with "map".
2408 static __isl_give isl_union_map *intersect_domains(
2409 __isl_take isl_union_map *umap, __isl_keep isl_map *map)
2411 isl_union_set *uset;
2413 umap = isl_union_map_zip(umap);
2414 uset = isl_union_set_from_set(isl_map_wrap(isl_map_copy(map)));
2415 umap = isl_union_map_intersect_domain(umap, uset);
2416 umap = isl_union_map_zip(umap);
2417 return umap;
2420 /* Update the dependence relation of the given edge based
2421 * on the current schedule.
2422 * If the dependence is carried completely by the current schedule, then
2423 * it is removed from the edge_tables. It is kept in the list of edges
2424 * as otherwise all edge_tables would have to be recomputed.
2426 static int update_edge(struct isl_sched_graph *graph,
2427 struct isl_sched_edge *edge)
2429 int empty;
2430 isl_map *id;
2432 id = specializer(edge->src, edge->dst);
2433 edge->map = isl_map_intersect(edge->map, isl_map_copy(id));
2434 if (!edge->map)
2435 goto error;
2437 if (edge->tagged_condition) {
2438 edge->tagged_condition =
2439 intersect_domains(edge->tagged_condition, id);
2440 if (!edge->tagged_condition)
2441 goto error;
2443 if (edge->tagged_validity) {
2444 edge->tagged_validity =
2445 intersect_domains(edge->tagged_validity, id);
2446 if (!edge->tagged_validity)
2447 goto error;
2450 empty = isl_map_plain_is_empty(edge->map);
2451 if (empty < 0)
2452 goto error;
2453 if (empty)
2454 graph_remove_edge(graph, edge);
2456 isl_map_free(id);
2457 return 0;
2458 error:
2459 isl_map_free(id);
2460 return -1;
2463 /* Does the domain of "umap" intersect "uset"?
2465 static int domain_intersects(__isl_keep isl_union_map *umap,
2466 __isl_keep isl_union_set *uset)
2468 int empty;
2470 umap = isl_union_map_copy(umap);
2471 umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(uset));
2472 empty = isl_union_map_is_empty(umap);
2473 isl_union_map_free(umap);
2475 return empty < 0 ? -1 : !empty;
2478 /* Does the range of "umap" intersect "uset"?
2480 static int range_intersects(__isl_keep isl_union_map *umap,
2481 __isl_keep isl_union_set *uset)
2483 int empty;
2485 umap = isl_union_map_copy(umap);
2486 umap = isl_union_map_intersect_range(umap, isl_union_set_copy(uset));
2487 empty = isl_union_map_is_empty(umap);
2488 isl_union_map_free(umap);
2490 return empty < 0 ? -1 : !empty;
2493 /* Are the condition dependences of "edge" local with respect to
2494 * the current schedule?
2496 * That is, are domain and range of the condition dependences mapped
2497 * to the same point?
2499 * In other words, is the condition false?
2501 static int is_condition_false(struct isl_sched_edge *edge)
2503 isl_union_map *umap;
2504 isl_map *map, *sched, *test;
2505 int empty, local;
2507 empty = isl_union_map_is_empty(edge->tagged_condition);
2508 if (empty < 0 || empty)
2509 return empty;
2511 umap = isl_union_map_copy(edge->tagged_condition);
2512 umap = isl_union_map_zip(umap);
2513 umap = isl_union_set_unwrap(isl_union_map_domain(umap));
2514 map = isl_map_from_union_map(umap);
2516 sched = node_extract_schedule(edge->src);
2517 map = isl_map_apply_domain(map, sched);
2518 sched = node_extract_schedule(edge->dst);
2519 map = isl_map_apply_range(map, sched);
2521 test = isl_map_identity(isl_map_get_space(map));
2522 local = isl_map_is_subset(map, test);
2523 isl_map_free(map);
2524 isl_map_free(test);
2526 return local;
2529 /* For each conditional validity constraint that is adjacent
2530 * to a condition with domain in condition_source or range in condition_sink,
2531 * turn it into an unconditional validity constraint.
2533 static int unconditionalize_adjacent_validity(struct isl_sched_graph *graph,
2534 __isl_take isl_union_set *condition_source,
2535 __isl_take isl_union_set *condition_sink)
2537 int i;
2539 condition_source = isl_union_set_coalesce(condition_source);
2540 condition_sink = isl_union_set_coalesce(condition_sink);
2542 for (i = 0; i < graph->n_edge; ++i) {
2543 int adjacent;
2544 isl_union_map *validity;
2546 if (!graph->edge[i].conditional_validity)
2547 continue;
2548 if (graph->edge[i].validity)
2549 continue;
2551 validity = graph->edge[i].tagged_validity;
2552 adjacent = domain_intersects(validity, condition_sink);
2553 if (adjacent >= 0 && !adjacent)
2554 adjacent = range_intersects(validity, condition_source);
2555 if (adjacent < 0)
2556 goto error;
2557 if (!adjacent)
2558 continue;
2560 graph->edge[i].validity = 1;
2563 isl_union_set_free(condition_source);
2564 isl_union_set_free(condition_sink);
2565 return 0;
2566 error:
2567 isl_union_set_free(condition_source);
2568 isl_union_set_free(condition_sink);
2569 return -1;
2572 /* Update the dependence relations of all edges based on the current schedule
2573 * and enforce conditional validity constraints that are adjacent
2574 * to satisfied condition constraints.
2576 * First check if any of the condition constraints are satisfied
2577 * (i.e., not local to the outer schedule) and keep track of
2578 * their domain and range.
2579 * Then update all dependence relations (which removes the non-local
2580 * constraints).
2581 * Finally, if any condition constraints turned out to be satisfied,
2582 * then turn all adjacent conditional validity constraints into
2583 * unconditional validity constraints.
2585 static int update_edges(isl_ctx *ctx, struct isl_sched_graph *graph)
2587 int i;
2588 int any = 0;
2589 isl_union_set *source, *sink;
2591 source = isl_union_set_empty(isl_space_params_alloc(ctx, 0));
2592 sink = isl_union_set_empty(isl_space_params_alloc(ctx, 0));
2593 for (i = 0; i < graph->n_edge; ++i) {
2594 int local;
2595 isl_union_set *uset;
2596 isl_union_map *umap;
2598 if (!graph->edge[i].condition)
2599 continue;
2600 if (graph->edge[i].local)
2601 continue;
2602 local = is_condition_false(&graph->edge[i]);
2603 if (local < 0)
2604 goto error;
2605 if (local)
2606 continue;
2608 any = 1;
2610 umap = isl_union_map_copy(graph->edge[i].tagged_condition);
2611 uset = isl_union_map_domain(umap);
2612 source = isl_union_set_union(source, uset);
2614 umap = isl_union_map_copy(graph->edge[i].tagged_condition);
2615 uset = isl_union_map_range(umap);
2616 sink = isl_union_set_union(sink, uset);
2619 for (i = graph->n_edge - 1; i >= 0; --i) {
2620 if (update_edge(graph, &graph->edge[i]) < 0)
2621 goto error;
2624 if (any)
2625 return unconditionalize_adjacent_validity(graph, source, sink);
2627 isl_union_set_free(source);
2628 isl_union_set_free(sink);
2629 return 0;
2630 error:
2631 isl_union_set_free(source);
2632 isl_union_set_free(sink);
2633 return -1;
2636 static void next_band(struct isl_sched_graph *graph)
2638 graph->band_start = graph->n_total_row;
2639 graph->n_band++;
2642 /* Construct an isl_schedule based on the computed schedule stored
2643 * in graph and with parameters specified by dim.
2645 static __isl_give isl_schedule *extract_schedule(struct isl_sched_graph *graph,
2646 __isl_take isl_space *dim)
2648 int i;
2649 isl_ctx *ctx;
2650 isl_schedule *sched = NULL;
2652 if (!dim)
2653 return NULL;
2655 ctx = isl_space_get_ctx(dim);
2656 sched = isl_calloc(ctx, struct isl_schedule,
2657 sizeof(struct isl_schedule) +
2658 (graph->n - 1) * sizeof(struct isl_schedule_node));
2659 if (!sched)
2660 goto error;
2662 sched->ref = 1;
2663 sched->n = graph->n;
2664 sched->n_band = graph->n_band;
2665 sched->n_total_row = graph->n_total_row;
2667 for (i = 0; i < sched->n; ++i) {
2668 int r, b;
2669 int *band_end, *band_id, *coincident;
2671 sched->node[i].sched =
2672 node_extract_schedule_multi_aff(&graph->node[i]);
2673 if (!sched->node[i].sched)
2674 goto error;
2676 sched->node[i].n_band = graph->n_band;
2677 if (graph->n_band == 0)
2678 continue;
2680 band_end = isl_alloc_array(ctx, int, graph->n_band);
2681 band_id = isl_alloc_array(ctx, int, graph->n_band);
2682 coincident = isl_alloc_array(ctx, int, graph->n_total_row);
2683 sched->node[i].band_end = band_end;
2684 sched->node[i].band_id = band_id;
2685 sched->node[i].coincident = coincident;
2686 if (!band_end || !band_id || !coincident)
2687 goto error;
2689 for (r = 0; r < graph->n_total_row; ++r)
2690 coincident[r] = graph->node[i].coincident[r];
2691 for (r = b = 0; r < graph->n_total_row; ++r) {
2692 if (graph->node[i].band[r] == b)
2693 continue;
2694 band_end[b++] = r;
2695 if (graph->node[i].band[r] == -1)
2696 break;
2698 if (r == graph->n_total_row)
2699 band_end[b++] = r;
2700 sched->node[i].n_band = b;
2701 for (--b; b >= 0; --b)
2702 band_id[b] = graph->node[i].band_id[b];
2705 sched->dim = dim;
2707 return sched;
2708 error:
2709 isl_space_free(dim);
2710 isl_schedule_free(sched);
2711 return NULL;
2714 /* Copy nodes that satisfy node_pred from the src dependence graph
2715 * to the dst dependence graph.
2717 static int copy_nodes(struct isl_sched_graph *dst, struct isl_sched_graph *src,
2718 int (*node_pred)(struct isl_sched_node *node, int data), int data)
2720 int i;
2722 dst->n = 0;
2723 for (i = 0; i < src->n; ++i) {
2724 int j;
2726 if (!node_pred(&src->node[i], data))
2727 continue;
2729 j = dst->n;
2730 dst->node[j].space = isl_space_copy(src->node[i].space);
2731 dst->node[j].compressed = src->node[i].compressed;
2732 dst->node[j].hull = isl_set_copy(src->node[i].hull);
2733 dst->node[j].compress =
2734 isl_multi_aff_copy(src->node[i].compress);
2735 dst->node[j].decompress =
2736 isl_multi_aff_copy(src->node[i].decompress);
2737 dst->node[j].nvar = src->node[i].nvar;
2738 dst->node[j].nparam = src->node[i].nparam;
2739 dst->node[j].sched = isl_mat_copy(src->node[i].sched);
2740 dst->node[j].sched_map = isl_map_copy(src->node[i].sched_map);
2741 dst->node[j].band = src->node[i].band;
2742 dst->node[j].band_id = src->node[i].band_id;
2743 dst->node[j].coincident = src->node[i].coincident;
2744 dst->n++;
2746 if (!dst->node[j].space || !dst->node[j].sched)
2747 return -1;
2748 if (dst->node[j].compressed &&
2749 (!dst->node[j].hull || !dst->node[j].compress ||
2750 !dst->node[j].decompress))
2751 return -1;
2754 return 0;
2757 /* Copy non-empty edges that satisfy edge_pred from the src dependence graph
2758 * to the dst dependence graph.
2759 * If the source or destination node of the edge is not in the destination
2760 * graph, then it must be a backward proximity edge and it should simply
2761 * be ignored.
2763 static int copy_edges(isl_ctx *ctx, struct isl_sched_graph *dst,
2764 struct isl_sched_graph *src,
2765 int (*edge_pred)(struct isl_sched_edge *edge, int data), int data)
2767 int i;
2768 enum isl_edge_type t;
2770 dst->n_edge = 0;
2771 for (i = 0; i < src->n_edge; ++i) {
2772 struct isl_sched_edge *edge = &src->edge[i];
2773 isl_map *map;
2774 isl_union_map *tagged_condition;
2775 isl_union_map *tagged_validity;
2776 struct isl_sched_node *dst_src, *dst_dst;
2778 if (!edge_pred(edge, data))
2779 continue;
2781 if (isl_map_plain_is_empty(edge->map))
2782 continue;
2784 dst_src = graph_find_node(ctx, dst, edge->src->space);
2785 dst_dst = graph_find_node(ctx, dst, edge->dst->space);
2786 if (!dst_src || !dst_dst) {
2787 if (edge->validity || edge->conditional_validity)
2788 isl_die(ctx, isl_error_internal,
2789 "backward (conditional) validity edge",
2790 return -1);
2791 continue;
2794 map = isl_map_copy(edge->map);
2795 tagged_condition = isl_union_map_copy(edge->tagged_condition);
2796 tagged_validity = isl_union_map_copy(edge->tagged_validity);
2798 dst->edge[dst->n_edge].src = dst_src;
2799 dst->edge[dst->n_edge].dst = dst_dst;
2800 dst->edge[dst->n_edge].map = map;
2801 dst->edge[dst->n_edge].tagged_condition = tagged_condition;
2802 dst->edge[dst->n_edge].tagged_validity = tagged_validity;
2803 dst->edge[dst->n_edge].validity = edge->validity;
2804 dst->edge[dst->n_edge].proximity = edge->proximity;
2805 dst->edge[dst->n_edge].coincidence = edge->coincidence;
2806 dst->edge[dst->n_edge].condition = edge->condition;
2807 dst->edge[dst->n_edge].conditional_validity =
2808 edge->conditional_validity;
2809 dst->n_edge++;
2811 if (edge->tagged_condition && !tagged_condition)
2812 return -1;
2813 if (edge->tagged_validity && !tagged_validity)
2814 return -1;
2816 for (t = isl_edge_first; t <= isl_edge_last; ++t) {
2817 if (edge !=
2818 graph_find_edge(src, t, edge->src, edge->dst))
2819 continue;
2820 if (graph_edge_table_add(ctx, dst, t,
2821 &dst->edge[dst->n_edge - 1]) < 0)
2822 return -1;
2826 return 0;
2829 /* Given a "src" dependence graph that contains the nodes from "dst"
2830 * that satisfy node_pred, copy the schedule computed in "src"
2831 * for those nodes back to "dst".
2833 static int copy_schedule(struct isl_sched_graph *dst,
2834 struct isl_sched_graph *src,
2835 int (*node_pred)(struct isl_sched_node *node, int data), int data)
2837 int i;
2839 src->n = 0;
2840 for (i = 0; i < dst->n; ++i) {
2841 if (!node_pred(&dst->node[i], data))
2842 continue;
2843 isl_mat_free(dst->node[i].sched);
2844 isl_map_free(dst->node[i].sched_map);
2845 dst->node[i].sched = isl_mat_copy(src->node[src->n].sched);
2846 dst->node[i].sched_map =
2847 isl_map_copy(src->node[src->n].sched_map);
2848 src->n++;
2851 dst->max_row = src->max_row;
2852 dst->n_total_row = src->n_total_row;
2853 dst->n_band = src->n_band;
2855 return 0;
2858 /* Compute the maximal number of variables over all nodes.
2859 * This is the maximal number of linearly independent schedule
2860 * rows that we need to compute.
2861 * Just in case we end up in a part of the dependence graph
2862 * with only lower-dimensional domains, we make sure we will
2863 * compute the required amount of extra linearly independent rows.
2865 static int compute_maxvar(struct isl_sched_graph *graph)
2867 int i;
2869 graph->maxvar = 0;
2870 for (i = 0; i < graph->n; ++i) {
2871 struct isl_sched_node *node = &graph->node[i];
2872 int nvar;
2874 if (node_update_cmap(node) < 0)
2875 return -1;
2876 nvar = node->nvar + graph->n_row - node->rank;
2877 if (nvar > graph->maxvar)
2878 graph->maxvar = nvar;
2881 return 0;
2884 static int compute_schedule(isl_ctx *ctx, struct isl_sched_graph *graph);
2885 static int compute_schedule_wcc(isl_ctx *ctx, struct isl_sched_graph *graph);
2887 /* Compute a schedule for a subgraph of "graph". In particular, for
2888 * the graph composed of nodes that satisfy node_pred and edges that
2889 * that satisfy edge_pred. The caller should precompute the number
2890 * of nodes and edges that satisfy these predicates and pass them along
2891 * as "n" and "n_edge".
2892 * If the subgraph is known to consist of a single component, then wcc should
2893 * be set and then we call compute_schedule_wcc on the constructed subgraph.
2894 * Otherwise, we call compute_schedule, which will check whether the subgraph
2895 * is connected.
2897 static int compute_sub_schedule(isl_ctx *ctx,
2898 struct isl_sched_graph *graph, int n, int n_edge,
2899 int (*node_pred)(struct isl_sched_node *node, int data),
2900 int (*edge_pred)(struct isl_sched_edge *edge, int data),
2901 int data, int wcc)
2903 struct isl_sched_graph split = { 0 };
2904 int t;
2906 if (graph_alloc(ctx, &split, n, n_edge) < 0)
2907 goto error;
2908 if (copy_nodes(&split, graph, node_pred, data) < 0)
2909 goto error;
2910 if (graph_init_table(ctx, &split) < 0)
2911 goto error;
2912 for (t = 0; t <= isl_edge_last; ++t)
2913 split.max_edge[t] = graph->max_edge[t];
2914 if (graph_init_edge_tables(ctx, &split) < 0)
2915 goto error;
2916 if (copy_edges(ctx, &split, graph, edge_pred, data) < 0)
2917 goto error;
2918 split.n_row = graph->n_row;
2919 split.max_row = graph->max_row;
2920 split.n_total_row = graph->n_total_row;
2921 split.n_band = graph->n_band;
2922 split.band_start = graph->band_start;
2924 if (wcc && compute_schedule_wcc(ctx, &split) < 0)
2925 goto error;
2926 if (!wcc && compute_schedule(ctx, &split) < 0)
2927 goto error;
2929 copy_schedule(graph, &split, node_pred, data);
2931 graph_free(ctx, &split);
2932 return 0;
2933 error:
2934 graph_free(ctx, &split);
2935 return -1;
2938 static int node_scc_exactly(struct isl_sched_node *node, int scc)
2940 return node->scc == scc;
2943 static int node_scc_at_most(struct isl_sched_node *node, int scc)
2945 return node->scc <= scc;
2948 static int node_scc_at_least(struct isl_sched_node *node, int scc)
2950 return node->scc >= scc;
2953 static int edge_scc_exactly(struct isl_sched_edge *edge, int scc)
2955 return edge->src->scc == scc && edge->dst->scc == scc;
2958 static int edge_dst_scc_at_most(struct isl_sched_edge *edge, int scc)
2960 return edge->dst->scc <= scc;
2963 static int edge_src_scc_at_least(struct isl_sched_edge *edge, int scc)
2965 return edge->src->scc >= scc;
2968 /* Pad the schedules of all nodes with zero rows such that in the end
2969 * they all have graph->n_total_row rows.
2970 * The extra rows don't belong to any band, so they get assigned band number -1.
2972 static int pad_schedule(struct isl_sched_graph *graph)
2974 int i, j;
2976 for (i = 0; i < graph->n; ++i) {
2977 struct isl_sched_node *node = &graph->node[i];
2978 int row = isl_mat_rows(node->sched);
2979 if (graph->n_total_row > row) {
2980 isl_map_free(node->sched_map);
2981 node->sched_map = NULL;
2983 node->sched = isl_mat_add_zero_rows(node->sched,
2984 graph->n_total_row - row);
2985 if (!node->sched)
2986 return -1;
2987 for (j = row; j < graph->n_total_row; ++j)
2988 node->band[j] = -1;
2991 return 0;
2994 /* Reset the current band by dropping all its schedule rows.
2996 static int reset_band(struct isl_sched_graph *graph)
2998 int i;
2999 int drop;
3001 drop = graph->n_total_row - graph->band_start;
3002 graph->n_total_row -= drop;
3003 graph->n_row -= drop;
3005 for (i = 0; i < graph->n; ++i) {
3006 struct isl_sched_node *node = &graph->node[i];
3008 isl_map_free(node->sched_map);
3009 node->sched_map = NULL;
3011 node->sched = isl_mat_drop_rows(node->sched,
3012 graph->band_start, drop);
3014 if (!node->sched)
3015 return -1;
3018 return 0;
3021 /* Split the current graph into two parts and compute a schedule for each
3022 * part individually. In particular, one part consists of all SCCs up
3023 * to and including graph->src_scc, while the other part contains the other
3024 * SCCS.
3026 * The split is enforced in the schedule by constant rows with two different
3027 * values (0 and 1). These constant rows replace the previously computed rows
3028 * in the current band.
3029 * It would be possible to reuse them as the first rows in the next
3030 * band, but recomputing them may result in better rows as we are looking
3031 * at a smaller part of the dependence graph.
3033 * Since we do not enforce coincidence, we conservatively mark the
3034 * splitting row as not coincident.
3036 * The band_id of the second group is set to n, where n is the number
3037 * of nodes in the first group. This ensures that the band_ids over
3038 * the two groups remain disjoint, even if either or both of the two
3039 * groups contain independent components.
3041 static int compute_split_schedule(isl_ctx *ctx, struct isl_sched_graph *graph)
3043 int i, j, n, e1, e2;
3044 int n_total_row, orig_total_row;
3045 int n_band, orig_band;
3047 if (graph->n_total_row >= graph->max_row)
3048 isl_die(ctx, isl_error_internal,
3049 "too many schedule rows", return -1);
3051 if (reset_band(graph) < 0)
3052 return -1;
3054 n = 0;
3055 for (i = 0; i < graph->n; ++i) {
3056 struct isl_sched_node *node = &graph->node[i];
3057 int row = isl_mat_rows(node->sched);
3058 int cols = isl_mat_cols(node->sched);
3059 int before = node->scc <= graph->src_scc;
3061 if (before)
3062 n++;
3064 isl_map_free(node->sched_map);
3065 node->sched_map = NULL;
3066 node->sched = isl_mat_add_rows(node->sched, 1);
3067 if (!node->sched)
3068 return -1;
3069 node->sched = isl_mat_set_element_si(node->sched, row, 0,
3070 !before);
3071 for (j = 1; j < cols; ++j)
3072 node->sched = isl_mat_set_element_si(node->sched,
3073 row, j, 0);
3074 node->band[graph->n_total_row] = graph->n_band;
3075 node->coincident[graph->n_total_row] = 0;
3078 e1 = e2 = 0;
3079 for (i = 0; i < graph->n_edge; ++i) {
3080 if (graph->edge[i].dst->scc <= graph->src_scc)
3081 e1++;
3082 if (graph->edge[i].src->scc > graph->src_scc)
3083 e2++;
3086 graph->n_total_row++;
3087 next_band(graph);
3089 for (i = 0; i < graph->n; ++i) {
3090 struct isl_sched_node *node = &graph->node[i];
3091 if (node->scc > graph->src_scc)
3092 node->band_id[graph->n_band] = n;
3095 orig_total_row = graph->n_total_row;
3096 orig_band = graph->n_band;
3097 if (compute_sub_schedule(ctx, graph, n, e1,
3098 &node_scc_at_most, &edge_dst_scc_at_most,
3099 graph->src_scc, 0) < 0)
3100 return -1;
3101 n_total_row = graph->n_total_row;
3102 graph->n_total_row = orig_total_row;
3103 n_band = graph->n_band;
3104 graph->n_band = orig_band;
3105 if (compute_sub_schedule(ctx, graph, graph->n - n, e2,
3106 &node_scc_at_least, &edge_src_scc_at_least,
3107 graph->src_scc + 1, 0) < 0)
3108 return -1;
3109 if (n_total_row > graph->n_total_row)
3110 graph->n_total_row = n_total_row;
3111 if (n_band > graph->n_band)
3112 graph->n_band = n_band;
3114 return pad_schedule(graph);
3117 /* Compute the next band of the schedule after updating the dependence
3118 * relations based on the the current schedule.
3120 static int compute_next_band(isl_ctx *ctx, struct isl_sched_graph *graph)
3122 if (update_edges(ctx, graph) < 0)
3123 return -1;
3124 next_band(graph);
3126 return compute_schedule(ctx, graph);
3129 /* Add constraints to graph->lp that force the dependence "map" (which
3130 * is part of the dependence relation of "edge")
3131 * to be respected and attempt to carry it, where the edge is one from
3132 * a node j to itself. "pos" is the sequence number of the given map.
3133 * That is, add constraints that enforce
3135 * (c_j_0 + c_j_n n + c_j_x y) - (c_j_0 + c_j_n n + c_j_x x)
3136 * = c_j_x (y - x) >= e_i
3138 * for each (x,y) in R.
3139 * We obtain general constraints on coefficients (c_0, c_n, c_x)
3140 * of valid constraints for (y - x) and then plug in (-e_i, 0, c_j_x),
3141 * with each coefficient in c_j_x represented as a pair of non-negative
3142 * coefficients.
3144 static int add_intra_constraints(struct isl_sched_graph *graph,
3145 struct isl_sched_edge *edge, __isl_take isl_map *map, int pos)
3147 unsigned total;
3148 isl_ctx *ctx = isl_map_get_ctx(map);
3149 isl_space *dim;
3150 isl_dim_map *dim_map;
3151 isl_basic_set *coef;
3152 struct isl_sched_node *node = edge->src;
3154 coef = intra_coefficients(graph, node, map);
3155 if (!coef)
3156 return -1;
3158 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
3160 total = isl_basic_set_total_dim(graph->lp);
3161 dim_map = isl_dim_map_alloc(ctx, total);
3162 isl_dim_map_range(dim_map, 3 + pos, 0, 0, 0, 1, -1);
3163 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 1, 2,
3164 isl_space_dim(dim, isl_dim_set), 1,
3165 node->nvar, -1);
3166 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 2, 2,
3167 isl_space_dim(dim, isl_dim_set), 1,
3168 node->nvar, 1);
3169 graph->lp = isl_basic_set_extend_constraints(graph->lp,
3170 coef->n_eq, coef->n_ineq);
3171 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
3172 coef, dim_map);
3173 isl_space_free(dim);
3175 return 0;
3178 /* Add constraints to graph->lp that force the dependence "map" (which
3179 * is part of the dependence relation of "edge")
3180 * to be respected and attempt to carry it, where the edge is one from
3181 * node j to node k. "pos" is the sequence number of the given map.
3182 * That is, add constraints that enforce
3184 * (c_k_0 + c_k_n n + c_k_x y) - (c_j_0 + c_j_n n + c_j_x x) >= e_i
3186 * for each (x,y) in R.
3187 * We obtain general constraints on coefficients (c_0, c_n, c_x)
3188 * of valid constraints for R and then plug in
3189 * (-e_i + c_k_0 - c_j_0, c_k_n - c_j_n, c_k_x - c_j_x)
3190 * with each coefficient (except e_i, c_k_0 and c_j_0)
3191 * represented as a pair of non-negative coefficients.
3193 static int add_inter_constraints(struct isl_sched_graph *graph,
3194 struct isl_sched_edge *edge, __isl_take isl_map *map, int pos)
3196 unsigned total;
3197 isl_ctx *ctx = isl_map_get_ctx(map);
3198 isl_space *dim;
3199 isl_dim_map *dim_map;
3200 isl_basic_set *coef;
3201 struct isl_sched_node *src = edge->src;
3202 struct isl_sched_node *dst = edge->dst;
3204 coef = inter_coefficients(graph, edge, map);
3205 if (!coef)
3206 return -1;
3208 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
3210 total = isl_basic_set_total_dim(graph->lp);
3211 dim_map = isl_dim_map_alloc(ctx, total);
3213 isl_dim_map_range(dim_map, 3 + pos, 0, 0, 0, 1, -1);
3215 isl_dim_map_range(dim_map, dst->start, 0, 0, 0, 1, 1);
3216 isl_dim_map_range(dim_map, dst->start + 1, 2, 1, 1, dst->nparam, -1);
3217 isl_dim_map_range(dim_map, dst->start + 2, 2, 1, 1, dst->nparam, 1);
3218 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 1, 2,
3219 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
3220 dst->nvar, -1);
3221 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 2, 2,
3222 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
3223 dst->nvar, 1);
3225 isl_dim_map_range(dim_map, src->start, 0, 0, 0, 1, -1);
3226 isl_dim_map_range(dim_map, src->start + 1, 2, 1, 1, src->nparam, 1);
3227 isl_dim_map_range(dim_map, src->start + 2, 2, 1, 1, src->nparam, -1);
3228 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 1, 2,
3229 isl_space_dim(dim, isl_dim_set), 1,
3230 src->nvar, 1);
3231 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 2, 2,
3232 isl_space_dim(dim, isl_dim_set), 1,
3233 src->nvar, -1);
3235 graph->lp = isl_basic_set_extend_constraints(graph->lp,
3236 coef->n_eq, coef->n_ineq);
3237 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
3238 coef, dim_map);
3239 isl_space_free(dim);
3241 return 0;
3244 /* Add constraints to graph->lp that force all (conditional) validity
3245 * dependences to be respected and attempt to carry them.
3247 static int add_all_constraints(struct isl_sched_graph *graph)
3249 int i, j;
3250 int pos;
3252 pos = 0;
3253 for (i = 0; i < graph->n_edge; ++i) {
3254 struct isl_sched_edge *edge= &graph->edge[i];
3256 if (!edge->validity && !edge->conditional_validity)
3257 continue;
3259 for (j = 0; j < edge->map->n; ++j) {
3260 isl_basic_map *bmap;
3261 isl_map *map;
3263 bmap = isl_basic_map_copy(edge->map->p[j]);
3264 map = isl_map_from_basic_map(bmap);
3266 if (edge->src == edge->dst &&
3267 add_intra_constraints(graph, edge, map, pos) < 0)
3268 return -1;
3269 if (edge->src != edge->dst &&
3270 add_inter_constraints(graph, edge, map, pos) < 0)
3271 return -1;
3272 ++pos;
3276 return 0;
3279 /* Count the number of equality and inequality constraints
3280 * that will be added to the carry_lp problem.
3281 * We count each edge exactly once.
3283 static int count_all_constraints(struct isl_sched_graph *graph,
3284 int *n_eq, int *n_ineq)
3286 int i, j;
3288 *n_eq = *n_ineq = 0;
3289 for (i = 0; i < graph->n_edge; ++i) {
3290 struct isl_sched_edge *edge= &graph->edge[i];
3291 for (j = 0; j < edge->map->n; ++j) {
3292 isl_basic_map *bmap;
3293 isl_map *map;
3295 bmap = isl_basic_map_copy(edge->map->p[j]);
3296 map = isl_map_from_basic_map(bmap);
3298 if (count_map_constraints(graph, edge, map,
3299 n_eq, n_ineq, 1, 0) < 0)
3300 return -1;
3304 return 0;
3307 /* Construct an LP problem for finding schedule coefficients
3308 * such that the schedule carries as many dependences as possible.
3309 * In particular, for each dependence i, we bound the dependence distance
3310 * from below by e_i, with 0 <= e_i <= 1 and then maximize the sum
3311 * of all e_i's. Dependence with e_i = 0 in the solution are simply
3312 * respected, while those with e_i > 0 (in practice e_i = 1) are carried.
3313 * Note that if the dependence relation is a union of basic maps,
3314 * then we have to consider each basic map individually as it may only
3315 * be possible to carry the dependences expressed by some of those
3316 * basic maps and not all off them.
3317 * Below, we consider each of those basic maps as a separate "edge".
3319 * All variables of the LP are non-negative. The actual coefficients
3320 * may be negative, so each coefficient is represented as the difference
3321 * of two non-negative variables. The negative part always appears
3322 * immediately before the positive part.
3323 * Other than that, the variables have the following order
3325 * - sum of (1 - e_i) over all edges
3326 * - sum of positive and negative parts of all c_n coefficients
3327 * (unconstrained when computing non-parametric schedules)
3328 * - sum of positive and negative parts of all c_x coefficients
3329 * - for each edge
3330 * - e_i
3331 * - for each node
3332 * - c_i_0
3333 * - positive and negative parts of c_i_n (if parametric)
3334 * - positive and negative parts of c_i_x
3336 * The constraints are those from the (validity) edges plus three equalities
3337 * to express the sums and n_edge inequalities to express e_i <= 1.
3339 static int setup_carry_lp(isl_ctx *ctx, struct isl_sched_graph *graph)
3341 int i, j;
3342 int k;
3343 isl_space *dim;
3344 unsigned total;
3345 int n_eq, n_ineq;
3346 int n_edge;
3348 n_edge = 0;
3349 for (i = 0; i < graph->n_edge; ++i)
3350 n_edge += graph->edge[i].map->n;
3352 total = 3 + n_edge;
3353 for (i = 0; i < graph->n; ++i) {
3354 struct isl_sched_node *node = &graph->node[graph->sorted[i]];
3355 node->start = total;
3356 total += 1 + 2 * (node->nparam + node->nvar);
3359 if (count_all_constraints(graph, &n_eq, &n_ineq) < 0)
3360 return -1;
3361 if (count_bound_coefficient_constraints(ctx, graph, &n_eq, &n_ineq) < 0)
3362 return -1;
3364 dim = isl_space_set_alloc(ctx, 0, total);
3365 isl_basic_set_free(graph->lp);
3366 n_eq += 3;
3367 n_ineq += n_edge;
3368 graph->lp = isl_basic_set_alloc_space(dim, 0, n_eq, n_ineq);
3369 graph->lp = isl_basic_set_set_rational(graph->lp);
3371 k = isl_basic_set_alloc_equality(graph->lp);
3372 if (k < 0)
3373 return -1;
3374 isl_seq_clr(graph->lp->eq[k], 1 + total);
3375 isl_int_set_si(graph->lp->eq[k][0], -n_edge);
3376 isl_int_set_si(graph->lp->eq[k][1], 1);
3377 for (i = 0; i < n_edge; ++i)
3378 isl_int_set_si(graph->lp->eq[k][4 + i], 1);
3380 k = isl_basic_set_alloc_equality(graph->lp);
3381 if (k < 0)
3382 return -1;
3383 isl_seq_clr(graph->lp->eq[k], 1 + total);
3384 isl_int_set_si(graph->lp->eq[k][2], -1);
3385 for (i = 0; i < graph->n; ++i) {
3386 int pos = 1 + graph->node[i].start + 1;
3388 for (j = 0; j < 2 * graph->node[i].nparam; ++j)
3389 isl_int_set_si(graph->lp->eq[k][pos + j], 1);
3392 k = isl_basic_set_alloc_equality(graph->lp);
3393 if (k < 0)
3394 return -1;
3395 isl_seq_clr(graph->lp->eq[k], 1 + total);
3396 isl_int_set_si(graph->lp->eq[k][3], -1);
3397 for (i = 0; i < graph->n; ++i) {
3398 struct isl_sched_node *node = &graph->node[i];
3399 int pos = 1 + node->start + 1 + 2 * node->nparam;
3401 for (j = 0; j < 2 * node->nvar; ++j)
3402 isl_int_set_si(graph->lp->eq[k][pos + j], 1);
3405 for (i = 0; i < n_edge; ++i) {
3406 k = isl_basic_set_alloc_inequality(graph->lp);
3407 if (k < 0)
3408 return -1;
3409 isl_seq_clr(graph->lp->ineq[k], 1 + total);
3410 isl_int_set_si(graph->lp->ineq[k][4 + i], -1);
3411 isl_int_set_si(graph->lp->ineq[k][0], 1);
3414 if (add_bound_coefficient_constraints(ctx, graph) < 0)
3415 return -1;
3416 if (add_all_constraints(graph) < 0)
3417 return -1;
3419 return 0;
3422 /* If the schedule_split_scaled option is set and if the linear
3423 * parts of the scheduling rows for all nodes in the graphs have
3424 * non-trivial common divisor, then split off the constant term
3425 * from the linear part.
3426 * The constant term is then placed in a separate band and
3427 * the linear part is reduced.
3429 static int split_scaled(isl_ctx *ctx, struct isl_sched_graph *graph)
3431 int i;
3432 int row;
3433 isl_int gcd, gcd_i;
3435 if (!ctx->opt->schedule_split_scaled)
3436 return 0;
3437 if (graph->n <= 1)
3438 return 0;
3440 if (graph->n_total_row >= graph->max_row)
3441 isl_die(ctx, isl_error_internal,
3442 "too many schedule rows", return -1);
3444 isl_int_init(gcd);
3445 isl_int_init(gcd_i);
3447 isl_int_set_si(gcd, 0);
3449 row = isl_mat_rows(graph->node[0].sched) - 1;
3451 for (i = 0; i < graph->n; ++i) {
3452 struct isl_sched_node *node = &graph->node[i];
3453 int cols = isl_mat_cols(node->sched);
3455 isl_seq_gcd(node->sched->row[row] + 1, cols - 1, &gcd_i);
3456 isl_int_gcd(gcd, gcd, gcd_i);
3459 isl_int_clear(gcd_i);
3461 if (isl_int_cmp_si(gcd, 1) <= 0) {
3462 isl_int_clear(gcd);
3463 return 0;
3466 next_band(graph);
3468 for (i = 0; i < graph->n; ++i) {
3469 struct isl_sched_node *node = &graph->node[i];
3471 isl_map_free(node->sched_map);
3472 node->sched_map = NULL;
3473 node->sched = isl_mat_add_zero_rows(node->sched, 1);
3474 if (!node->sched)
3475 goto error;
3476 isl_int_fdiv_r(node->sched->row[row + 1][0],
3477 node->sched->row[row][0], gcd);
3478 isl_int_fdiv_q(node->sched->row[row][0],
3479 node->sched->row[row][0], gcd);
3480 isl_int_mul(node->sched->row[row][0],
3481 node->sched->row[row][0], gcd);
3482 node->sched = isl_mat_scale_down_row(node->sched, row, gcd);
3483 if (!node->sched)
3484 goto error;
3485 node->band[graph->n_total_row] = graph->n_band;
3488 graph->n_total_row++;
3490 isl_int_clear(gcd);
3491 return 0;
3492 error:
3493 isl_int_clear(gcd);
3494 return -1;
3497 static int compute_component_schedule(isl_ctx *ctx,
3498 struct isl_sched_graph *graph);
3500 /* Is the schedule row "sol" trivial on node "node"?
3501 * That is, is the solution zero on the dimensions orthogonal to
3502 * the previously found solutions?
3503 * Return 1 if the solution is trivial, 0 if it is not and -1 on error.
3505 * Each coefficient is represented as the difference between
3506 * two non-negative values in "sol". "sol" has been computed
3507 * in terms of the original iterators (i.e., without use of cmap).
3508 * We construct the schedule row s and write it as a linear
3509 * combination of (linear combinations of) previously computed schedule rows.
3510 * s = Q c or c = U s.
3511 * If the final entries of c are all zero, then the solution is trivial.
3513 static int is_trivial(struct isl_sched_node *node, __isl_keep isl_vec *sol)
3515 int i;
3516 int pos;
3517 int trivial;
3518 isl_ctx *ctx;
3519 isl_vec *node_sol;
3521 if (!sol)
3522 return -1;
3523 if (node->nvar == node->rank)
3524 return 0;
3526 ctx = isl_vec_get_ctx(sol);
3527 node_sol = isl_vec_alloc(ctx, node->nvar);
3528 if (!node_sol)
3529 return -1;
3531 pos = 1 + node->start + 1 + 2 * node->nparam;
3533 for (i = 0; i < node->nvar; ++i)
3534 isl_int_sub(node_sol->el[i],
3535 sol->el[pos + 2 * i + 1], sol->el[pos + 2 * i]);
3537 node_sol = isl_mat_vec_product(isl_mat_copy(node->cinv), node_sol);
3539 if (!node_sol)
3540 return -1;
3542 trivial = isl_seq_first_non_zero(node_sol->el + node->rank,
3543 node->nvar - node->rank) == -1;
3545 isl_vec_free(node_sol);
3547 return trivial;
3550 /* Is the schedule row "sol" trivial on any node where it should
3551 * not be trivial?
3552 * "sol" has been computed in terms of the original iterators
3553 * (i.e., without use of cmap).
3554 * Return 1 if any solution is trivial, 0 if they are not and -1 on error.
3556 static int is_any_trivial(struct isl_sched_graph *graph,
3557 __isl_keep isl_vec *sol)
3559 int i;
3561 for (i = 0; i < graph->n; ++i) {
3562 struct isl_sched_node *node = &graph->node[i];
3563 int trivial;
3565 if (!needs_row(graph, node))
3566 continue;
3567 trivial = is_trivial(node, sol);
3568 if (trivial < 0 || trivial)
3569 return trivial;
3572 return 0;
3575 /* Construct a schedule row for each node such that as many dependences
3576 * as possible are carried and then continue with the next band.
3578 * If the computed schedule row turns out to be trivial on one or
3579 * more nodes where it should not be trivial, then we throw it away
3580 * and try again on each component separately.
3582 * If there is only one component, then we accept the schedule row anyway,
3583 * but we do not consider it as a complete row and therefore do not
3584 * increment graph->n_row. Note that the ranks of the nodes that
3585 * do get a non-trivial schedule part will get updated regardless and
3586 * graph->maxvar is computed based on these ranks. The test for
3587 * whether more schedule rows are required in compute_schedule_wcc
3588 * is therefore not affected.
3590 static int carry_dependences(isl_ctx *ctx, struct isl_sched_graph *graph)
3592 int i;
3593 int n_edge;
3594 int trivial;
3595 isl_vec *sol;
3596 isl_basic_set *lp;
3598 n_edge = 0;
3599 for (i = 0; i < graph->n_edge; ++i)
3600 n_edge += graph->edge[i].map->n;
3602 if (setup_carry_lp(ctx, graph) < 0)
3603 return -1;
3605 lp = isl_basic_set_copy(graph->lp);
3606 sol = isl_tab_basic_set_non_neg_lexmin(lp);
3607 if (!sol)
3608 return -1;
3610 if (sol->size == 0) {
3611 isl_vec_free(sol);
3612 isl_die(ctx, isl_error_internal,
3613 "error in schedule construction", return -1);
3616 isl_int_divexact(sol->el[1], sol->el[1], sol->el[0]);
3617 if (isl_int_cmp_si(sol->el[1], n_edge) >= 0) {
3618 isl_vec_free(sol);
3619 isl_die(ctx, isl_error_unknown,
3620 "unable to carry dependences", return -1);
3623 trivial = is_any_trivial(graph, sol);
3624 if (trivial < 0) {
3625 sol = isl_vec_free(sol);
3626 } else if (trivial && graph->scc > 1) {
3627 isl_vec_free(sol);
3628 return compute_component_schedule(ctx, graph);
3631 if (update_schedule(graph, sol, 0, 0) < 0)
3632 return -1;
3633 if (trivial)
3634 graph->n_row--;
3636 if (split_scaled(ctx, graph) < 0)
3637 return -1;
3639 return compute_next_band(ctx, graph);
3642 /* Topologically sort statements mapped to the same schedule iteration
3643 * and add a row to the schedule corresponding to this order.
3645 static int sort_statements(isl_ctx *ctx, struct isl_sched_graph *graph)
3647 int i, j;
3649 if (graph->n <= 1)
3650 return 0;
3652 if (update_edges(ctx, graph) < 0)
3653 return -1;
3655 if (graph->n_edge == 0)
3656 return 0;
3658 if (detect_sccs(ctx, graph) < 0)
3659 return -1;
3661 if (graph->n_total_row >= graph->max_row)
3662 isl_die(ctx, isl_error_internal,
3663 "too many schedule rows", return -1);
3665 for (i = 0; i < graph->n; ++i) {
3666 struct isl_sched_node *node = &graph->node[i];
3667 int row = isl_mat_rows(node->sched);
3668 int cols = isl_mat_cols(node->sched);
3670 isl_map_free(node->sched_map);
3671 node->sched_map = NULL;
3672 node->sched = isl_mat_add_rows(node->sched, 1);
3673 if (!node->sched)
3674 return -1;
3675 node->sched = isl_mat_set_element_si(node->sched, row, 0,
3676 node->scc);
3677 for (j = 1; j < cols; ++j)
3678 node->sched = isl_mat_set_element_si(node->sched,
3679 row, j, 0);
3680 node->band[graph->n_total_row] = graph->n_band;
3683 graph->n_total_row++;
3684 next_band(graph);
3686 return 0;
3689 /* Are there any (non-empty) (conditional) validity edges in the graph?
3691 static int has_validity_edges(struct isl_sched_graph *graph)
3693 int i;
3695 for (i = 0; i < graph->n_edge; ++i) {
3696 int empty;
3698 empty = isl_map_plain_is_empty(graph->edge[i].map);
3699 if (empty < 0)
3700 return -1;
3701 if (empty)
3702 continue;
3703 if (graph->edge[i].validity ||
3704 graph->edge[i].conditional_validity)
3705 return 1;
3708 return 0;
3711 /* Should we apply a Feautrier step?
3712 * That is, did the user request the Feautrier algorithm and are
3713 * there any validity dependences (left)?
3715 static int need_feautrier_step(isl_ctx *ctx, struct isl_sched_graph *graph)
3717 if (ctx->opt->schedule_algorithm != ISL_SCHEDULE_ALGORITHM_FEAUTRIER)
3718 return 0;
3720 return has_validity_edges(graph);
3723 /* Compute a schedule for a connected dependence graph using Feautrier's
3724 * multi-dimensional scheduling algorithm.
3725 * The original algorithm is described in [1].
3726 * The main idea is to minimize the number of scheduling dimensions, by
3727 * trying to satisfy as many dependences as possible per scheduling dimension.
3729 * [1] P. Feautrier, Some Efficient Solutions to the Affine Scheduling
3730 * Problem, Part II: Multi-Dimensional Time.
3731 * In Intl. Journal of Parallel Programming, 1992.
3733 static int compute_schedule_wcc_feautrier(isl_ctx *ctx,
3734 struct isl_sched_graph *graph)
3736 return carry_dependences(ctx, graph);
3739 /* Turn off the "local" bit on all (condition) edges.
3741 static void clear_local_edges(struct isl_sched_graph *graph)
3743 int i;
3745 for (i = 0; i < graph->n_edge; ++i)
3746 if (graph->edge[i].condition)
3747 graph->edge[i].local = 0;
3750 /* Does "graph" have both condition and conditional validity edges?
3752 static int need_condition_check(struct isl_sched_graph *graph)
3754 int i;
3755 int any_condition = 0;
3756 int any_conditional_validity = 0;
3758 for (i = 0; i < graph->n_edge; ++i) {
3759 if (graph->edge[i].condition)
3760 any_condition = 1;
3761 if (graph->edge[i].conditional_validity)
3762 any_conditional_validity = 1;
3765 return any_condition && any_conditional_validity;
3768 /* Does "graph" contain any coincidence edge?
3770 static int has_any_coincidence(struct isl_sched_graph *graph)
3772 int i;
3774 for (i = 0; i < graph->n_edge; ++i)
3775 if (graph->edge[i].coincidence)
3776 return 1;
3778 return 0;
3781 /* Extract the final schedule row as a map with the iteration domain
3782 * of "node" as domain.
3784 static __isl_give isl_map *final_row(struct isl_sched_node *node)
3786 isl_local_space *ls;
3787 isl_aff *aff;
3788 int row;
3790 row = isl_mat_rows(node->sched) - 1;
3791 ls = isl_local_space_from_space(isl_space_copy(node->space));
3792 aff = extract_schedule_row(ls, node, row);
3793 return isl_map_from_aff(aff);
3796 /* Is the conditional validity dependence in the edge with index "edge_index"
3797 * violated by the latest (i.e., final) row of the schedule?
3798 * That is, is i scheduled after j
3799 * for any conditional validity dependence i -> j?
3801 static int is_violated(struct isl_sched_graph *graph, int edge_index)
3803 isl_map *src_sched, *dst_sched, *map;
3804 struct isl_sched_edge *edge = &graph->edge[edge_index];
3805 int empty;
3807 src_sched = final_row(edge->src);
3808 dst_sched = final_row(edge->dst);
3809 map = isl_map_copy(edge->map);
3810 map = isl_map_apply_domain(map, src_sched);
3811 map = isl_map_apply_range(map, dst_sched);
3812 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_out, 0);
3813 empty = isl_map_is_empty(map);
3814 isl_map_free(map);
3816 if (empty < 0)
3817 return -1;
3819 return !empty;
3822 /* Does "graph" have any satisfied condition edges that
3823 * are adjacent to the conditional validity constraint with
3824 * domain "conditional_source" and range "conditional_sink"?
3826 * A satisfied condition is one that is not local.
3827 * If a condition was forced to be local already (i.e., marked as local)
3828 * then there is no need to check if it is in fact local.
3830 * Additionally, mark all adjacent condition edges found as local.
3832 static int has_adjacent_true_conditions(struct isl_sched_graph *graph,
3833 __isl_keep isl_union_set *conditional_source,
3834 __isl_keep isl_union_set *conditional_sink)
3836 int i;
3837 int any = 0;
3839 for (i = 0; i < graph->n_edge; ++i) {
3840 int adjacent, local;
3841 isl_union_map *condition;
3843 if (!graph->edge[i].condition)
3844 continue;
3845 if (graph->edge[i].local)
3846 continue;
3848 condition = graph->edge[i].tagged_condition;
3849 adjacent = domain_intersects(condition, conditional_sink);
3850 if (adjacent >= 0 && !adjacent)
3851 adjacent = range_intersects(condition,
3852 conditional_source);
3853 if (adjacent < 0)
3854 return -1;
3855 if (!adjacent)
3856 continue;
3858 graph->edge[i].local = 1;
3860 local = is_condition_false(&graph->edge[i]);
3861 if (local < 0)
3862 return -1;
3863 if (!local)
3864 any = 1;
3867 return any;
3870 /* Are there any violated conditional validity dependences with
3871 * adjacent condition dependences that are not local with respect
3872 * to the current schedule?
3873 * That is, is the conditional validity constraint violated?
3875 * Additionally, mark all those adjacent condition dependences as local.
3876 * We also mark those adjacent condition dependences that were not marked
3877 * as local before, but just happened to be local already. This ensures
3878 * that they remain local if the schedule is recomputed.
3880 * We first collect domain and range of all violated conditional validity
3881 * dependences and then check if there are any adjacent non-local
3882 * condition dependences.
3884 static int has_violated_conditional_constraint(isl_ctx *ctx,
3885 struct isl_sched_graph *graph)
3887 int i;
3888 int any = 0;
3889 isl_union_set *source, *sink;
3891 source = isl_union_set_empty(isl_space_params_alloc(ctx, 0));
3892 sink = isl_union_set_empty(isl_space_params_alloc(ctx, 0));
3893 for (i = 0; i < graph->n_edge; ++i) {
3894 isl_union_set *uset;
3895 isl_union_map *umap;
3896 int violated;
3898 if (!graph->edge[i].conditional_validity)
3899 continue;
3901 violated = is_violated(graph, i);
3902 if (violated < 0)
3903 goto error;
3904 if (!violated)
3905 continue;
3907 any = 1;
3909 umap = isl_union_map_copy(graph->edge[i].tagged_validity);
3910 uset = isl_union_map_domain(umap);
3911 source = isl_union_set_union(source, uset);
3912 source = isl_union_set_coalesce(source);
3914 umap = isl_union_map_copy(graph->edge[i].tagged_validity);
3915 uset = isl_union_map_range(umap);
3916 sink = isl_union_set_union(sink, uset);
3917 sink = isl_union_set_coalesce(sink);
3920 if (any)
3921 any = has_adjacent_true_conditions(graph, source, sink);
3923 isl_union_set_free(source);
3924 isl_union_set_free(sink);
3925 return any;
3926 error:
3927 isl_union_set_free(source);
3928 isl_union_set_free(sink);
3929 return -1;
3932 /* Compute a schedule for a connected dependence graph.
3933 * We try to find a sequence of as many schedule rows as possible that result
3934 * in non-negative dependence distances (independent of the previous rows
3935 * in the sequence, i.e., such that the sequence is tilable), with as
3936 * many of the initial rows as possible satisfying the coincidence constraints.
3937 * If we can't find any more rows we either
3938 * - split between SCCs and start over (assuming we found an interesting
3939 * pair of SCCs between which to split)
3940 * - continue with the next band (assuming the current band has at least
3941 * one row)
3942 * - try to carry as many dependences as possible and continue with the next
3943 * band
3945 * If Feautrier's algorithm is selected, we first recursively try to satisfy
3946 * as many validity dependences as possible. When all validity dependences
3947 * are satisfied we extend the schedule to a full-dimensional schedule.
3949 * If we manage to complete the schedule, we finish off by topologically
3950 * sorting the statements based on the remaining dependences.
3952 * If ctx->opt->schedule_outer_coincidence is set, then we force the
3953 * outermost dimension to satisfy the coincidence constraints. If this
3954 * turns out to be impossible, we fall back on the general scheme above
3955 * and try to carry as many dependences as possible.
3957 * If "graph" contains both condition and conditional validity dependences,
3958 * then we need to check that that the conditional schedule constraint
3959 * is satisfied, i.e., there are no violated conditional validity dependences
3960 * that are adjacent to any non-local condition dependences.
3961 * If there are, then we mark all those adjacent condition dependences
3962 * as local and recompute the current band. Those dependences that
3963 * are marked local will then be forced to be local.
3964 * The initial computation is performed with no dependences marked as local.
3965 * If we are lucky, then there will be no violated conditional validity
3966 * dependences adjacent to any non-local condition dependences.
3967 * Otherwise, we mark some additional condition dependences as local and
3968 * recompute. We continue this process until there are no violations left or
3969 * until we are no longer able to compute a schedule.
3970 * Since there are only a finite number of dependences,
3971 * there will only be a finite number of iterations.
3973 static int compute_schedule_wcc(isl_ctx *ctx, struct isl_sched_graph *graph)
3975 int has_coincidence;
3976 int use_coincidence;
3977 int force_coincidence = 0;
3978 int check_conditional;
3980 if (detect_sccs(ctx, graph) < 0)
3981 return -1;
3982 if (sort_sccs(graph) < 0)
3983 return -1;
3985 if (compute_maxvar(graph) < 0)
3986 return -1;
3988 if (need_feautrier_step(ctx, graph))
3989 return compute_schedule_wcc_feautrier(ctx, graph);
3991 clear_local_edges(graph);
3992 check_conditional = need_condition_check(graph);
3993 has_coincidence = has_any_coincidence(graph);
3995 if (ctx->opt->schedule_outer_coincidence)
3996 force_coincidence = 1;
3998 use_coincidence = has_coincidence;
3999 while (graph->n_row < graph->maxvar) {
4000 isl_vec *sol;
4001 int violated;
4002 int coincident;
4004 graph->src_scc = -1;
4005 graph->dst_scc = -1;
4007 if (setup_lp(ctx, graph, use_coincidence) < 0)
4008 return -1;
4009 sol = solve_lp(graph);
4010 if (!sol)
4011 return -1;
4012 if (sol->size == 0) {
4013 int empty = graph->n_total_row == graph->band_start;
4015 isl_vec_free(sol);
4016 if (use_coincidence && (!force_coincidence || !empty)) {
4017 use_coincidence = 0;
4018 continue;
4020 if (!ctx->opt->schedule_maximize_band_depth && !empty)
4021 return compute_next_band(ctx, graph);
4022 if (graph->src_scc >= 0)
4023 return compute_split_schedule(ctx, graph);
4024 if (!empty)
4025 return compute_next_band(ctx, graph);
4026 return carry_dependences(ctx, graph);
4028 coincident = !has_coincidence || use_coincidence;
4029 if (update_schedule(graph, sol, 1, coincident) < 0)
4030 return -1;
4032 if (!check_conditional)
4033 continue;
4034 violated = has_violated_conditional_constraint(ctx, graph);
4035 if (violated < 0)
4036 return -1;
4037 if (!violated)
4038 continue;
4039 if (reset_band(graph) < 0)
4040 return -1;
4041 use_coincidence = has_coincidence;
4044 if (graph->n_total_row > graph->band_start)
4045 next_band(graph);
4046 return sort_statements(ctx, graph);
4049 /* Add a row to the schedules that separates the SCCs and move
4050 * to the next band.
4052 static int split_on_scc(isl_ctx *ctx, struct isl_sched_graph *graph)
4054 int i;
4056 if (graph->n_total_row >= graph->max_row)
4057 isl_die(ctx, isl_error_internal,
4058 "too many schedule rows", return -1);
4060 for (i = 0; i < graph->n; ++i) {
4061 struct isl_sched_node *node = &graph->node[i];
4062 int row = isl_mat_rows(node->sched);
4064 isl_map_free(node->sched_map);
4065 node->sched_map = NULL;
4066 node->sched = isl_mat_add_zero_rows(node->sched, 1);
4067 node->sched = isl_mat_set_element_si(node->sched, row, 0,
4068 node->scc);
4069 if (!node->sched)
4070 return -1;
4071 node->band[graph->n_total_row] = graph->n_band;
4074 graph->n_total_row++;
4075 next_band(graph);
4077 return 0;
4080 /* Compute a schedule for each component (identified by node->scc)
4081 * of the dependence graph separately and then combine the results.
4082 * Depending on the setting of schedule_fuse, a component may be
4083 * either weakly or strongly connected.
4085 * The band_id is adjusted such that each component has a separate id.
4086 * Note that the band_id may have already been set to a value different
4087 * from zero by compute_split_schedule.
4089 static int compute_component_schedule(isl_ctx *ctx,
4090 struct isl_sched_graph *graph)
4092 int wcc, i;
4093 int n, n_edge;
4094 int n_total_row, orig_total_row;
4095 int n_band, orig_band;
4097 if (ctx->opt->schedule_fuse == ISL_SCHEDULE_FUSE_MIN ||
4098 ctx->opt->schedule_separate_components)
4099 if (split_on_scc(ctx, graph) < 0)
4100 return -1;
4102 n_total_row = 0;
4103 orig_total_row = graph->n_total_row;
4104 n_band = 0;
4105 orig_band = graph->n_band;
4106 for (i = 0; i < graph->n; ++i)
4107 graph->node[i].band_id[graph->n_band] += graph->node[i].scc;
4108 for (wcc = 0; wcc < graph->scc; ++wcc) {
4109 n = 0;
4110 for (i = 0; i < graph->n; ++i)
4111 if (graph->node[i].scc == wcc)
4112 n++;
4113 n_edge = 0;
4114 for (i = 0; i < graph->n_edge; ++i)
4115 if (graph->edge[i].src->scc == wcc &&
4116 graph->edge[i].dst->scc == wcc)
4117 n_edge++;
4119 if (compute_sub_schedule(ctx, graph, n, n_edge,
4120 &node_scc_exactly,
4121 &edge_scc_exactly, wcc, 1) < 0)
4122 return -1;
4123 if (graph->n_total_row > n_total_row)
4124 n_total_row = graph->n_total_row;
4125 graph->n_total_row = orig_total_row;
4126 if (graph->n_band > n_band)
4127 n_band = graph->n_band;
4128 graph->n_band = orig_band;
4131 graph->n_total_row = n_total_row;
4132 graph->n_band = n_band;
4134 return pad_schedule(graph);
4137 /* Compute a schedule for the given dependence graph.
4138 * We first check if the graph is connected (through validity and conditional
4139 * validity dependences) and, if not, compute a schedule
4140 * for each component separately.
4141 * If schedule_fuse is set to minimal fusion, then we check for strongly
4142 * connected components instead and compute a separate schedule for
4143 * each such strongly connected component.
4145 static int compute_schedule(isl_ctx *ctx, struct isl_sched_graph *graph)
4147 if (ctx->opt->schedule_fuse == ISL_SCHEDULE_FUSE_MIN) {
4148 if (detect_sccs(ctx, graph) < 0)
4149 return -1;
4150 } else {
4151 if (detect_wccs(ctx, graph) < 0)
4152 return -1;
4155 if (graph->scc > 1)
4156 return compute_component_schedule(ctx, graph);
4158 return compute_schedule_wcc(ctx, graph);
4161 /* Compute a schedule on sc->domain that respects the given schedule
4162 * constraints.
4164 * In particular, the schedule respects all the validity dependences.
4165 * If the default isl scheduling algorithm is used, it tries to minimize
4166 * the dependence distances over the proximity dependences.
4167 * If Feautrier's scheduling algorithm is used, the proximity dependence
4168 * distances are only minimized during the extension to a full-dimensional
4169 * schedule.
4171 * If there are any condition and conditional validity dependences,
4172 * then the conditional validity dependences may be violated inside
4173 * a tilable band, provided they have no adjacent non-local
4174 * condition dependences.
4176 __isl_give isl_schedule *isl_schedule_constraints_compute_schedule(
4177 __isl_take isl_schedule_constraints *sc)
4179 isl_ctx *ctx = isl_schedule_constraints_get_ctx(sc);
4180 struct isl_sched_graph graph = { 0 };
4181 isl_schedule *sched;
4182 struct isl_extract_edge_data data;
4183 enum isl_edge_type i;
4185 sc = isl_schedule_constraints_align_params(sc);
4186 if (!sc)
4187 return NULL;
4189 graph.n = isl_union_set_n_set(sc->domain);
4190 if (graph.n == 0)
4191 goto empty;
4192 if (graph_alloc(ctx, &graph, graph.n,
4193 isl_schedule_constraints_n_map(sc)) < 0)
4194 goto error;
4195 if (compute_max_row(&graph, sc) < 0)
4196 goto error;
4197 graph.root = 1;
4198 graph.n = 0;
4199 if (isl_union_set_foreach_set(sc->domain, &extract_node, &graph) < 0)
4200 goto error;
4201 if (graph_init_table(ctx, &graph) < 0)
4202 goto error;
4203 for (i = isl_edge_first; i <= isl_edge_last; ++i)
4204 graph.max_edge[i] = isl_union_map_n_map(sc->constraint[i]);
4205 if (graph_init_edge_tables(ctx, &graph) < 0)
4206 goto error;
4207 graph.n_edge = 0;
4208 data.graph = &graph;
4209 for (i = isl_edge_first; i <= isl_edge_last; ++i) {
4210 data.type = i;
4211 if (isl_union_map_foreach_map(sc->constraint[i],
4212 &extract_edge, &data) < 0)
4213 goto error;
4216 if (compute_schedule(ctx, &graph) < 0)
4217 goto error;
4219 empty:
4220 sched = extract_schedule(&graph, isl_union_set_get_space(sc->domain));
4222 graph_free(ctx, &graph);
4223 isl_schedule_constraints_free(sc);
4225 return sched;
4226 error:
4227 graph_free(ctx, &graph);
4228 isl_schedule_constraints_free(sc);
4229 return NULL;
4232 /* Compute a schedule for the given union of domains that respects
4233 * all the validity dependences and minimizes
4234 * the dependence distances over the proximity dependences.
4236 * This function is kept for backward compatibility.
4238 __isl_give isl_schedule *isl_union_set_compute_schedule(
4239 __isl_take isl_union_set *domain,
4240 __isl_take isl_union_map *validity,
4241 __isl_take isl_union_map *proximity)
4243 isl_schedule_constraints *sc;
4245 sc = isl_schedule_constraints_on_domain(domain);
4246 sc = isl_schedule_constraints_set_validity(sc, validity);
4247 sc = isl_schedule_constraints_set_proximity(sc, proximity);
4249 return isl_schedule_constraints_compute_schedule(sc);