isl_range.c: has_sign: drop unused variable
[isl.git] / isl_scheduler.c
blobc552f51c6b4efdd48fc25c0f5146dbed04f86c1a
1 /*
2 * Copyright 2011 INRIA Saclay
3 * Copyright 2012-2014 Ecole Normale Superieure
5 * Use of this software is governed by the MIT license
7 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
8 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
9 * 91893 Orsay, France
10 * and Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
13 #include <isl_ctx_private.h>
14 #include <isl_map_private.h>
15 #include <isl_space_private.h>
16 #include <isl_aff_private.h>
17 #include <isl/hash.h>
18 #include <isl/constraint.h>
19 #include <isl/schedule.h>
20 #include <isl/schedule_node.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_options_private.h>
30 #include <isl_tarjan.h>
31 #include <isl_morph.h>
34 * The scheduling algorithm implemented in this file was inspired by
35 * Bondhugula et al., "Automatic Transformations for Communication-Minimized
36 * Parallelization and Locality Optimization in the Polyhedral Model".
39 enum isl_edge_type {
40 isl_edge_validity = 0,
41 isl_edge_first = isl_edge_validity,
42 isl_edge_coincidence,
43 isl_edge_condition,
44 isl_edge_conditional_validity,
45 isl_edge_proximity,
46 isl_edge_last = isl_edge_proximity
49 /* The constraints that need to be satisfied by a schedule on "domain".
51 * "context" specifies extra constraints on the parameters.
53 * "validity" constraints map domain elements i to domain elements
54 * that should be scheduled after i. (Hard constraint)
55 * "proximity" constraints map domain elements i to domains elements
56 * that should be scheduled as early as possible after i (or before i).
57 * (Soft constraint)
59 * "condition" and "conditional_validity" constraints map possibly "tagged"
60 * domain elements i -> s to "tagged" domain elements j -> t.
61 * The elements of the "conditional_validity" constraints, but without the
62 * tags (i.e., the elements i -> j) are treated as validity constraints,
63 * except that during the construction of a tilable band,
64 * the elements of the "conditional_validity" constraints may be violated
65 * provided that all adjacent elements of the "condition" constraints
66 * are local within the band.
67 * A dependence is local within a band if domain and range are mapped
68 * to the same schedule point by the band.
70 struct isl_schedule_constraints {
71 isl_union_set *domain;
72 isl_set *context;
74 isl_union_map *constraint[isl_edge_last + 1];
77 __isl_give isl_schedule_constraints *isl_schedule_constraints_copy(
78 __isl_keep isl_schedule_constraints *sc)
80 isl_ctx *ctx;
81 isl_schedule_constraints *sc_copy;
82 enum isl_edge_type i;
84 ctx = isl_union_set_get_ctx(sc->domain);
85 sc_copy = isl_calloc_type(ctx, struct isl_schedule_constraints);
86 if (!sc_copy)
87 return NULL;
89 sc_copy->domain = isl_union_set_copy(sc->domain);
90 sc_copy->context = isl_set_copy(sc->context);
91 if (!sc_copy->domain || !sc_copy->context)
92 return isl_schedule_constraints_free(sc_copy);
94 for (i = isl_edge_first; i <= isl_edge_last; ++i) {
95 sc_copy->constraint[i] = isl_union_map_copy(sc->constraint[i]);
96 if (!sc_copy->constraint[i])
97 return isl_schedule_constraints_free(sc_copy);
100 return sc_copy;
104 /* Construct an isl_schedule_constraints object for computing a schedule
105 * on "domain". The initial object does not impose any constraints.
107 __isl_give isl_schedule_constraints *isl_schedule_constraints_on_domain(
108 __isl_take isl_union_set *domain)
110 isl_ctx *ctx;
111 isl_space *space;
112 isl_schedule_constraints *sc;
113 isl_union_map *empty;
114 enum isl_edge_type i;
116 if (!domain)
117 return NULL;
119 ctx = isl_union_set_get_ctx(domain);
120 sc = isl_calloc_type(ctx, struct isl_schedule_constraints);
121 if (!sc)
122 goto error;
124 space = isl_union_set_get_space(domain);
125 sc->domain = domain;
126 sc->context = isl_set_universe(isl_space_copy(space));
127 empty = isl_union_map_empty(space);
128 for (i = isl_edge_first; i <= isl_edge_last; ++i) {
129 sc->constraint[i] = isl_union_map_copy(empty);
130 if (!sc->constraint[i])
131 sc->domain = isl_union_set_free(sc->domain);
133 isl_union_map_free(empty);
135 if (!sc->domain || !sc->context)
136 return isl_schedule_constraints_free(sc);
138 return sc;
139 error:
140 isl_union_set_free(domain);
141 return NULL;
144 /* Replace the context of "sc" by "context".
146 __isl_give isl_schedule_constraints *isl_schedule_constraints_set_context(
147 __isl_take isl_schedule_constraints *sc, __isl_take isl_set *context)
149 if (!sc || !context)
150 goto error;
152 isl_set_free(sc->context);
153 sc->context = context;
155 return sc;
156 error:
157 isl_schedule_constraints_free(sc);
158 isl_set_free(context);
159 return NULL;
162 /* Replace the validity constraints of "sc" by "validity".
164 __isl_give isl_schedule_constraints *isl_schedule_constraints_set_validity(
165 __isl_take isl_schedule_constraints *sc,
166 __isl_take isl_union_map *validity)
168 if (!sc || !validity)
169 goto error;
171 isl_union_map_free(sc->constraint[isl_edge_validity]);
172 sc->constraint[isl_edge_validity] = validity;
174 return sc;
175 error:
176 isl_schedule_constraints_free(sc);
177 isl_union_map_free(validity);
178 return NULL;
181 /* Replace the coincidence constraints of "sc" by "coincidence".
183 __isl_give isl_schedule_constraints *isl_schedule_constraints_set_coincidence(
184 __isl_take isl_schedule_constraints *sc,
185 __isl_take isl_union_map *coincidence)
187 if (!sc || !coincidence)
188 goto error;
190 isl_union_map_free(sc->constraint[isl_edge_coincidence]);
191 sc->constraint[isl_edge_coincidence] = coincidence;
193 return sc;
194 error:
195 isl_schedule_constraints_free(sc);
196 isl_union_map_free(coincidence);
197 return NULL;
200 /* Replace the proximity constraints of "sc" by "proximity".
202 __isl_give isl_schedule_constraints *isl_schedule_constraints_set_proximity(
203 __isl_take isl_schedule_constraints *sc,
204 __isl_take isl_union_map *proximity)
206 if (!sc || !proximity)
207 goto error;
209 isl_union_map_free(sc->constraint[isl_edge_proximity]);
210 sc->constraint[isl_edge_proximity] = proximity;
212 return sc;
213 error:
214 isl_schedule_constraints_free(sc);
215 isl_union_map_free(proximity);
216 return NULL;
219 /* Replace the conditional validity constraints of "sc" by "condition"
220 * and "validity".
222 __isl_give isl_schedule_constraints *
223 isl_schedule_constraints_set_conditional_validity(
224 __isl_take isl_schedule_constraints *sc,
225 __isl_take isl_union_map *condition,
226 __isl_take isl_union_map *validity)
228 if (!sc || !condition || !validity)
229 goto error;
231 isl_union_map_free(sc->constraint[isl_edge_condition]);
232 sc->constraint[isl_edge_condition] = condition;
233 isl_union_map_free(sc->constraint[isl_edge_conditional_validity]);
234 sc->constraint[isl_edge_conditional_validity] = validity;
236 return sc;
237 error:
238 isl_schedule_constraints_free(sc);
239 isl_union_map_free(condition);
240 isl_union_map_free(validity);
241 return NULL;
244 __isl_null isl_schedule_constraints *isl_schedule_constraints_free(
245 __isl_take isl_schedule_constraints *sc)
247 enum isl_edge_type i;
249 if (!sc)
250 return NULL;
252 isl_union_set_free(sc->domain);
253 isl_set_free(sc->context);
254 for (i = isl_edge_first; i <= isl_edge_last; ++i)
255 isl_union_map_free(sc->constraint[i]);
257 free(sc);
259 return NULL;
262 isl_ctx *isl_schedule_constraints_get_ctx(
263 __isl_keep isl_schedule_constraints *sc)
265 return sc ? isl_union_set_get_ctx(sc->domain) : NULL;
268 void isl_schedule_constraints_dump(__isl_keep isl_schedule_constraints *sc)
270 if (!sc)
271 return;
273 fprintf(stderr, "domain: ");
274 isl_union_set_dump(sc->domain);
275 fprintf(stderr, "context: ");
276 isl_set_dump(sc->context);
277 fprintf(stderr, "validity: ");
278 isl_union_map_dump(sc->constraint[isl_edge_validity]);
279 fprintf(stderr, "proximity: ");
280 isl_union_map_dump(sc->constraint[isl_edge_proximity]);
281 fprintf(stderr, "coincidence: ");
282 isl_union_map_dump(sc->constraint[isl_edge_coincidence]);
283 fprintf(stderr, "condition: ");
284 isl_union_map_dump(sc->constraint[isl_edge_condition]);
285 fprintf(stderr, "conditional_validity: ");
286 isl_union_map_dump(sc->constraint[isl_edge_conditional_validity]);
289 /* Align the parameters of the fields of "sc".
291 static __isl_give isl_schedule_constraints *
292 isl_schedule_constraints_align_params(__isl_take isl_schedule_constraints *sc)
294 isl_space *space;
295 enum isl_edge_type i;
297 if (!sc)
298 return NULL;
300 space = isl_union_set_get_space(sc->domain);
301 space = isl_space_align_params(space, isl_set_get_space(sc->context));
302 for (i = isl_edge_first; i <= isl_edge_last; ++i)
303 space = isl_space_align_params(space,
304 isl_union_map_get_space(sc->constraint[i]));
306 for (i = isl_edge_first; i <= isl_edge_last; ++i) {
307 sc->constraint[i] = isl_union_map_align_params(
308 sc->constraint[i], isl_space_copy(space));
309 if (!sc->constraint[i])
310 space = isl_space_free(space);
312 sc->context = isl_set_align_params(sc->context, isl_space_copy(space));
313 sc->domain = isl_union_set_align_params(sc->domain, space);
314 if (!sc->context || !sc->domain)
315 return isl_schedule_constraints_free(sc);
317 return sc;
320 /* Return the total number of isl_maps in the constraints of "sc".
322 static __isl_give int isl_schedule_constraints_n_map(
323 __isl_keep isl_schedule_constraints *sc)
325 enum isl_edge_type i;
326 int n = 0;
328 for (i = isl_edge_first; i <= isl_edge_last; ++i)
329 n += isl_union_map_n_map(sc->constraint[i]);
331 return n;
334 /* Internal information about a node that is used during the construction
335 * of a schedule.
336 * space represents the space in which the domain lives
337 * sched is a matrix representation of the schedule being constructed
338 * for this node; if compressed is set, then this schedule is
339 * defined over the compressed domain space
340 * sched_map is an isl_map representation of the same (partial) schedule
341 * sched_map may be NULL; if compressed is set, then this map
342 * is defined over the uncompressed domain space
343 * rank is the number of linearly independent rows in the linear part
344 * of sched
345 * the columns of cmap represent a change of basis for the schedule
346 * coefficients; the first rank columns span the linear part of
347 * the schedule rows
348 * cinv is the inverse of cmap.
349 * start is the first variable in the LP problem in the sequences that
350 * represents the schedule coefficients of this node
351 * nvar is the dimension of the domain
352 * nparam is the number of parameters or 0 if we are not constructing
353 * a parametric schedule
355 * If compressed is set, then hull represents the constraints
356 * that were used to derive the compression, while compress and
357 * decompress map the original space to the compressed space and
358 * vice versa.
360 * scc is the index of SCC (or WCC) this node belongs to
362 * coincident contains a boolean for each of the rows of the schedule,
363 * indicating whether the corresponding scheduling dimension satisfies
364 * the coincidence constraints in the sense that the corresponding
365 * dependence distances are zero.
367 struct isl_sched_node {
368 isl_space *space;
369 int compressed;
370 isl_set *hull;
371 isl_multi_aff *compress;
372 isl_multi_aff *decompress;
373 isl_mat *sched;
374 isl_map *sched_map;
375 int rank;
376 isl_mat *cmap;
377 isl_mat *cinv;
378 int start;
379 int nvar;
380 int nparam;
382 int scc;
384 int *coincident;
387 static int node_has_space(const void *entry, const void *val)
389 struct isl_sched_node *node = (struct isl_sched_node *)entry;
390 isl_space *dim = (isl_space *)val;
392 return isl_space_is_equal(node->space, dim);
395 static int node_scc_exactly(struct isl_sched_node *node, int scc)
397 return node->scc == scc;
400 static int node_scc_at_most(struct isl_sched_node *node, int scc)
402 return node->scc <= scc;
405 static int node_scc_at_least(struct isl_sched_node *node, int scc)
407 return node->scc >= scc;
410 /* An edge in the dependence graph. An edge may be used to
411 * ensure validity of the generated schedule, to minimize the dependence
412 * distance or both
414 * map is the dependence relation, with i -> j in the map if j depends on i
415 * tagged_condition and tagged_validity contain the union of all tagged
416 * condition or conditional validity dependence relations that
417 * specialize the dependence relation "map"; that is,
418 * if (i -> a) -> (j -> b) is an element of "tagged_condition"
419 * or "tagged_validity", then i -> j is an element of "map".
420 * If these fields are NULL, then they represent the empty relation.
421 * src is the source node
422 * dst is the sink node
423 * validity is set if the edge is used to ensure correctness
424 * coincidence is used to enforce zero dependence distances
425 * proximity is set if the edge is used to minimize dependence distances
426 * condition is set if the edge represents a condition
427 * for a conditional validity schedule constraint
428 * local can only be set for condition edges and indicates that
429 * the dependence distance over the edge should be zero
430 * conditional_validity is set if the edge is used to conditionally
431 * ensure correctness
433 * For validity edges, start and end mark the sequence of inequality
434 * constraints in the LP problem that encode the validity constraint
435 * corresponding to this edge.
437 struct isl_sched_edge {
438 isl_map *map;
439 isl_union_map *tagged_condition;
440 isl_union_map *tagged_validity;
442 struct isl_sched_node *src;
443 struct isl_sched_node *dst;
445 unsigned validity : 1;
446 unsigned coincidence : 1;
447 unsigned proximity : 1;
448 unsigned local : 1;
449 unsigned condition : 1;
450 unsigned conditional_validity : 1;
452 int start;
453 int end;
456 /* Internal information about the dependence graph used during
457 * the construction of the schedule.
459 * intra_hmap is a cache, mapping dependence relations to their dual,
460 * for dependences from a node to itself
461 * inter_hmap is a cache, mapping dependence relations to their dual,
462 * for dependences between distinct nodes
463 * if compression is involved then the key for these maps
464 * it the original, uncompressed dependence relation, while
465 * the value is the dual of the compressed dependence relation.
467 * n is the number of nodes
468 * node is the list of nodes
469 * maxvar is the maximal number of variables over all nodes
470 * max_row is the allocated number of rows in the schedule
471 * n_row is the current (maximal) number of linearly independent
472 * rows in the node schedules
473 * n_total_row is the current number of rows in the node schedules
474 * band_start is the starting row in the node schedules of the current band
475 * root is set if this graph is the original dependence graph,
476 * without any splitting
478 * sorted contains a list of node indices sorted according to the
479 * SCC to which a node belongs
481 * n_edge is the number of edges
482 * edge is the list of edges
483 * max_edge contains the maximal number of edges of each type;
484 * in particular, it contains the number of edges in the inital graph.
485 * edge_table contains pointers into the edge array, hashed on the source
486 * and sink spaces; there is one such table for each type;
487 * a given edge may be referenced from more than one table
488 * if the corresponding relation appears in more than of the
489 * sets of dependences
491 * node_table contains pointers into the node array, hashed on the space
493 * region contains a list of variable sequences that should be non-trivial
495 * lp contains the (I)LP problem used to obtain new schedule rows
497 * src_scc and dst_scc are the source and sink SCCs of an edge with
498 * conflicting constraints
500 * scc represents the number of components
501 * weak is set if the components are weakly connected
503 struct isl_sched_graph {
504 isl_map_to_basic_set *intra_hmap;
505 isl_map_to_basic_set *inter_hmap;
507 struct isl_sched_node *node;
508 int n;
509 int maxvar;
510 int max_row;
511 int n_row;
513 int *sorted;
515 int n_total_row;
516 int band_start;
518 int root;
520 struct isl_sched_edge *edge;
521 int n_edge;
522 int max_edge[isl_edge_last + 1];
523 struct isl_hash_table *edge_table[isl_edge_last + 1];
525 struct isl_hash_table *node_table;
526 struct isl_region *region;
528 isl_basic_set *lp;
530 int src_scc;
531 int dst_scc;
533 int scc;
534 int weak;
537 /* Initialize node_table based on the list of nodes.
539 static int graph_init_table(isl_ctx *ctx, struct isl_sched_graph *graph)
541 int i;
543 graph->node_table = isl_hash_table_alloc(ctx, graph->n);
544 if (!graph->node_table)
545 return -1;
547 for (i = 0; i < graph->n; ++i) {
548 struct isl_hash_table_entry *entry;
549 uint32_t hash;
551 hash = isl_space_get_hash(graph->node[i].space);
552 entry = isl_hash_table_find(ctx, graph->node_table, hash,
553 &node_has_space,
554 graph->node[i].space, 1);
555 if (!entry)
556 return -1;
557 entry->data = &graph->node[i];
560 return 0;
563 /* Return a pointer to the node that lives within the given space,
564 * or NULL if there is no such node.
566 static struct isl_sched_node *graph_find_node(isl_ctx *ctx,
567 struct isl_sched_graph *graph, __isl_keep isl_space *dim)
569 struct isl_hash_table_entry *entry;
570 uint32_t hash;
572 hash = isl_space_get_hash(dim);
573 entry = isl_hash_table_find(ctx, graph->node_table, hash,
574 &node_has_space, dim, 0);
576 return entry ? entry->data : NULL;
579 static int edge_has_src_and_dst(const void *entry, const void *val)
581 const struct isl_sched_edge *edge = entry;
582 const struct isl_sched_edge *temp = val;
584 return edge->src == temp->src && edge->dst == temp->dst;
587 /* Add the given edge to graph->edge_table[type].
589 static int graph_edge_table_add(isl_ctx *ctx, struct isl_sched_graph *graph,
590 enum isl_edge_type type, struct isl_sched_edge *edge)
592 struct isl_hash_table_entry *entry;
593 uint32_t hash;
595 hash = isl_hash_init();
596 hash = isl_hash_builtin(hash, edge->src);
597 hash = isl_hash_builtin(hash, edge->dst);
598 entry = isl_hash_table_find(ctx, graph->edge_table[type], hash,
599 &edge_has_src_and_dst, edge, 1);
600 if (!entry)
601 return -1;
602 entry->data = edge;
604 return 0;
607 /* Allocate the edge_tables based on the maximal number of edges of
608 * each type.
610 static int graph_init_edge_tables(isl_ctx *ctx, struct isl_sched_graph *graph)
612 int i;
614 for (i = 0; i <= isl_edge_last; ++i) {
615 graph->edge_table[i] = isl_hash_table_alloc(ctx,
616 graph->max_edge[i]);
617 if (!graph->edge_table[i])
618 return -1;
621 return 0;
624 /* If graph->edge_table[type] contains an edge from the given source
625 * to the given destination, then return the hash table entry of this edge.
626 * Otherwise, return NULL.
628 static struct isl_hash_table_entry *graph_find_edge_entry(
629 struct isl_sched_graph *graph,
630 enum isl_edge_type type,
631 struct isl_sched_node *src, struct isl_sched_node *dst)
633 isl_ctx *ctx = isl_space_get_ctx(src->space);
634 uint32_t hash;
635 struct isl_sched_edge temp = { .src = src, .dst = dst };
637 hash = isl_hash_init();
638 hash = isl_hash_builtin(hash, temp.src);
639 hash = isl_hash_builtin(hash, temp.dst);
640 return isl_hash_table_find(ctx, graph->edge_table[type], hash,
641 &edge_has_src_and_dst, &temp, 0);
645 /* If graph->edge_table[type] contains an edge from the given source
646 * to the given destination, then return this edge.
647 * Otherwise, return NULL.
649 static struct isl_sched_edge *graph_find_edge(struct isl_sched_graph *graph,
650 enum isl_edge_type type,
651 struct isl_sched_node *src, struct isl_sched_node *dst)
653 struct isl_hash_table_entry *entry;
655 entry = graph_find_edge_entry(graph, type, src, dst);
656 if (!entry)
657 return NULL;
659 return entry->data;
662 /* Check whether the dependence graph has an edge of the given type
663 * between the given two nodes.
665 static int graph_has_edge(struct isl_sched_graph *graph,
666 enum isl_edge_type type,
667 struct isl_sched_node *src, struct isl_sched_node *dst)
669 struct isl_sched_edge *edge;
670 int empty;
672 edge = graph_find_edge(graph, type, src, dst);
673 if (!edge)
674 return 0;
676 empty = isl_map_plain_is_empty(edge->map);
677 if (empty < 0)
678 return -1;
680 return !empty;
683 /* Look for any edge with the same src, dst and map fields as "model".
685 * Return the matching edge if one can be found.
686 * Return "model" if no matching edge is found.
687 * Return NULL on error.
689 static struct isl_sched_edge *graph_find_matching_edge(
690 struct isl_sched_graph *graph, struct isl_sched_edge *model)
692 enum isl_edge_type i;
693 struct isl_sched_edge *edge;
695 for (i = isl_edge_first; i <= isl_edge_last; ++i) {
696 int is_equal;
698 edge = graph_find_edge(graph, i, model->src, model->dst);
699 if (!edge)
700 continue;
701 is_equal = isl_map_plain_is_equal(model->map, edge->map);
702 if (is_equal < 0)
703 return NULL;
704 if (is_equal)
705 return edge;
708 return model;
711 /* Remove the given edge from all the edge_tables that refer to it.
713 static void graph_remove_edge(struct isl_sched_graph *graph,
714 struct isl_sched_edge *edge)
716 isl_ctx *ctx = isl_map_get_ctx(edge->map);
717 enum isl_edge_type i;
719 for (i = isl_edge_first; i <= isl_edge_last; ++i) {
720 struct isl_hash_table_entry *entry;
722 entry = graph_find_edge_entry(graph, i, edge->src, edge->dst);
723 if (!entry)
724 continue;
725 if (entry->data != edge)
726 continue;
727 isl_hash_table_remove(ctx, graph->edge_table[i], entry);
731 /* Check whether the dependence graph has any edge
732 * between the given two nodes.
734 static int graph_has_any_edge(struct isl_sched_graph *graph,
735 struct isl_sched_node *src, struct isl_sched_node *dst)
737 enum isl_edge_type i;
738 int r;
740 for (i = isl_edge_first; i <= isl_edge_last; ++i) {
741 r = graph_has_edge(graph, i, src, dst);
742 if (r < 0 || r)
743 return r;
746 return r;
749 /* Check whether the dependence graph has a validity edge
750 * between the given two nodes.
752 * Conditional validity edges are essentially validity edges that
753 * can be ignored if the corresponding condition edges are iteration private.
754 * Here, we are only checking for the presence of validity
755 * edges, so we need to consider the conditional validity edges too.
756 * In particular, this function is used during the detection
757 * of strongly connected components and we cannot ignore
758 * conditional validity edges during this detection.
760 static int graph_has_validity_edge(struct isl_sched_graph *graph,
761 struct isl_sched_node *src, struct isl_sched_node *dst)
763 int r;
765 r = graph_has_edge(graph, isl_edge_validity, src, dst);
766 if (r < 0 || r)
767 return r;
769 return graph_has_edge(graph, isl_edge_conditional_validity, src, dst);
772 static int graph_alloc(isl_ctx *ctx, struct isl_sched_graph *graph,
773 int n_node, int n_edge)
775 int i;
777 graph->n = n_node;
778 graph->n_edge = n_edge;
779 graph->node = isl_calloc_array(ctx, struct isl_sched_node, graph->n);
780 graph->sorted = isl_calloc_array(ctx, int, graph->n);
781 graph->region = isl_alloc_array(ctx, struct isl_region, graph->n);
782 graph->edge = isl_calloc_array(ctx,
783 struct isl_sched_edge, graph->n_edge);
785 graph->intra_hmap = isl_map_to_basic_set_alloc(ctx, 2 * n_edge);
786 graph->inter_hmap = isl_map_to_basic_set_alloc(ctx, 2 * n_edge);
788 if (!graph->node || !graph->region || (graph->n_edge && !graph->edge) ||
789 !graph->sorted)
790 return -1;
792 for(i = 0; i < graph->n; ++i)
793 graph->sorted[i] = i;
795 return 0;
798 static void graph_free(isl_ctx *ctx, struct isl_sched_graph *graph)
800 int i;
802 isl_map_to_basic_set_free(graph->intra_hmap);
803 isl_map_to_basic_set_free(graph->inter_hmap);
805 if (graph->node)
806 for (i = 0; i < graph->n; ++i) {
807 isl_space_free(graph->node[i].space);
808 isl_set_free(graph->node[i].hull);
809 isl_multi_aff_free(graph->node[i].compress);
810 isl_multi_aff_free(graph->node[i].decompress);
811 isl_mat_free(graph->node[i].sched);
812 isl_map_free(graph->node[i].sched_map);
813 isl_mat_free(graph->node[i].cmap);
814 isl_mat_free(graph->node[i].cinv);
815 if (graph->root)
816 free(graph->node[i].coincident);
818 free(graph->node);
819 free(graph->sorted);
820 if (graph->edge)
821 for (i = 0; i < graph->n_edge; ++i) {
822 isl_map_free(graph->edge[i].map);
823 isl_union_map_free(graph->edge[i].tagged_condition);
824 isl_union_map_free(graph->edge[i].tagged_validity);
826 free(graph->edge);
827 free(graph->region);
828 for (i = 0; i <= isl_edge_last; ++i)
829 isl_hash_table_free(ctx, graph->edge_table[i]);
830 isl_hash_table_free(ctx, graph->node_table);
831 isl_basic_set_free(graph->lp);
834 /* For each "set" on which this function is called, increment
835 * graph->n by one and update graph->maxvar.
837 static int init_n_maxvar(__isl_take isl_set *set, void *user)
839 struct isl_sched_graph *graph = user;
840 int nvar = isl_set_dim(set, isl_dim_set);
842 graph->n++;
843 if (nvar > graph->maxvar)
844 graph->maxvar = nvar;
846 isl_set_free(set);
848 return 0;
851 /* Add the number of basic maps in "map" to *n.
853 static int add_n_basic_map(__isl_take isl_map *map, void *user)
855 int *n = user;
857 *n += isl_map_n_basic_map(map);
858 isl_map_free(map);
860 return 0;
863 /* Compute the number of rows that should be allocated for the schedule.
864 * In particular, we need one row for each variable or one row
865 * for each basic map in the dependences.
866 * Note that it is practically impossible to exhaust both
867 * the number of dependences and the number of variables.
869 static int compute_max_row(struct isl_sched_graph *graph,
870 __isl_keep isl_schedule_constraints *sc)
872 enum isl_edge_type i;
873 int n_edge;
875 graph->n = 0;
876 graph->maxvar = 0;
877 if (isl_union_set_foreach_set(sc->domain, &init_n_maxvar, graph) < 0)
878 return -1;
879 n_edge = 0;
880 for (i = isl_edge_first; i <= isl_edge_last; ++i)
881 if (isl_union_map_foreach_map(sc->constraint[i],
882 &add_n_basic_map, &n_edge) < 0)
883 return -1;
884 graph->max_row = n_edge + graph->maxvar;
886 return 0;
889 /* Does "bset" have any defining equalities for its set variables?
891 static int has_any_defining_equality(__isl_keep isl_basic_set *bset)
893 int i, n;
895 if (!bset)
896 return -1;
898 n = isl_basic_set_dim(bset, isl_dim_set);
899 for (i = 0; i < n; ++i) {
900 int has;
902 has = isl_basic_set_has_defining_equality(bset, isl_dim_set, i,
903 NULL);
904 if (has < 0 || has)
905 return has;
908 return 0;
911 /* Add a new node to the graph representing the given space.
912 * "nvar" is the (possibly compressed) number of variables and
913 * may be smaller than then number of set variables in "space"
914 * if "compressed" is set.
915 * If "compressed" is set, then "hull" represents the constraints
916 * that were used to derive the compression, while "compress" and
917 * "decompress" map the original space to the compressed space and
918 * vice versa.
919 * If "compressed" is not set, then "hull", "compress" and "decompress"
920 * should be NULL.
922 static int add_node(struct isl_sched_graph *graph, __isl_take isl_space *space,
923 int nvar, int compressed, __isl_take isl_set *hull,
924 __isl_take isl_multi_aff *compress,
925 __isl_take isl_multi_aff *decompress)
927 int nparam;
928 isl_ctx *ctx;
929 isl_mat *sched;
930 int *coincident;
932 if (!space)
933 return -1;
935 ctx = isl_space_get_ctx(space);
936 nparam = isl_space_dim(space, isl_dim_param);
937 if (!ctx->opt->schedule_parametric)
938 nparam = 0;
939 sched = isl_mat_alloc(ctx, 0, 1 + nparam + nvar);
940 graph->node[graph->n].space = space;
941 graph->node[graph->n].nvar = nvar;
942 graph->node[graph->n].nparam = nparam;
943 graph->node[graph->n].sched = sched;
944 graph->node[graph->n].sched_map = NULL;
945 coincident = isl_calloc_array(ctx, int, graph->max_row);
946 graph->node[graph->n].coincident = coincident;
947 graph->node[graph->n].compressed = compressed;
948 graph->node[graph->n].hull = hull;
949 graph->node[graph->n].compress = compress;
950 graph->node[graph->n].decompress = decompress;
951 graph->n++;
953 if (!space || !sched || (graph->max_row && !coincident))
954 return -1;
955 if (compressed && (!hull || !compress || !decompress))
956 return -1;
958 return 0;
961 /* Add a new node to the graph representing the given set.
963 * If any of the set variables is defined by an equality, then
964 * we perform variable compression such that we can perform
965 * the scheduling on the compressed domain.
967 static int extract_node(__isl_take isl_set *set, void *user)
969 int nvar;
970 int has_equality;
971 isl_space *space;
972 isl_basic_set *hull;
973 isl_set *hull_set;
974 isl_morph *morph;
975 isl_multi_aff *compress, *decompress;
976 struct isl_sched_graph *graph = user;
978 space = isl_set_get_space(set);
979 hull = isl_set_affine_hull(set);
980 hull = isl_basic_set_remove_divs(hull);
981 nvar = isl_space_dim(space, isl_dim_set);
982 has_equality = has_any_defining_equality(hull);
984 if (has_equality < 0)
985 goto error;
986 if (!has_equality) {
987 isl_basic_set_free(hull);
988 return add_node(graph, space, nvar, 0, NULL, NULL, NULL);
991 morph = isl_basic_set_variable_compression(hull, isl_dim_set);
992 nvar = isl_morph_ran_dim(morph, isl_dim_set);
993 compress = isl_morph_get_var_multi_aff(morph);
994 morph = isl_morph_inverse(morph);
995 decompress = isl_morph_get_var_multi_aff(morph);
996 isl_morph_free(morph);
998 hull_set = isl_set_from_basic_set(hull);
999 return add_node(graph, space, nvar, 1, hull_set, compress, decompress);
1000 error:
1001 isl_basic_set_free(hull);
1002 isl_space_free(space);
1003 return -1;
1006 struct isl_extract_edge_data {
1007 enum isl_edge_type type;
1008 struct isl_sched_graph *graph;
1011 /* Merge edge2 into edge1, freeing the contents of edge2.
1012 * "type" is the type of the schedule constraint from which edge2 was
1013 * extracted.
1014 * Return 0 on success and -1 on failure.
1016 * edge1 and edge2 are assumed to have the same value for the map field.
1018 static int merge_edge(enum isl_edge_type type, struct isl_sched_edge *edge1,
1019 struct isl_sched_edge *edge2)
1021 edge1->validity |= edge2->validity;
1022 edge1->coincidence |= edge2->coincidence;
1023 edge1->proximity |= edge2->proximity;
1024 edge1->condition |= edge2->condition;
1025 edge1->conditional_validity |= edge2->conditional_validity;
1026 isl_map_free(edge2->map);
1028 if (type == isl_edge_condition) {
1029 if (!edge1->tagged_condition)
1030 edge1->tagged_condition = edge2->tagged_condition;
1031 else
1032 edge1->tagged_condition =
1033 isl_union_map_union(edge1->tagged_condition,
1034 edge2->tagged_condition);
1037 if (type == isl_edge_conditional_validity) {
1038 if (!edge1->tagged_validity)
1039 edge1->tagged_validity = edge2->tagged_validity;
1040 else
1041 edge1->tagged_validity =
1042 isl_union_map_union(edge1->tagged_validity,
1043 edge2->tagged_validity);
1046 if (type == isl_edge_condition && !edge1->tagged_condition)
1047 return -1;
1048 if (type == isl_edge_conditional_validity && !edge1->tagged_validity)
1049 return -1;
1051 return 0;
1054 /* Insert dummy tags in domain and range of "map".
1056 * In particular, if "map" is of the form
1058 * A -> B
1060 * then return
1062 * [A -> dummy_tag] -> [B -> dummy_tag]
1064 * where the dummy_tags are identical and equal to any dummy tags
1065 * introduced by any other call to this function.
1067 static __isl_give isl_map *insert_dummy_tags(__isl_take isl_map *map)
1069 static char dummy;
1070 isl_ctx *ctx;
1071 isl_id *id;
1072 isl_space *space;
1073 isl_set *domain, *range;
1075 ctx = isl_map_get_ctx(map);
1077 id = isl_id_alloc(ctx, NULL, &dummy);
1078 space = isl_space_params(isl_map_get_space(map));
1079 space = isl_space_set_from_params(space);
1080 space = isl_space_set_tuple_id(space, isl_dim_set, id);
1081 space = isl_space_map_from_set(space);
1083 domain = isl_map_wrap(map);
1084 range = isl_map_wrap(isl_map_universe(space));
1085 map = isl_map_from_domain_and_range(domain, range);
1086 map = isl_map_zip(map);
1088 return map;
1091 /* Given that at least one of "src" or "dst" is compressed, return
1092 * a map between the spaces of these nodes restricted to the affine
1093 * hull that was used in the compression.
1095 static __isl_give isl_map *extract_hull(struct isl_sched_node *src,
1096 struct isl_sched_node *dst)
1098 isl_set *dom, *ran;
1100 if (src->compressed)
1101 dom = isl_set_copy(src->hull);
1102 else
1103 dom = isl_set_universe(isl_space_copy(src->space));
1104 if (dst->compressed)
1105 ran = isl_set_copy(dst->hull);
1106 else
1107 ran = isl_set_universe(isl_space_copy(dst->space));
1109 return isl_map_from_domain_and_range(dom, ran);
1112 /* Intersect the domains of the nested relations in domain and range
1113 * of "tagged" with "map".
1115 static __isl_give isl_map *map_intersect_domains(__isl_take isl_map *tagged,
1116 __isl_keep isl_map *map)
1118 isl_set *set;
1120 tagged = isl_map_zip(tagged);
1121 set = isl_map_wrap(isl_map_copy(map));
1122 tagged = isl_map_intersect_domain(tagged, set);
1123 tagged = isl_map_zip(tagged);
1124 return tagged;
1127 /* Add a new edge to the graph based on the given map
1128 * and add it to data->graph->edge_table[data->type].
1129 * If a dependence relation of a given type happens to be identical
1130 * to one of the dependence relations of a type that was added before,
1131 * then we don't create a new edge, but instead mark the original edge
1132 * as also representing a dependence of the current type.
1134 * Edges of type isl_edge_condition or isl_edge_conditional_validity
1135 * may be specified as "tagged" dependence relations. That is, "map"
1136 * may contain elements (i -> a) -> (j -> b), where i -> j denotes
1137 * the dependence on iterations and a and b are tags.
1138 * edge->map is set to the relation containing the elements i -> j,
1139 * while edge->tagged_condition and edge->tagged_validity contain
1140 * the union of all the "map" relations
1141 * for which extract_edge is called that result in the same edge->map.
1143 * If the source or the destination node is compressed, then
1144 * intersect both "map" and "tagged" with the constraints that
1145 * were used to construct the compression.
1146 * This ensures that there are no schedule constraints defined
1147 * outside of these domains, while the scheduler no longer has
1148 * any control over those outside parts.
1150 static int extract_edge(__isl_take isl_map *map, void *user)
1152 isl_ctx *ctx = isl_map_get_ctx(map);
1153 struct isl_extract_edge_data *data = user;
1154 struct isl_sched_graph *graph = data->graph;
1155 struct isl_sched_node *src, *dst;
1156 isl_space *dim;
1157 struct isl_sched_edge *edge;
1158 isl_map *tagged = NULL;
1160 if (data->type == isl_edge_condition ||
1161 data->type == isl_edge_conditional_validity) {
1162 if (isl_map_can_zip(map)) {
1163 tagged = isl_map_copy(map);
1164 map = isl_set_unwrap(isl_map_domain(isl_map_zip(map)));
1165 } else {
1166 tagged = insert_dummy_tags(isl_map_copy(map));
1170 dim = isl_space_domain(isl_map_get_space(map));
1171 src = graph_find_node(ctx, graph, dim);
1172 isl_space_free(dim);
1173 dim = isl_space_range(isl_map_get_space(map));
1174 dst = graph_find_node(ctx, graph, dim);
1175 isl_space_free(dim);
1177 if (!src || !dst) {
1178 isl_map_free(map);
1179 isl_map_free(tagged);
1180 return 0;
1183 if (src->compressed || dst->compressed) {
1184 isl_map *hull;
1185 hull = extract_hull(src, dst);
1186 if (tagged)
1187 tagged = map_intersect_domains(tagged, hull);
1188 map = isl_map_intersect(map, hull);
1191 graph->edge[graph->n_edge].src = src;
1192 graph->edge[graph->n_edge].dst = dst;
1193 graph->edge[graph->n_edge].map = map;
1194 graph->edge[graph->n_edge].validity = 0;
1195 graph->edge[graph->n_edge].coincidence = 0;
1196 graph->edge[graph->n_edge].proximity = 0;
1197 graph->edge[graph->n_edge].condition = 0;
1198 graph->edge[graph->n_edge].local = 0;
1199 graph->edge[graph->n_edge].conditional_validity = 0;
1200 graph->edge[graph->n_edge].tagged_condition = NULL;
1201 graph->edge[graph->n_edge].tagged_validity = NULL;
1202 if (data->type == isl_edge_validity)
1203 graph->edge[graph->n_edge].validity = 1;
1204 if (data->type == isl_edge_coincidence)
1205 graph->edge[graph->n_edge].coincidence = 1;
1206 if (data->type == isl_edge_proximity)
1207 graph->edge[graph->n_edge].proximity = 1;
1208 if (data->type == isl_edge_condition) {
1209 graph->edge[graph->n_edge].condition = 1;
1210 graph->edge[graph->n_edge].tagged_condition =
1211 isl_union_map_from_map(tagged);
1213 if (data->type == isl_edge_conditional_validity) {
1214 graph->edge[graph->n_edge].conditional_validity = 1;
1215 graph->edge[graph->n_edge].tagged_validity =
1216 isl_union_map_from_map(tagged);
1219 edge = graph_find_matching_edge(graph, &graph->edge[graph->n_edge]);
1220 if (!edge) {
1221 graph->n_edge++;
1222 return -1;
1224 if (edge == &graph->edge[graph->n_edge])
1225 return graph_edge_table_add(ctx, graph, data->type,
1226 &graph->edge[graph->n_edge++]);
1228 if (merge_edge(data->type, edge, &graph->edge[graph->n_edge]) < 0)
1229 return -1;
1231 return graph_edge_table_add(ctx, graph, data->type, edge);
1234 /* Check whether there is any dependence from node[j] to node[i]
1235 * or from node[i] to node[j].
1237 static int node_follows_weak(int i, int j, void *user)
1239 int f;
1240 struct isl_sched_graph *graph = user;
1242 f = graph_has_any_edge(graph, &graph->node[j], &graph->node[i]);
1243 if (f < 0 || f)
1244 return f;
1245 return graph_has_any_edge(graph, &graph->node[i], &graph->node[j]);
1248 /* Check whether there is a (conditional) validity dependence from node[j]
1249 * to node[i], forcing node[i] to follow node[j].
1251 static int node_follows_strong(int i, int j, void *user)
1253 struct isl_sched_graph *graph = user;
1255 return graph_has_validity_edge(graph, &graph->node[j], &graph->node[i]);
1258 /* Use Tarjan's algorithm for computing the strongly connected components
1259 * in the dependence graph (only validity edges).
1260 * If weak is set, we consider the graph to be undirected and
1261 * we effectively compute the (weakly) connected components.
1262 * Additionally, we also consider other edges when weak is set.
1264 static int detect_ccs(isl_ctx *ctx, struct isl_sched_graph *graph, int weak)
1266 int i, n;
1267 struct isl_tarjan_graph *g = NULL;
1269 g = isl_tarjan_graph_init(ctx, graph->n,
1270 weak ? &node_follows_weak : &node_follows_strong, graph);
1271 if (!g)
1272 return -1;
1274 graph->weak = weak;
1275 graph->scc = 0;
1276 i = 0;
1277 n = graph->n;
1278 while (n) {
1279 while (g->order[i] != -1) {
1280 graph->node[g->order[i]].scc = graph->scc;
1281 --n;
1282 ++i;
1284 ++i;
1285 graph->scc++;
1288 isl_tarjan_graph_free(g);
1290 return 0;
1293 /* Apply Tarjan's algorithm to detect the strongly connected components
1294 * in the dependence graph.
1296 static int detect_sccs(isl_ctx *ctx, struct isl_sched_graph *graph)
1298 return detect_ccs(ctx, graph, 0);
1301 /* Apply Tarjan's algorithm to detect the (weakly) connected components
1302 * in the dependence graph.
1304 static int detect_wccs(isl_ctx *ctx, struct isl_sched_graph *graph)
1306 return detect_ccs(ctx, graph, 1);
1309 static int cmp_scc(const void *a, const void *b, void *data)
1311 struct isl_sched_graph *graph = data;
1312 const int *i1 = a;
1313 const int *i2 = b;
1315 return graph->node[*i1].scc - graph->node[*i2].scc;
1318 /* Sort the elements of graph->sorted according to the corresponding SCCs.
1320 static int sort_sccs(struct isl_sched_graph *graph)
1322 return isl_sort(graph->sorted, graph->n, sizeof(int), &cmp_scc, graph);
1325 /* Given a dependence relation R from "node" to itself,
1326 * construct the set of coefficients of valid constraints for elements
1327 * in that dependence relation.
1328 * In particular, the result contains tuples of coefficients
1329 * c_0, c_n, c_x such that
1331 * c_0 + c_n n + c_x y - c_x x >= 0 for each (x,y) in R
1333 * or, equivalently,
1335 * c_0 + c_n n + c_x d >= 0 for each d in delta R = { y - x | (x,y) in R }
1337 * We choose here to compute the dual of delta R.
1338 * Alternatively, we could have computed the dual of R, resulting
1339 * in a set of tuples c_0, c_n, c_x, c_y, and then
1340 * plugged in (c_0, c_n, c_x, -c_x).
1342 * If "node" has been compressed, then the dependence relation
1343 * is also compressed before the set of coefficients is computed.
1345 static __isl_give isl_basic_set *intra_coefficients(
1346 struct isl_sched_graph *graph, struct isl_sched_node *node,
1347 __isl_take isl_map *map)
1349 isl_set *delta;
1350 isl_map *key;
1351 isl_basic_set *coef;
1353 if (isl_map_to_basic_set_has(graph->intra_hmap, map))
1354 return isl_map_to_basic_set_get(graph->intra_hmap, map);
1356 key = isl_map_copy(map);
1357 if (node->compressed) {
1358 map = isl_map_preimage_domain_multi_aff(map,
1359 isl_multi_aff_copy(node->decompress));
1360 map = isl_map_preimage_range_multi_aff(map,
1361 isl_multi_aff_copy(node->decompress));
1363 delta = isl_set_remove_divs(isl_map_deltas(map));
1364 coef = isl_set_coefficients(delta);
1365 graph->intra_hmap = isl_map_to_basic_set_set(graph->intra_hmap, key,
1366 isl_basic_set_copy(coef));
1368 return coef;
1371 /* Given a dependence relation R, construct the set of coefficients
1372 * of valid constraints for elements in that dependence relation.
1373 * In particular, the result contains tuples of coefficients
1374 * c_0, c_n, c_x, c_y such that
1376 * c_0 + c_n n + c_x x + c_y y >= 0 for each (x,y) in R
1378 * If the source or destination nodes of "edge" have been compressed,
1379 * then the dependence relation is also compressed before
1380 * the set of coefficients is computed.
1382 static __isl_give isl_basic_set *inter_coefficients(
1383 struct isl_sched_graph *graph, struct isl_sched_edge *edge,
1384 __isl_take isl_map *map)
1386 isl_set *set;
1387 isl_map *key;
1388 isl_basic_set *coef;
1390 if (isl_map_to_basic_set_has(graph->inter_hmap, map))
1391 return isl_map_to_basic_set_get(graph->inter_hmap, map);
1393 key = isl_map_copy(map);
1394 if (edge->src->compressed)
1395 map = isl_map_preimage_domain_multi_aff(map,
1396 isl_multi_aff_copy(edge->src->decompress));
1397 if (edge->dst->compressed)
1398 map = isl_map_preimage_range_multi_aff(map,
1399 isl_multi_aff_copy(edge->dst->decompress));
1400 set = isl_map_wrap(isl_map_remove_divs(map));
1401 coef = isl_set_coefficients(set);
1402 graph->inter_hmap = isl_map_to_basic_set_set(graph->inter_hmap, key,
1403 isl_basic_set_copy(coef));
1405 return coef;
1408 /* Add constraints to graph->lp that force validity for the given
1409 * dependence from a node i to itself.
1410 * That is, add constraints that enforce
1412 * (c_i_0 + c_i_n n + c_i_x y) - (c_i_0 + c_i_n n + c_i_x x)
1413 * = c_i_x (y - x) >= 0
1415 * for each (x,y) in R.
1416 * We obtain general constraints on coefficients (c_0, c_n, c_x)
1417 * of valid constraints for (y - x) and then plug in (0, 0, c_i_x^+ - c_i_x^-),
1418 * where c_i_x = c_i_x^+ - c_i_x^-, with c_i_x^+ and c_i_x^- non-negative.
1419 * In graph->lp, the c_i_x^- appear before their c_i_x^+ counterpart.
1421 * Actually, we do not construct constraints for the c_i_x themselves,
1422 * but for the coefficients of c_i_x written as a linear combination
1423 * of the columns in node->cmap.
1425 static int add_intra_validity_constraints(struct isl_sched_graph *graph,
1426 struct isl_sched_edge *edge)
1428 unsigned total;
1429 isl_map *map = isl_map_copy(edge->map);
1430 isl_ctx *ctx = isl_map_get_ctx(map);
1431 isl_space *dim;
1432 isl_dim_map *dim_map;
1433 isl_basic_set *coef;
1434 struct isl_sched_node *node = edge->src;
1436 coef = intra_coefficients(graph, node, map);
1438 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
1440 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
1441 isl_space_dim(dim, isl_dim_set), isl_mat_copy(node->cmap));
1442 if (!coef)
1443 goto error;
1445 total = isl_basic_set_total_dim(graph->lp);
1446 dim_map = isl_dim_map_alloc(ctx, total);
1447 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 1, 2,
1448 isl_space_dim(dim, isl_dim_set), 1,
1449 node->nvar, -1);
1450 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 2, 2,
1451 isl_space_dim(dim, isl_dim_set), 1,
1452 node->nvar, 1);
1453 graph->lp = isl_basic_set_extend_constraints(graph->lp,
1454 coef->n_eq, coef->n_ineq);
1455 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
1456 coef, dim_map);
1457 isl_space_free(dim);
1459 return 0;
1460 error:
1461 isl_space_free(dim);
1462 return -1;
1465 /* Add constraints to graph->lp that force validity for the given
1466 * dependence from node i to node j.
1467 * That is, add constraints that enforce
1469 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x) >= 0
1471 * for each (x,y) in R.
1472 * We obtain general constraints on coefficients (c_0, c_n, c_x, c_y)
1473 * of valid constraints for R and then plug in
1474 * (c_j_0 - c_i_0, c_j_n^+ - c_j_n^- - (c_i_n^+ - c_i_n^-),
1475 * c_j_x^+ - c_j_x^- - (c_i_x^+ - c_i_x^-)),
1476 * where c_* = c_*^+ - c_*^-, with c_*^+ and c_*^- non-negative.
1477 * In graph->lp, the c_*^- appear before their c_*^+ counterpart.
1479 * Actually, we do not construct constraints for the c_*_x themselves,
1480 * but for the coefficients of c_*_x written as a linear combination
1481 * of the columns in node->cmap.
1483 static int add_inter_validity_constraints(struct isl_sched_graph *graph,
1484 struct isl_sched_edge *edge)
1486 unsigned total;
1487 isl_map *map = isl_map_copy(edge->map);
1488 isl_ctx *ctx = isl_map_get_ctx(map);
1489 isl_space *dim;
1490 isl_dim_map *dim_map;
1491 isl_basic_set *coef;
1492 struct isl_sched_node *src = edge->src;
1493 struct isl_sched_node *dst = edge->dst;
1495 coef = inter_coefficients(graph, edge, map);
1497 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
1499 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
1500 isl_space_dim(dim, isl_dim_set), isl_mat_copy(src->cmap));
1501 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
1502 isl_space_dim(dim, isl_dim_set) + src->nvar,
1503 isl_mat_copy(dst->cmap));
1504 if (!coef)
1505 goto error;
1507 total = isl_basic_set_total_dim(graph->lp);
1508 dim_map = isl_dim_map_alloc(ctx, total);
1510 isl_dim_map_range(dim_map, dst->start, 0, 0, 0, 1, 1);
1511 isl_dim_map_range(dim_map, dst->start + 1, 2, 1, 1, dst->nparam, -1);
1512 isl_dim_map_range(dim_map, dst->start + 2, 2, 1, 1, dst->nparam, 1);
1513 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 1, 2,
1514 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
1515 dst->nvar, -1);
1516 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 2, 2,
1517 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
1518 dst->nvar, 1);
1520 isl_dim_map_range(dim_map, src->start, 0, 0, 0, 1, -1);
1521 isl_dim_map_range(dim_map, src->start + 1, 2, 1, 1, src->nparam, 1);
1522 isl_dim_map_range(dim_map, src->start + 2, 2, 1, 1, src->nparam, -1);
1523 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 1, 2,
1524 isl_space_dim(dim, isl_dim_set), 1,
1525 src->nvar, 1);
1526 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 2, 2,
1527 isl_space_dim(dim, isl_dim_set), 1,
1528 src->nvar, -1);
1530 edge->start = graph->lp->n_ineq;
1531 graph->lp = isl_basic_set_extend_constraints(graph->lp,
1532 coef->n_eq, coef->n_ineq);
1533 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
1534 coef, dim_map);
1535 if (!graph->lp)
1536 goto error;
1537 isl_space_free(dim);
1538 edge->end = graph->lp->n_ineq;
1540 return 0;
1541 error:
1542 isl_space_free(dim);
1543 return -1;
1546 /* Add constraints to graph->lp that bound the dependence distance for the given
1547 * dependence from a node i to itself.
1548 * If s = 1, we add the constraint
1550 * c_i_x (y - x) <= m_0 + m_n n
1552 * or
1554 * -c_i_x (y - x) + m_0 + m_n n >= 0
1556 * for each (x,y) in R.
1557 * If s = -1, we add the constraint
1559 * -c_i_x (y - x) <= m_0 + m_n n
1561 * or
1563 * c_i_x (y - x) + m_0 + m_n n >= 0
1565 * for each (x,y) in R.
1566 * We obtain general constraints on coefficients (c_0, c_n, c_x)
1567 * of valid constraints for (y - x) and then plug in (m_0, m_n, -s * c_i_x),
1568 * with each coefficient (except m_0) represented as a pair of non-negative
1569 * coefficients.
1571 * Actually, we do not construct constraints for the c_i_x themselves,
1572 * but for the coefficients of c_i_x written as a linear combination
1573 * of the columns in node->cmap.
1576 * If "local" is set, then we add constraints
1578 * c_i_x (y - x) <= 0
1580 * or
1582 * -c_i_x (y - x) <= 0
1584 * instead, forcing the dependence distance to be (less than or) equal to 0.
1585 * That is, we plug in (0, 0, -s * c_i_x),
1586 * Note that dependences marked local are treated as validity constraints
1587 * by add_all_validity_constraints and therefore also have
1588 * their distances bounded by 0 from below.
1590 static int add_intra_proximity_constraints(struct isl_sched_graph *graph,
1591 struct isl_sched_edge *edge, int s, int local)
1593 unsigned total;
1594 unsigned nparam;
1595 isl_map *map = isl_map_copy(edge->map);
1596 isl_ctx *ctx = isl_map_get_ctx(map);
1597 isl_space *dim;
1598 isl_dim_map *dim_map;
1599 isl_basic_set *coef;
1600 struct isl_sched_node *node = edge->src;
1602 coef = intra_coefficients(graph, node, map);
1604 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
1606 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
1607 isl_space_dim(dim, isl_dim_set), isl_mat_copy(node->cmap));
1608 if (!coef)
1609 goto error;
1611 nparam = isl_space_dim(node->space, isl_dim_param);
1612 total = isl_basic_set_total_dim(graph->lp);
1613 dim_map = isl_dim_map_alloc(ctx, total);
1615 if (!local) {
1616 isl_dim_map_range(dim_map, 1, 0, 0, 0, 1, 1);
1617 isl_dim_map_range(dim_map, 4, 2, 1, 1, nparam, -1);
1618 isl_dim_map_range(dim_map, 5, 2, 1, 1, nparam, 1);
1620 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 1, 2,
1621 isl_space_dim(dim, isl_dim_set), 1,
1622 node->nvar, s);
1623 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 2, 2,
1624 isl_space_dim(dim, isl_dim_set), 1,
1625 node->nvar, -s);
1626 graph->lp = isl_basic_set_extend_constraints(graph->lp,
1627 coef->n_eq, coef->n_ineq);
1628 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
1629 coef, dim_map);
1630 isl_space_free(dim);
1632 return 0;
1633 error:
1634 isl_space_free(dim);
1635 return -1;
1638 /* Add constraints to graph->lp that bound the dependence distance for the given
1639 * dependence from node i to node j.
1640 * If s = 1, we add the constraint
1642 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x)
1643 * <= m_0 + m_n n
1645 * or
1647 * -(c_j_0 + c_j_n n + c_j_x y) + (c_i_0 + c_i_n n + c_i_x x) +
1648 * m_0 + m_n n >= 0
1650 * for each (x,y) in R.
1651 * If s = -1, we add the constraint
1653 * -((c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x))
1654 * <= m_0 + m_n n
1656 * or
1658 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x) +
1659 * m_0 + m_n n >= 0
1661 * for each (x,y) in R.
1662 * We obtain general constraints on coefficients (c_0, c_n, c_x, c_y)
1663 * of valid constraints for R and then plug in
1664 * (m_0 - s*c_j_0 + s*c_i_0, m_n - s*c_j_n + s*c_i_n,
1665 * -s*c_j_x+s*c_i_x)
1666 * with each coefficient (except m_0, c_j_0 and c_i_0)
1667 * represented as a pair of non-negative coefficients.
1669 * Actually, we do not construct constraints for the c_*_x themselves,
1670 * but for the coefficients of c_*_x written as a linear combination
1671 * of the columns in node->cmap.
1674 * If "local" is set, then we add constraints
1676 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x) <= 0
1678 * or
1680 * -((c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x)) <= 0
1682 * instead, forcing the dependence distance to be (less than or) equal to 0.
1683 * That is, we plug in
1684 * (-s*c_j_0 + s*c_i_0, -s*c_j_n + s*c_i_n, -s*c_j_x+s*c_i_x).
1685 * Note that dependences marked local are treated as validity constraints
1686 * by add_all_validity_constraints and therefore also have
1687 * their distances bounded by 0 from below.
1689 static int add_inter_proximity_constraints(struct isl_sched_graph *graph,
1690 struct isl_sched_edge *edge, int s, int local)
1692 unsigned total;
1693 unsigned nparam;
1694 isl_map *map = isl_map_copy(edge->map);
1695 isl_ctx *ctx = isl_map_get_ctx(map);
1696 isl_space *dim;
1697 isl_dim_map *dim_map;
1698 isl_basic_set *coef;
1699 struct isl_sched_node *src = edge->src;
1700 struct isl_sched_node *dst = edge->dst;
1702 coef = inter_coefficients(graph, edge, map);
1704 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
1706 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
1707 isl_space_dim(dim, isl_dim_set), isl_mat_copy(src->cmap));
1708 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
1709 isl_space_dim(dim, isl_dim_set) + src->nvar,
1710 isl_mat_copy(dst->cmap));
1711 if (!coef)
1712 goto error;
1714 nparam = isl_space_dim(src->space, isl_dim_param);
1715 total = isl_basic_set_total_dim(graph->lp);
1716 dim_map = isl_dim_map_alloc(ctx, total);
1718 if (!local) {
1719 isl_dim_map_range(dim_map, 1, 0, 0, 0, 1, 1);
1720 isl_dim_map_range(dim_map, 4, 2, 1, 1, nparam, -1);
1721 isl_dim_map_range(dim_map, 5, 2, 1, 1, nparam, 1);
1724 isl_dim_map_range(dim_map, dst->start, 0, 0, 0, 1, -s);
1725 isl_dim_map_range(dim_map, dst->start + 1, 2, 1, 1, dst->nparam, s);
1726 isl_dim_map_range(dim_map, dst->start + 2, 2, 1, 1, dst->nparam, -s);
1727 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 1, 2,
1728 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
1729 dst->nvar, s);
1730 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 2, 2,
1731 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
1732 dst->nvar, -s);
1734 isl_dim_map_range(dim_map, src->start, 0, 0, 0, 1, s);
1735 isl_dim_map_range(dim_map, src->start + 1, 2, 1, 1, src->nparam, -s);
1736 isl_dim_map_range(dim_map, src->start + 2, 2, 1, 1, src->nparam, s);
1737 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 1, 2,
1738 isl_space_dim(dim, isl_dim_set), 1,
1739 src->nvar, -s);
1740 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 2, 2,
1741 isl_space_dim(dim, isl_dim_set), 1,
1742 src->nvar, s);
1744 graph->lp = isl_basic_set_extend_constraints(graph->lp,
1745 coef->n_eq, coef->n_ineq);
1746 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
1747 coef, dim_map);
1748 isl_space_free(dim);
1750 return 0;
1751 error:
1752 isl_space_free(dim);
1753 return -1;
1756 /* Add all validity constraints to graph->lp.
1758 * An edge that is forced to be local needs to have its dependence
1759 * distances equal to zero. We take care of bounding them by 0 from below
1760 * here. add_all_proximity_constraints takes care of bounding them by 0
1761 * from above.
1763 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
1764 * Otherwise, we ignore them.
1766 static int add_all_validity_constraints(struct isl_sched_graph *graph,
1767 int use_coincidence)
1769 int i;
1771 for (i = 0; i < graph->n_edge; ++i) {
1772 struct isl_sched_edge *edge= &graph->edge[i];
1773 int local;
1775 local = edge->local || (edge->coincidence && use_coincidence);
1776 if (!edge->validity && !local)
1777 continue;
1778 if (edge->src != edge->dst)
1779 continue;
1780 if (add_intra_validity_constraints(graph, edge) < 0)
1781 return -1;
1784 for (i = 0; i < graph->n_edge; ++i) {
1785 struct isl_sched_edge *edge = &graph->edge[i];
1786 int local;
1788 local = edge->local || (edge->coincidence && use_coincidence);
1789 if (!edge->validity && !local)
1790 continue;
1791 if (edge->src == edge->dst)
1792 continue;
1793 if (add_inter_validity_constraints(graph, edge) < 0)
1794 return -1;
1797 return 0;
1800 /* Add constraints to graph->lp that bound the dependence distance
1801 * for all dependence relations.
1802 * If a given proximity dependence is identical to a validity
1803 * dependence, then the dependence distance is already bounded
1804 * from below (by zero), so we only need to bound the distance
1805 * from above. (This includes the case of "local" dependences
1806 * which are treated as validity dependence by add_all_validity_constraints.)
1807 * Otherwise, we need to bound the distance both from above and from below.
1809 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
1810 * Otherwise, we ignore them.
1812 static int add_all_proximity_constraints(struct isl_sched_graph *graph,
1813 int use_coincidence)
1815 int i;
1817 for (i = 0; i < graph->n_edge; ++i) {
1818 struct isl_sched_edge *edge= &graph->edge[i];
1819 int local;
1821 local = edge->local || (edge->coincidence && use_coincidence);
1822 if (!edge->proximity && !local)
1823 continue;
1824 if (edge->src == edge->dst &&
1825 add_intra_proximity_constraints(graph, edge, 1, local) < 0)
1826 return -1;
1827 if (edge->src != edge->dst &&
1828 add_inter_proximity_constraints(graph, edge, 1, local) < 0)
1829 return -1;
1830 if (edge->validity || local)
1831 continue;
1832 if (edge->src == edge->dst &&
1833 add_intra_proximity_constraints(graph, edge, -1, 0) < 0)
1834 return -1;
1835 if (edge->src != edge->dst &&
1836 add_inter_proximity_constraints(graph, edge, -1, 0) < 0)
1837 return -1;
1840 return 0;
1843 /* Compute a basis for the rows in the linear part of the schedule
1844 * and extend this basis to a full basis. The remaining rows
1845 * can then be used to force linear independence from the rows
1846 * in the schedule.
1848 * In particular, given the schedule rows S, we compute
1850 * S = H Q
1851 * S U = H
1853 * with H the Hermite normal form of S. That is, all but the
1854 * first rank columns of H are zero and so each row in S is
1855 * a linear combination of the first rank rows of Q.
1856 * The matrix Q is then transposed because we will write the
1857 * coefficients of the next schedule row as a column vector s
1858 * and express this s as a linear combination s = Q c of the
1859 * computed basis.
1860 * Similarly, the matrix U is transposed such that we can
1861 * compute the coefficients c = U s from a schedule row s.
1863 static int node_update_cmap(struct isl_sched_node *node)
1865 isl_mat *H, *U, *Q;
1866 int n_row = isl_mat_rows(node->sched);
1868 H = isl_mat_sub_alloc(node->sched, 0, n_row,
1869 1 + node->nparam, node->nvar);
1871 H = isl_mat_left_hermite(H, 0, &U, &Q);
1872 isl_mat_free(node->cmap);
1873 isl_mat_free(node->cinv);
1874 node->cmap = isl_mat_transpose(Q);
1875 node->cinv = isl_mat_transpose(U);
1876 node->rank = isl_mat_initial_non_zero_cols(H);
1877 isl_mat_free(H);
1879 if (!node->cmap || !node->cinv || node->rank < 0)
1880 return -1;
1881 return 0;
1884 /* How many times should we count the constraints in "edge"?
1886 * If carry is set, then we are counting the number of
1887 * (validity or conditional validity) constraints that will be added
1888 * in setup_carry_lp and we count each edge exactly once.
1890 * Otherwise, we count as follows
1891 * validity -> 1 (>= 0)
1892 * validity+proximity -> 2 (>= 0 and upper bound)
1893 * proximity -> 2 (lower and upper bound)
1894 * local(+any) -> 2 (>= 0 and <= 0)
1896 * If an edge is only marked conditional_validity then it counts
1897 * as zero since it is only checked afterwards.
1899 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
1900 * Otherwise, we ignore them.
1902 static int edge_multiplicity(struct isl_sched_edge *edge, int carry,
1903 int use_coincidence)
1905 if (carry && !edge->validity && !edge->conditional_validity)
1906 return 0;
1907 if (carry)
1908 return 1;
1909 if (edge->proximity || edge->local)
1910 return 2;
1911 if (use_coincidence && edge->coincidence)
1912 return 2;
1913 if (edge->validity)
1914 return 1;
1915 return 0;
1918 /* Count the number of equality and inequality constraints
1919 * that will be added for the given map.
1921 * "use_coincidence" is set if we should take into account coincidence edges.
1923 static int count_map_constraints(struct isl_sched_graph *graph,
1924 struct isl_sched_edge *edge, __isl_take isl_map *map,
1925 int *n_eq, int *n_ineq, int carry, int use_coincidence)
1927 isl_basic_set *coef;
1928 int f = edge_multiplicity(edge, carry, use_coincidence);
1930 if (f == 0) {
1931 isl_map_free(map);
1932 return 0;
1935 if (edge->src == edge->dst)
1936 coef = intra_coefficients(graph, edge->src, map);
1937 else
1938 coef = inter_coefficients(graph, edge, map);
1939 if (!coef)
1940 return -1;
1941 *n_eq += f * coef->n_eq;
1942 *n_ineq += f * coef->n_ineq;
1943 isl_basic_set_free(coef);
1945 return 0;
1948 /* Count the number of equality and inequality constraints
1949 * that will be added to the main lp problem.
1950 * We count as follows
1951 * validity -> 1 (>= 0)
1952 * validity+proximity -> 2 (>= 0 and upper bound)
1953 * proximity -> 2 (lower and upper bound)
1954 * local(+any) -> 2 (>= 0 and <= 0)
1956 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
1957 * Otherwise, we ignore them.
1959 static int count_constraints(struct isl_sched_graph *graph,
1960 int *n_eq, int *n_ineq, int use_coincidence)
1962 int i;
1964 *n_eq = *n_ineq = 0;
1965 for (i = 0; i < graph->n_edge; ++i) {
1966 struct isl_sched_edge *edge= &graph->edge[i];
1967 isl_map *map = isl_map_copy(edge->map);
1969 if (count_map_constraints(graph, edge, map, n_eq, n_ineq,
1970 0, use_coincidence) < 0)
1971 return -1;
1974 return 0;
1977 /* Count the number of constraints that will be added by
1978 * add_bound_coefficient_constraints and increment *n_eq and *n_ineq
1979 * accordingly.
1981 * In practice, add_bound_coefficient_constraints only adds inequalities.
1983 static int count_bound_coefficient_constraints(isl_ctx *ctx,
1984 struct isl_sched_graph *graph, int *n_eq, int *n_ineq)
1986 int i;
1988 if (ctx->opt->schedule_max_coefficient == -1)
1989 return 0;
1991 for (i = 0; i < graph->n; ++i)
1992 *n_ineq += 2 * graph->node[i].nparam + 2 * graph->node[i].nvar;
1994 return 0;
1997 /* Add constraints that bound the values of the variable and parameter
1998 * coefficients of the schedule.
2000 * The maximal value of the coefficients is defined by the option
2001 * 'schedule_max_coefficient'.
2003 static int add_bound_coefficient_constraints(isl_ctx *ctx,
2004 struct isl_sched_graph *graph)
2006 int i, j, k;
2007 int max_coefficient;
2008 int total;
2010 max_coefficient = ctx->opt->schedule_max_coefficient;
2012 if (max_coefficient == -1)
2013 return 0;
2015 total = isl_basic_set_total_dim(graph->lp);
2017 for (i = 0; i < graph->n; ++i) {
2018 struct isl_sched_node *node = &graph->node[i];
2019 for (j = 0; j < 2 * node->nparam + 2 * node->nvar; ++j) {
2020 int dim;
2021 k = isl_basic_set_alloc_inequality(graph->lp);
2022 if (k < 0)
2023 return -1;
2024 dim = 1 + node->start + 1 + j;
2025 isl_seq_clr(graph->lp->ineq[k], 1 + total);
2026 isl_int_set_si(graph->lp->ineq[k][dim], -1);
2027 isl_int_set_si(graph->lp->ineq[k][0], max_coefficient);
2031 return 0;
2034 /* Construct an ILP problem for finding schedule coefficients
2035 * that result in non-negative, but small dependence distances
2036 * over all dependences.
2037 * In particular, the dependence distances over proximity edges
2038 * are bounded by m_0 + m_n n and we compute schedule coefficients
2039 * with small values (preferably zero) of m_n and m_0.
2041 * All variables of the ILP are non-negative. The actual coefficients
2042 * may be negative, so each coefficient is represented as the difference
2043 * of two non-negative variables. The negative part always appears
2044 * immediately before the positive part.
2045 * Other than that, the variables have the following order
2047 * - sum of positive and negative parts of m_n coefficients
2048 * - m_0
2049 * - sum of positive and negative parts of all c_n coefficients
2050 * (unconstrained when computing non-parametric schedules)
2051 * - sum of positive and negative parts of all c_x coefficients
2052 * - positive and negative parts of m_n coefficients
2053 * - for each node
2054 * - c_i_0
2055 * - positive and negative parts of c_i_n (if parametric)
2056 * - positive and negative parts of c_i_x
2058 * The c_i_x are not represented directly, but through the columns of
2059 * node->cmap. That is, the computed values are for variable t_i_x
2060 * such that c_i_x = Q t_i_x with Q equal to node->cmap.
2062 * The constraints are those from the edges plus two or three equalities
2063 * to express the sums.
2065 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
2066 * Otherwise, we ignore them.
2068 static int setup_lp(isl_ctx *ctx, struct isl_sched_graph *graph,
2069 int use_coincidence)
2071 int i, j;
2072 int k;
2073 unsigned nparam;
2074 unsigned total;
2075 isl_space *dim;
2076 int parametric;
2077 int param_pos;
2078 int n_eq, n_ineq;
2079 int max_constant_term;
2081 max_constant_term = ctx->opt->schedule_max_constant_term;
2083 parametric = ctx->opt->schedule_parametric;
2084 nparam = isl_space_dim(graph->node[0].space, isl_dim_param);
2085 param_pos = 4;
2086 total = param_pos + 2 * nparam;
2087 for (i = 0; i < graph->n; ++i) {
2088 struct isl_sched_node *node = &graph->node[graph->sorted[i]];
2089 if (node_update_cmap(node) < 0)
2090 return -1;
2091 node->start = total;
2092 total += 1 + 2 * (node->nparam + node->nvar);
2095 if (count_constraints(graph, &n_eq, &n_ineq, use_coincidence) < 0)
2096 return -1;
2097 if (count_bound_coefficient_constraints(ctx, graph, &n_eq, &n_ineq) < 0)
2098 return -1;
2100 dim = isl_space_set_alloc(ctx, 0, total);
2101 isl_basic_set_free(graph->lp);
2102 n_eq += 2 + parametric;
2103 if (max_constant_term != -1)
2104 n_ineq += graph->n;
2106 graph->lp = isl_basic_set_alloc_space(dim, 0, n_eq, n_ineq);
2108 k = isl_basic_set_alloc_equality(graph->lp);
2109 if (k < 0)
2110 return -1;
2111 isl_seq_clr(graph->lp->eq[k], 1 + total);
2112 isl_int_set_si(graph->lp->eq[k][1], -1);
2113 for (i = 0; i < 2 * nparam; ++i)
2114 isl_int_set_si(graph->lp->eq[k][1 + param_pos + i], 1);
2116 if (parametric) {
2117 k = isl_basic_set_alloc_equality(graph->lp);
2118 if (k < 0)
2119 return -1;
2120 isl_seq_clr(graph->lp->eq[k], 1 + total);
2121 isl_int_set_si(graph->lp->eq[k][3], -1);
2122 for (i = 0; i < graph->n; ++i) {
2123 int pos = 1 + graph->node[i].start + 1;
2125 for (j = 0; j < 2 * graph->node[i].nparam; ++j)
2126 isl_int_set_si(graph->lp->eq[k][pos + j], 1);
2130 k = isl_basic_set_alloc_equality(graph->lp);
2131 if (k < 0)
2132 return -1;
2133 isl_seq_clr(graph->lp->eq[k], 1 + total);
2134 isl_int_set_si(graph->lp->eq[k][4], -1);
2135 for (i = 0; i < graph->n; ++i) {
2136 struct isl_sched_node *node = &graph->node[i];
2137 int pos = 1 + node->start + 1 + 2 * node->nparam;
2139 for (j = 0; j < 2 * node->nvar; ++j)
2140 isl_int_set_si(graph->lp->eq[k][pos + j], 1);
2143 if (max_constant_term != -1)
2144 for (i = 0; i < graph->n; ++i) {
2145 struct isl_sched_node *node = &graph->node[i];
2146 k = isl_basic_set_alloc_inequality(graph->lp);
2147 if (k < 0)
2148 return -1;
2149 isl_seq_clr(graph->lp->ineq[k], 1 + total);
2150 isl_int_set_si(graph->lp->ineq[k][1 + node->start], -1);
2151 isl_int_set_si(graph->lp->ineq[k][0], max_constant_term);
2154 if (add_bound_coefficient_constraints(ctx, graph) < 0)
2155 return -1;
2156 if (add_all_validity_constraints(graph, use_coincidence) < 0)
2157 return -1;
2158 if (add_all_proximity_constraints(graph, use_coincidence) < 0)
2159 return -1;
2161 return 0;
2164 /* Analyze the conflicting constraint found by
2165 * isl_tab_basic_set_non_trivial_lexmin. If it corresponds to the validity
2166 * constraint of one of the edges between distinct nodes, living, moreover
2167 * in distinct SCCs, then record the source and sink SCC as this may
2168 * be a good place to cut between SCCs.
2170 static int check_conflict(int con, void *user)
2172 int i;
2173 struct isl_sched_graph *graph = user;
2175 if (graph->src_scc >= 0)
2176 return 0;
2178 con -= graph->lp->n_eq;
2180 if (con >= graph->lp->n_ineq)
2181 return 0;
2183 for (i = 0; i < graph->n_edge; ++i) {
2184 if (!graph->edge[i].validity)
2185 continue;
2186 if (graph->edge[i].src == graph->edge[i].dst)
2187 continue;
2188 if (graph->edge[i].src->scc == graph->edge[i].dst->scc)
2189 continue;
2190 if (graph->edge[i].start > con)
2191 continue;
2192 if (graph->edge[i].end <= con)
2193 continue;
2194 graph->src_scc = graph->edge[i].src->scc;
2195 graph->dst_scc = graph->edge[i].dst->scc;
2198 return 0;
2201 /* Check whether the next schedule row of the given node needs to be
2202 * non-trivial. Lower-dimensional domains may have some trivial rows,
2203 * but as soon as the number of remaining required non-trivial rows
2204 * is as large as the number or remaining rows to be computed,
2205 * all remaining rows need to be non-trivial.
2207 static int needs_row(struct isl_sched_graph *graph, struct isl_sched_node *node)
2209 return node->nvar - node->rank >= graph->maxvar - graph->n_row;
2212 /* Solve the ILP problem constructed in setup_lp.
2213 * For each node such that all the remaining rows of its schedule
2214 * need to be non-trivial, we construct a non-triviality region.
2215 * This region imposes that the next row is independent of previous rows.
2216 * In particular the coefficients c_i_x are represented by t_i_x
2217 * variables with c_i_x = Q t_i_x and Q a unimodular matrix such that
2218 * its first columns span the rows of the previously computed part
2219 * of the schedule. The non-triviality region enforces that at least
2220 * one of the remaining components of t_i_x is non-zero, i.e.,
2221 * that the new schedule row depends on at least one of the remaining
2222 * columns of Q.
2224 static __isl_give isl_vec *solve_lp(struct isl_sched_graph *graph)
2226 int i;
2227 isl_vec *sol;
2228 isl_basic_set *lp;
2230 for (i = 0; i < graph->n; ++i) {
2231 struct isl_sched_node *node = &graph->node[i];
2232 int skip = node->rank;
2233 graph->region[i].pos = node->start + 1 + 2*(node->nparam+skip);
2234 if (needs_row(graph, node))
2235 graph->region[i].len = 2 * (node->nvar - skip);
2236 else
2237 graph->region[i].len = 0;
2239 lp = isl_basic_set_copy(graph->lp);
2240 sol = isl_tab_basic_set_non_trivial_lexmin(lp, 2, graph->n,
2241 graph->region, &check_conflict, graph);
2242 return sol;
2245 /* Update the schedules of all nodes based on the given solution
2246 * of the LP problem.
2247 * The new row is added to the current band.
2248 * All possibly negative coefficients are encoded as a difference
2249 * of two non-negative variables, so we need to perform the subtraction
2250 * here. Moreover, if use_cmap is set, then the solution does
2251 * not refer to the actual coefficients c_i_x, but instead to variables
2252 * t_i_x such that c_i_x = Q t_i_x and Q is equal to node->cmap.
2253 * In this case, we then also need to perform this multiplication
2254 * to obtain the values of c_i_x.
2256 * If coincident is set, then the caller guarantees that the new
2257 * row satisfies the coincidence constraints.
2259 static int update_schedule(struct isl_sched_graph *graph,
2260 __isl_take isl_vec *sol, int use_cmap, int coincident)
2262 int i, j;
2263 isl_vec *csol = NULL;
2265 if (!sol)
2266 goto error;
2267 if (sol->size == 0)
2268 isl_die(sol->ctx, isl_error_internal,
2269 "no solution found", goto error);
2270 if (graph->n_total_row >= graph->max_row)
2271 isl_die(sol->ctx, isl_error_internal,
2272 "too many schedule rows", goto error);
2274 for (i = 0; i < graph->n; ++i) {
2275 struct isl_sched_node *node = &graph->node[i];
2276 int pos = node->start;
2277 int row = isl_mat_rows(node->sched);
2279 isl_vec_free(csol);
2280 csol = isl_vec_alloc(sol->ctx, node->nvar);
2281 if (!csol)
2282 goto error;
2284 isl_map_free(node->sched_map);
2285 node->sched_map = NULL;
2286 node->sched = isl_mat_add_rows(node->sched, 1);
2287 if (!node->sched)
2288 goto error;
2289 node->sched = isl_mat_set_element(node->sched, row, 0,
2290 sol->el[1 + pos]);
2291 for (j = 0; j < node->nparam + node->nvar; ++j)
2292 isl_int_sub(sol->el[1 + pos + 1 + 2 * j + 1],
2293 sol->el[1 + pos + 1 + 2 * j + 1],
2294 sol->el[1 + pos + 1 + 2 * j]);
2295 for (j = 0; j < node->nparam; ++j)
2296 node->sched = isl_mat_set_element(node->sched,
2297 row, 1 + j, sol->el[1+pos+1+2*j+1]);
2298 for (j = 0; j < node->nvar; ++j)
2299 isl_int_set(csol->el[j],
2300 sol->el[1+pos+1+2*(node->nparam+j)+1]);
2301 if (use_cmap)
2302 csol = isl_mat_vec_product(isl_mat_copy(node->cmap),
2303 csol);
2304 if (!csol)
2305 goto error;
2306 for (j = 0; j < node->nvar; ++j)
2307 node->sched = isl_mat_set_element(node->sched,
2308 row, 1 + node->nparam + j, csol->el[j]);
2309 node->coincident[graph->n_total_row] = coincident;
2311 isl_vec_free(sol);
2312 isl_vec_free(csol);
2314 graph->n_row++;
2315 graph->n_total_row++;
2317 return 0;
2318 error:
2319 isl_vec_free(sol);
2320 isl_vec_free(csol);
2321 return -1;
2324 /* Convert row "row" of node->sched into an isl_aff living in "ls"
2325 * and return this isl_aff.
2327 static __isl_give isl_aff *extract_schedule_row(__isl_take isl_local_space *ls,
2328 struct isl_sched_node *node, int row)
2330 int j;
2331 isl_int v;
2332 isl_aff *aff;
2334 isl_int_init(v);
2336 aff = isl_aff_zero_on_domain(ls);
2337 isl_mat_get_element(node->sched, row, 0, &v);
2338 aff = isl_aff_set_constant(aff, v);
2339 for (j = 0; j < node->nparam; ++j) {
2340 isl_mat_get_element(node->sched, row, 1 + j, &v);
2341 aff = isl_aff_set_coefficient(aff, isl_dim_param, j, v);
2343 for (j = 0; j < node->nvar; ++j) {
2344 isl_mat_get_element(node->sched, row, 1 + node->nparam + j, &v);
2345 aff = isl_aff_set_coefficient(aff, isl_dim_in, j, v);
2348 isl_int_clear(v);
2350 return aff;
2353 /* Convert the "n" rows starting at "first" of node->sched into a multi_aff
2354 * and return this multi_aff.
2356 * The result is defined over the uncompressed node domain.
2358 static __isl_give isl_multi_aff *node_extract_partial_schedule_multi_aff(
2359 struct isl_sched_node *node, int first, int n)
2361 int i;
2362 isl_space *space;
2363 isl_local_space *ls;
2364 isl_aff *aff;
2365 isl_multi_aff *ma;
2366 int nrow, ncol;
2368 nrow = isl_mat_rows(node->sched);
2369 ncol = isl_mat_cols(node->sched) - 1;
2370 if (node->compressed)
2371 space = isl_multi_aff_get_domain_space(node->decompress);
2372 else
2373 space = isl_space_copy(node->space);
2374 ls = isl_local_space_from_space(isl_space_copy(space));
2375 space = isl_space_from_domain(space);
2376 space = isl_space_add_dims(space, isl_dim_out, n);
2377 ma = isl_multi_aff_zero(space);
2379 for (i = first; i < first + n; ++i) {
2380 aff = extract_schedule_row(isl_local_space_copy(ls), node, i);
2381 ma = isl_multi_aff_set_aff(ma, i - first, aff);
2384 isl_local_space_free(ls);
2386 if (node->compressed)
2387 ma = isl_multi_aff_pullback_multi_aff(ma,
2388 isl_multi_aff_copy(node->compress));
2390 return ma;
2393 /* Convert node->sched into a multi_aff and return this multi_aff.
2395 * The result is defined over the uncompressed node domain.
2397 static __isl_give isl_multi_aff *node_extract_schedule_multi_aff(
2398 struct isl_sched_node *node)
2400 int nrow;
2402 nrow = isl_mat_rows(node->sched);
2403 return node_extract_partial_schedule_multi_aff(node, 0, nrow);
2406 /* Convert node->sched into a map and return this map.
2408 * The result is cached in node->sched_map, which needs to be released
2409 * whenever node->sched is updated.
2410 * It is defined over the uncompressed node domain.
2412 static __isl_give isl_map *node_extract_schedule(struct isl_sched_node *node)
2414 if (!node->sched_map) {
2415 isl_multi_aff *ma;
2417 ma = node_extract_schedule_multi_aff(node);
2418 node->sched_map = isl_map_from_multi_aff(ma);
2421 return isl_map_copy(node->sched_map);
2424 /* Construct a map that can be used to update a dependence relation
2425 * based on the current schedule.
2426 * That is, construct a map expressing that source and sink
2427 * are executed within the same iteration of the current schedule.
2428 * This map can then be intersected with the dependence relation.
2429 * This is not the most efficient way, but this shouldn't be a critical
2430 * operation.
2432 static __isl_give isl_map *specializer(struct isl_sched_node *src,
2433 struct isl_sched_node *dst)
2435 isl_map *src_sched, *dst_sched;
2437 src_sched = node_extract_schedule(src);
2438 dst_sched = node_extract_schedule(dst);
2439 return isl_map_apply_range(src_sched, isl_map_reverse(dst_sched));
2442 /* Intersect the domains of the nested relations in domain and range
2443 * of "umap" with "map".
2445 static __isl_give isl_union_map *intersect_domains(
2446 __isl_take isl_union_map *umap, __isl_keep isl_map *map)
2448 isl_union_set *uset;
2450 umap = isl_union_map_zip(umap);
2451 uset = isl_union_set_from_set(isl_map_wrap(isl_map_copy(map)));
2452 umap = isl_union_map_intersect_domain(umap, uset);
2453 umap = isl_union_map_zip(umap);
2454 return umap;
2457 /* Update the dependence relation of the given edge based
2458 * on the current schedule.
2459 * If the dependence is carried completely by the current schedule, then
2460 * it is removed from the edge_tables. It is kept in the list of edges
2461 * as otherwise all edge_tables would have to be recomputed.
2463 static int update_edge(struct isl_sched_graph *graph,
2464 struct isl_sched_edge *edge)
2466 isl_map *id;
2468 id = specializer(edge->src, edge->dst);
2469 edge->map = isl_map_intersect(edge->map, isl_map_copy(id));
2470 if (!edge->map)
2471 goto error;
2473 if (edge->tagged_condition) {
2474 edge->tagged_condition =
2475 intersect_domains(edge->tagged_condition, id);
2476 if (!edge->tagged_condition)
2477 goto error;
2479 if (edge->tagged_validity) {
2480 edge->tagged_validity =
2481 intersect_domains(edge->tagged_validity, id);
2482 if (!edge->tagged_validity)
2483 goto error;
2486 isl_map_free(id);
2487 if (isl_map_plain_is_empty(edge->map))
2488 graph_remove_edge(graph, edge);
2490 return 0;
2491 error:
2492 isl_map_free(id);
2493 return -1;
2496 /* Update the dependence relations of all edges based on the current schedule.
2498 static int update_edges(isl_ctx *ctx, struct isl_sched_graph *graph)
2500 int i;
2502 for (i = graph->n_edge - 1; i >= 0; --i) {
2503 if (update_edge(graph, &graph->edge[i]) < 0)
2504 return -1;
2507 return 0;
2510 static void next_band(struct isl_sched_graph *graph)
2512 graph->band_start = graph->n_total_row;
2515 /* Return the union of the universe domains of the nodes in "graph"
2516 * that satisfy "pred".
2518 static __isl_give isl_union_set *isl_sched_graph_domain(isl_ctx *ctx,
2519 struct isl_sched_graph *graph,
2520 int (*pred)(struct isl_sched_node *node, int data), int data)
2522 int i;
2523 isl_set *set;
2524 isl_union_set *dom;
2526 for (i = 0; i < graph->n; ++i)
2527 if (pred(&graph->node[i], data))
2528 break;
2530 if (i >= graph->n)
2531 isl_die(ctx, isl_error_internal,
2532 "empty component", return NULL);
2534 set = isl_set_universe(isl_space_copy(graph->node[i].space));
2535 dom = isl_union_set_from_set(set);
2537 for (i = i + 1; i < graph->n; ++i) {
2538 if (!pred(&graph->node[i], data))
2539 continue;
2540 set = isl_set_universe(isl_space_copy(graph->node[i].space));
2541 dom = isl_union_set_union(dom, isl_union_set_from_set(set));
2544 return dom;
2547 /* Return a list of unions of universe domains, where each element
2548 * in the list corresponds to an SCC (or WCC) indexed by node->scc.
2550 static __isl_give isl_union_set_list *extract_sccs(isl_ctx *ctx,
2551 struct isl_sched_graph *graph)
2553 int i;
2554 isl_union_set_list *filters;
2556 filters = isl_union_set_list_alloc(ctx, graph->scc);
2557 for (i = 0; i < graph->scc; ++i) {
2558 isl_union_set *dom;
2560 dom = isl_sched_graph_domain(ctx, graph, &node_scc_exactly, i);
2561 filters = isl_union_set_list_add(filters, dom);
2564 return filters;
2567 /* Return a list of two unions of universe domains, one for the SCCs up
2568 * to and including graph->src_scc and another for the other SCCS.
2570 static __isl_give isl_union_set_list *extract_split(isl_ctx *ctx,
2571 struct isl_sched_graph *graph)
2573 isl_union_set *dom;
2574 isl_union_set_list *filters;
2576 filters = isl_union_set_list_alloc(ctx, 2);
2577 dom = isl_sched_graph_domain(ctx, graph,
2578 &node_scc_at_most, graph->src_scc);
2579 filters = isl_union_set_list_add(filters, dom);
2580 dom = isl_sched_graph_domain(ctx, graph,
2581 &node_scc_at_least, graph->src_scc + 1);
2582 filters = isl_union_set_list_add(filters, dom);
2584 return filters;
2587 /* Topologically sort statements mapped to the same schedule iteration
2588 * and add insert a sequence node in front of "node"
2589 * corresponding to this order.
2591 static __isl_give isl_schedule_node *sort_statements(
2592 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph)
2594 isl_ctx *ctx;
2595 isl_union_set_list *filters;
2597 if (!node)
2598 return NULL;
2600 ctx = isl_schedule_node_get_ctx(node);
2601 if (graph->n < 1)
2602 isl_die(ctx, isl_error_internal,
2603 "graph should have at least one node",
2604 return isl_schedule_node_free(node));
2606 if (graph->n == 1)
2607 return node;
2609 if (update_edges(ctx, graph) < 0)
2610 return isl_schedule_node_free(node);
2612 if (graph->n_edge == 0)
2613 return node;
2615 if (detect_sccs(ctx, graph) < 0)
2616 return isl_schedule_node_free(node);
2618 filters = extract_sccs(ctx, graph);
2619 node = isl_schedule_node_insert_sequence(node, filters);
2621 return node;
2624 /* Copy nodes that satisfy node_pred from the src dependence graph
2625 * to the dst dependence graph.
2627 static int copy_nodes(struct isl_sched_graph *dst, struct isl_sched_graph *src,
2628 int (*node_pred)(struct isl_sched_node *node, int data), int data)
2630 int i;
2632 dst->n = 0;
2633 for (i = 0; i < src->n; ++i) {
2634 int j;
2636 if (!node_pred(&src->node[i], data))
2637 continue;
2639 j = dst->n;
2640 dst->node[j].space = isl_space_copy(src->node[i].space);
2641 dst->node[j].compressed = src->node[i].compressed;
2642 dst->node[j].hull = isl_set_copy(src->node[i].hull);
2643 dst->node[j].compress =
2644 isl_multi_aff_copy(src->node[i].compress);
2645 dst->node[j].decompress =
2646 isl_multi_aff_copy(src->node[i].decompress);
2647 dst->node[j].nvar = src->node[i].nvar;
2648 dst->node[j].nparam = src->node[i].nparam;
2649 dst->node[j].sched = isl_mat_copy(src->node[i].sched);
2650 dst->node[j].sched_map = isl_map_copy(src->node[i].sched_map);
2651 dst->node[j].coincident = src->node[i].coincident;
2652 dst->n++;
2654 if (!dst->node[j].space || !dst->node[j].sched)
2655 return -1;
2656 if (dst->node[j].compressed &&
2657 (!dst->node[j].hull || !dst->node[j].compress ||
2658 !dst->node[j].decompress))
2659 return -1;
2662 return 0;
2665 /* Copy non-empty edges that satisfy edge_pred from the src dependence graph
2666 * to the dst dependence graph.
2667 * If the source or destination node of the edge is not in the destination
2668 * graph, then it must be a backward proximity edge and it should simply
2669 * be ignored.
2671 static int copy_edges(isl_ctx *ctx, struct isl_sched_graph *dst,
2672 struct isl_sched_graph *src,
2673 int (*edge_pred)(struct isl_sched_edge *edge, int data), int data)
2675 int i;
2676 enum isl_edge_type t;
2678 dst->n_edge = 0;
2679 for (i = 0; i < src->n_edge; ++i) {
2680 struct isl_sched_edge *edge = &src->edge[i];
2681 isl_map *map;
2682 isl_union_map *tagged_condition;
2683 isl_union_map *tagged_validity;
2684 struct isl_sched_node *dst_src, *dst_dst;
2686 if (!edge_pred(edge, data))
2687 continue;
2689 if (isl_map_plain_is_empty(edge->map))
2690 continue;
2692 dst_src = graph_find_node(ctx, dst, edge->src->space);
2693 dst_dst = graph_find_node(ctx, dst, edge->dst->space);
2694 if (!dst_src || !dst_dst) {
2695 if (edge->validity || edge->conditional_validity)
2696 isl_die(ctx, isl_error_internal,
2697 "backward (conditional) validity edge",
2698 return -1);
2699 continue;
2702 map = isl_map_copy(edge->map);
2703 tagged_condition = isl_union_map_copy(edge->tagged_condition);
2704 tagged_validity = isl_union_map_copy(edge->tagged_validity);
2706 dst->edge[dst->n_edge].src = dst_src;
2707 dst->edge[dst->n_edge].dst = dst_dst;
2708 dst->edge[dst->n_edge].map = map;
2709 dst->edge[dst->n_edge].tagged_condition = tagged_condition;
2710 dst->edge[dst->n_edge].tagged_validity = tagged_validity;
2711 dst->edge[dst->n_edge].validity = edge->validity;
2712 dst->edge[dst->n_edge].proximity = edge->proximity;
2713 dst->edge[dst->n_edge].coincidence = edge->coincidence;
2714 dst->edge[dst->n_edge].condition = edge->condition;
2715 dst->edge[dst->n_edge].conditional_validity =
2716 edge->conditional_validity;
2717 dst->n_edge++;
2719 if (edge->tagged_condition && !tagged_condition)
2720 return -1;
2721 if (edge->tagged_validity && !tagged_validity)
2722 return -1;
2724 for (t = isl_edge_first; t <= isl_edge_last; ++t) {
2725 if (edge !=
2726 graph_find_edge(src, t, edge->src, edge->dst))
2727 continue;
2728 if (graph_edge_table_add(ctx, dst, t,
2729 &dst->edge[dst->n_edge - 1]) < 0)
2730 return -1;
2734 return 0;
2737 /* Compute the maximal number of variables over all nodes.
2738 * This is the maximal number of linearly independent schedule
2739 * rows that we need to compute.
2740 * Just in case we end up in a part of the dependence graph
2741 * with only lower-dimensional domains, we make sure we will
2742 * compute the required amount of extra linearly independent rows.
2744 static int compute_maxvar(struct isl_sched_graph *graph)
2746 int i;
2748 graph->maxvar = 0;
2749 for (i = 0; i < graph->n; ++i) {
2750 struct isl_sched_node *node = &graph->node[i];
2751 int nvar;
2753 if (node_update_cmap(node) < 0)
2754 return -1;
2755 nvar = node->nvar + graph->n_row - node->rank;
2756 if (nvar > graph->maxvar)
2757 graph->maxvar = nvar;
2760 return 0;
2763 static __isl_give isl_schedule_node *compute_schedule(isl_schedule_node *node,
2764 struct isl_sched_graph *graph);
2765 static __isl_give isl_schedule_node *compute_schedule_wcc(
2766 isl_schedule_node *node, struct isl_sched_graph *graph);
2768 /* Compute a schedule for a subgraph of "graph". In particular, for
2769 * the graph composed of nodes that satisfy node_pred and edges that
2770 * that satisfy edge_pred. The caller should precompute the number
2771 * of nodes and edges that satisfy these predicates and pass them along
2772 * as "n" and "n_edge".
2773 * If the subgraph is known to consist of a single component, then wcc should
2774 * be set and then we call compute_schedule_wcc on the constructed subgraph.
2775 * Otherwise, we call compute_schedule, which will check whether the subgraph
2776 * is connected.
2778 * The schedule is inserted at "node" and the updated schedule node
2779 * is returned.
2781 static __isl_give isl_schedule_node *compute_sub_schedule(
2782 __isl_take isl_schedule_node *node, isl_ctx *ctx,
2783 struct isl_sched_graph *graph, int n, int n_edge,
2784 int (*node_pred)(struct isl_sched_node *node, int data),
2785 int (*edge_pred)(struct isl_sched_edge *edge, int data),
2786 int data, int wcc)
2788 struct isl_sched_graph split = { 0 };
2789 int t;
2791 if (graph_alloc(ctx, &split, n, n_edge) < 0)
2792 goto error;
2793 if (copy_nodes(&split, graph, node_pred, data) < 0)
2794 goto error;
2795 if (graph_init_table(ctx, &split) < 0)
2796 goto error;
2797 for (t = 0; t <= isl_edge_last; ++t)
2798 split.max_edge[t] = graph->max_edge[t];
2799 if (graph_init_edge_tables(ctx, &split) < 0)
2800 goto error;
2801 if (copy_edges(ctx, &split, graph, edge_pred, data) < 0)
2802 goto error;
2803 split.n_row = graph->n_row;
2804 split.max_row = graph->max_row;
2805 split.n_total_row = graph->n_total_row;
2806 split.band_start = graph->band_start;
2808 if (wcc)
2809 node = compute_schedule_wcc(node, &split);
2810 else
2811 node = compute_schedule(node, &split);
2813 graph_free(ctx, &split);
2814 return node;
2815 error:
2816 graph_free(ctx, &split);
2817 return isl_schedule_node_free(node);
2820 static int edge_scc_exactly(struct isl_sched_edge *edge, int scc)
2822 return edge->src->scc == scc && edge->dst->scc == scc;
2825 static int edge_dst_scc_at_most(struct isl_sched_edge *edge, int scc)
2827 return edge->dst->scc <= scc;
2830 static int edge_src_scc_at_least(struct isl_sched_edge *edge, int scc)
2832 return edge->src->scc >= scc;
2835 /* Reset the current band by dropping all its schedule rows.
2837 static int reset_band(struct isl_sched_graph *graph)
2839 int i;
2840 int drop;
2842 drop = graph->n_total_row - graph->band_start;
2843 graph->n_total_row -= drop;
2844 graph->n_row -= drop;
2846 for (i = 0; i < graph->n; ++i) {
2847 struct isl_sched_node *node = &graph->node[i];
2849 isl_map_free(node->sched_map);
2850 node->sched_map = NULL;
2852 node->sched = isl_mat_drop_rows(node->sched,
2853 graph->band_start, drop);
2855 if (!node->sched)
2856 return -1;
2859 return 0;
2862 /* Split the current graph into two parts and compute a schedule for each
2863 * part individually. In particular, one part consists of all SCCs up
2864 * to and including graph->src_scc, while the other part contains the other
2865 * SCCS. The split is enforced by a sequence node inserted at position "node"
2866 * in the schedule tree. Return the updated schedule node.
2868 * The current band is reset. It would be possible to reuse
2869 * the previously computed rows as the first rows in the next
2870 * band, but recomputing them may result in better rows as we are looking
2871 * at a smaller part of the dependence graph.
2873 static __isl_give isl_schedule_node *compute_split_schedule(
2874 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph)
2876 int i, n, e1, e2;
2877 int orig_total_row;
2878 isl_ctx *ctx;
2879 isl_union_set_list *filters;
2881 if (!node)
2882 return NULL;
2884 if (reset_band(graph) < 0)
2885 return isl_schedule_node_free(node);
2887 n = 0;
2888 for (i = 0; i < graph->n; ++i) {
2889 struct isl_sched_node *node = &graph->node[i];
2890 int before = node->scc <= graph->src_scc;
2892 if (before)
2893 n++;
2896 e1 = e2 = 0;
2897 for (i = 0; i < graph->n_edge; ++i) {
2898 if (graph->edge[i].dst->scc <= graph->src_scc)
2899 e1++;
2900 if (graph->edge[i].src->scc > graph->src_scc)
2901 e2++;
2904 next_band(graph);
2906 ctx = isl_schedule_node_get_ctx(node);
2907 filters = extract_split(ctx, graph);
2908 node = isl_schedule_node_insert_sequence(node, filters);
2909 node = isl_schedule_node_child(node, 0);
2910 node = isl_schedule_node_child(node, 0);
2912 orig_total_row = graph->n_total_row;
2913 node = compute_sub_schedule(node, ctx, graph, n, e1,
2914 &node_scc_at_most, &edge_dst_scc_at_most,
2915 graph->src_scc, 0);
2916 node = isl_schedule_node_parent(node);
2917 node = isl_schedule_node_next_sibling(node);
2918 node = isl_schedule_node_child(node, 0);
2919 graph->n_total_row = orig_total_row;
2920 node = compute_sub_schedule(node, ctx, graph, graph->n - n, e2,
2921 &node_scc_at_least, &edge_src_scc_at_least,
2922 graph->src_scc + 1, 0);
2923 node = isl_schedule_node_parent(node);
2924 node = isl_schedule_node_parent(node);
2926 return node;
2929 /* Insert a band node at position "node" in the schedule tree corresponding
2930 * to the current band in "graph". Mark the band node permutable
2931 * if "permutable" is set.
2932 * The partial schedules and the coincidence property are extracted
2933 * from the graph nodes.
2934 * Return the updated schedule node.
2936 static __isl_give isl_schedule_node *insert_current_band(
2937 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph,
2938 int permutable)
2940 int i;
2941 int start, end, n;
2942 isl_multi_aff *ma;
2943 isl_multi_pw_aff *mpa;
2944 isl_multi_union_pw_aff *mupa;
2946 if (!node)
2947 return NULL;
2949 if (graph->n < 1)
2950 isl_die(isl_schedule_node_get_ctx(node), isl_error_internal,
2951 "graph should have at least one node",
2952 return isl_schedule_node_free(node));
2954 start = graph->band_start;
2955 end = graph->n_total_row;
2956 n = end - start;
2958 ma = node_extract_partial_schedule_multi_aff(&graph->node[0], start, n);
2959 mpa = isl_multi_pw_aff_from_multi_aff(ma);
2960 mupa = isl_multi_union_pw_aff_from_multi_pw_aff(mpa);
2962 for (i = 1; i < graph->n; ++i) {
2963 isl_multi_union_pw_aff *mupa_i;
2965 ma = node_extract_partial_schedule_multi_aff(&graph->node[i],
2966 start, n);
2967 mpa = isl_multi_pw_aff_from_multi_aff(ma);
2968 mupa_i = isl_multi_union_pw_aff_from_multi_pw_aff(mpa);
2969 mupa = isl_multi_union_pw_aff_union_add(mupa, mupa_i);
2971 node = isl_schedule_node_insert_partial_schedule(node, mupa);
2973 for (i = 0; i < n; ++i)
2974 node = isl_schedule_node_band_member_set_coincident(node, i,
2975 graph->node[0].coincident[start + i]);
2976 node = isl_schedule_node_band_set_permutable(node, permutable);
2978 return node;
2981 /* Update the dependence relations based on the current schedule,
2982 * add the current band to "node" and the continue with the computation
2983 * of the next band.
2984 * Return the updated schedule node.
2986 static __isl_give isl_schedule_node *compute_next_band(
2987 __isl_take isl_schedule_node *node,
2988 struct isl_sched_graph *graph, int permutable)
2990 isl_ctx *ctx;
2992 if (!node)
2993 return NULL;
2995 ctx = isl_schedule_node_get_ctx(node);
2996 if (update_edges(ctx, graph) < 0)
2997 return isl_schedule_node_free(node);
2998 node = insert_current_band(node, graph, permutable);
2999 next_band(graph);
3001 node = isl_schedule_node_child(node, 0);
3002 node = compute_schedule(node, graph);
3003 node = isl_schedule_node_parent(node);
3005 return node;
3008 /* Add constraints to graph->lp that force the dependence "map" (which
3009 * is part of the dependence relation of "edge")
3010 * to be respected and attempt to carry it, where the edge is one from
3011 * a node j to itself. "pos" is the sequence number of the given map.
3012 * That is, add constraints that enforce
3014 * (c_j_0 + c_j_n n + c_j_x y) - (c_j_0 + c_j_n n + c_j_x x)
3015 * = c_j_x (y - x) >= e_i
3017 * for each (x,y) in R.
3018 * We obtain general constraints on coefficients (c_0, c_n, c_x)
3019 * of valid constraints for (y - x) and then plug in (-e_i, 0, c_j_x),
3020 * with each coefficient in c_j_x represented as a pair of non-negative
3021 * coefficients.
3023 static int add_intra_constraints(struct isl_sched_graph *graph,
3024 struct isl_sched_edge *edge, __isl_take isl_map *map, int pos)
3026 unsigned total;
3027 isl_ctx *ctx = isl_map_get_ctx(map);
3028 isl_space *dim;
3029 isl_dim_map *dim_map;
3030 isl_basic_set *coef;
3031 struct isl_sched_node *node = edge->src;
3033 coef = intra_coefficients(graph, node, map);
3034 if (!coef)
3035 return -1;
3037 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
3039 total = isl_basic_set_total_dim(graph->lp);
3040 dim_map = isl_dim_map_alloc(ctx, total);
3041 isl_dim_map_range(dim_map, 3 + pos, 0, 0, 0, 1, -1);
3042 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 1, 2,
3043 isl_space_dim(dim, isl_dim_set), 1,
3044 node->nvar, -1);
3045 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 2, 2,
3046 isl_space_dim(dim, isl_dim_set), 1,
3047 node->nvar, 1);
3048 graph->lp = isl_basic_set_extend_constraints(graph->lp,
3049 coef->n_eq, coef->n_ineq);
3050 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
3051 coef, dim_map);
3052 isl_space_free(dim);
3054 return 0;
3057 /* Add constraints to graph->lp that force the dependence "map" (which
3058 * is part of the dependence relation of "edge")
3059 * to be respected and attempt to carry it, where the edge is one from
3060 * node j to node k. "pos" is the sequence number of the given map.
3061 * That is, add constraints that enforce
3063 * (c_k_0 + c_k_n n + c_k_x y) - (c_j_0 + c_j_n n + c_j_x x) >= e_i
3065 * for each (x,y) in R.
3066 * We obtain general constraints on coefficients (c_0, c_n, c_x)
3067 * of valid constraints for R and then plug in
3068 * (-e_i + c_k_0 - c_j_0, c_k_n - c_j_n, c_k_x - c_j_x)
3069 * with each coefficient (except e_i, c_k_0 and c_j_0)
3070 * represented as a pair of non-negative coefficients.
3072 static int add_inter_constraints(struct isl_sched_graph *graph,
3073 struct isl_sched_edge *edge, __isl_take isl_map *map, int pos)
3075 unsigned total;
3076 isl_ctx *ctx = isl_map_get_ctx(map);
3077 isl_space *dim;
3078 isl_dim_map *dim_map;
3079 isl_basic_set *coef;
3080 struct isl_sched_node *src = edge->src;
3081 struct isl_sched_node *dst = edge->dst;
3083 coef = inter_coefficients(graph, edge, map);
3084 if (!coef)
3085 return -1;
3087 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
3089 total = isl_basic_set_total_dim(graph->lp);
3090 dim_map = isl_dim_map_alloc(ctx, total);
3092 isl_dim_map_range(dim_map, 3 + pos, 0, 0, 0, 1, -1);
3094 isl_dim_map_range(dim_map, dst->start, 0, 0, 0, 1, 1);
3095 isl_dim_map_range(dim_map, dst->start + 1, 2, 1, 1, dst->nparam, -1);
3096 isl_dim_map_range(dim_map, dst->start + 2, 2, 1, 1, dst->nparam, 1);
3097 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 1, 2,
3098 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
3099 dst->nvar, -1);
3100 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 2, 2,
3101 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
3102 dst->nvar, 1);
3104 isl_dim_map_range(dim_map, src->start, 0, 0, 0, 1, -1);
3105 isl_dim_map_range(dim_map, src->start + 1, 2, 1, 1, src->nparam, 1);
3106 isl_dim_map_range(dim_map, src->start + 2, 2, 1, 1, src->nparam, -1);
3107 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 1, 2,
3108 isl_space_dim(dim, isl_dim_set), 1,
3109 src->nvar, 1);
3110 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 2, 2,
3111 isl_space_dim(dim, isl_dim_set), 1,
3112 src->nvar, -1);
3114 graph->lp = isl_basic_set_extend_constraints(graph->lp,
3115 coef->n_eq, coef->n_ineq);
3116 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
3117 coef, dim_map);
3118 isl_space_free(dim);
3120 return 0;
3123 /* Add constraints to graph->lp that force all (conditional) validity
3124 * dependences to be respected and attempt to carry them.
3126 static int add_all_constraints(struct isl_sched_graph *graph)
3128 int i, j;
3129 int pos;
3131 pos = 0;
3132 for (i = 0; i < graph->n_edge; ++i) {
3133 struct isl_sched_edge *edge= &graph->edge[i];
3135 if (!edge->validity && !edge->conditional_validity)
3136 continue;
3138 for (j = 0; j < edge->map->n; ++j) {
3139 isl_basic_map *bmap;
3140 isl_map *map;
3142 bmap = isl_basic_map_copy(edge->map->p[j]);
3143 map = isl_map_from_basic_map(bmap);
3145 if (edge->src == edge->dst &&
3146 add_intra_constraints(graph, edge, map, pos) < 0)
3147 return -1;
3148 if (edge->src != edge->dst &&
3149 add_inter_constraints(graph, edge, map, pos) < 0)
3150 return -1;
3151 ++pos;
3155 return 0;
3158 /* Count the number of equality and inequality constraints
3159 * that will be added to the carry_lp problem.
3160 * We count each edge exactly once.
3162 static int count_all_constraints(struct isl_sched_graph *graph,
3163 int *n_eq, int *n_ineq)
3165 int i, j;
3167 *n_eq = *n_ineq = 0;
3168 for (i = 0; i < graph->n_edge; ++i) {
3169 struct isl_sched_edge *edge= &graph->edge[i];
3170 for (j = 0; j < edge->map->n; ++j) {
3171 isl_basic_map *bmap;
3172 isl_map *map;
3174 bmap = isl_basic_map_copy(edge->map->p[j]);
3175 map = isl_map_from_basic_map(bmap);
3177 if (count_map_constraints(graph, edge, map,
3178 n_eq, n_ineq, 1, 0) < 0)
3179 return -1;
3183 return 0;
3186 /* Construct an LP problem for finding schedule coefficients
3187 * such that the schedule carries as many dependences as possible.
3188 * In particular, for each dependence i, we bound the dependence distance
3189 * from below by e_i, with 0 <= e_i <= 1 and then maximize the sum
3190 * of all e_i's. Dependence with e_i = 0 in the solution are simply
3191 * respected, while those with e_i > 0 (in practice e_i = 1) are carried.
3192 * Note that if the dependence relation is a union of basic maps,
3193 * then we have to consider each basic map individually as it may only
3194 * be possible to carry the dependences expressed by some of those
3195 * basic maps and not all off them.
3196 * Below, we consider each of those basic maps as a separate "edge".
3198 * All variables of the LP are non-negative. The actual coefficients
3199 * may be negative, so each coefficient is represented as the difference
3200 * of two non-negative variables. The negative part always appears
3201 * immediately before the positive part.
3202 * Other than that, the variables have the following order
3204 * - sum of (1 - e_i) over all edges
3205 * - sum of positive and negative parts of all c_n coefficients
3206 * (unconstrained when computing non-parametric schedules)
3207 * - sum of positive and negative parts of all c_x coefficients
3208 * - for each edge
3209 * - e_i
3210 * - for each node
3211 * - c_i_0
3212 * - positive and negative parts of c_i_n (if parametric)
3213 * - positive and negative parts of c_i_x
3215 * The constraints are those from the (validity) edges plus three equalities
3216 * to express the sums and n_edge inequalities to express e_i <= 1.
3218 static int setup_carry_lp(isl_ctx *ctx, struct isl_sched_graph *graph)
3220 int i, j;
3221 int k;
3222 isl_space *dim;
3223 unsigned total;
3224 int n_eq, n_ineq;
3225 int n_edge;
3227 n_edge = 0;
3228 for (i = 0; i < graph->n_edge; ++i)
3229 n_edge += graph->edge[i].map->n;
3231 total = 3 + n_edge;
3232 for (i = 0; i < graph->n; ++i) {
3233 struct isl_sched_node *node = &graph->node[graph->sorted[i]];
3234 node->start = total;
3235 total += 1 + 2 * (node->nparam + node->nvar);
3238 if (count_all_constraints(graph, &n_eq, &n_ineq) < 0)
3239 return -1;
3240 if (count_bound_coefficient_constraints(ctx, graph, &n_eq, &n_ineq) < 0)
3241 return -1;
3243 dim = isl_space_set_alloc(ctx, 0, total);
3244 isl_basic_set_free(graph->lp);
3245 n_eq += 3;
3246 n_ineq += n_edge;
3247 graph->lp = isl_basic_set_alloc_space(dim, 0, n_eq, n_ineq);
3248 graph->lp = isl_basic_set_set_rational(graph->lp);
3250 k = isl_basic_set_alloc_equality(graph->lp);
3251 if (k < 0)
3252 return -1;
3253 isl_seq_clr(graph->lp->eq[k], 1 + total);
3254 isl_int_set_si(graph->lp->eq[k][0], -n_edge);
3255 isl_int_set_si(graph->lp->eq[k][1], 1);
3256 for (i = 0; i < n_edge; ++i)
3257 isl_int_set_si(graph->lp->eq[k][4 + i], 1);
3259 k = isl_basic_set_alloc_equality(graph->lp);
3260 if (k < 0)
3261 return -1;
3262 isl_seq_clr(graph->lp->eq[k], 1 + total);
3263 isl_int_set_si(graph->lp->eq[k][2], -1);
3264 for (i = 0; i < graph->n; ++i) {
3265 int pos = 1 + graph->node[i].start + 1;
3267 for (j = 0; j < 2 * graph->node[i].nparam; ++j)
3268 isl_int_set_si(graph->lp->eq[k][pos + j], 1);
3271 k = isl_basic_set_alloc_equality(graph->lp);
3272 if (k < 0)
3273 return -1;
3274 isl_seq_clr(graph->lp->eq[k], 1 + total);
3275 isl_int_set_si(graph->lp->eq[k][3], -1);
3276 for (i = 0; i < graph->n; ++i) {
3277 struct isl_sched_node *node = &graph->node[i];
3278 int pos = 1 + node->start + 1 + 2 * node->nparam;
3280 for (j = 0; j < 2 * node->nvar; ++j)
3281 isl_int_set_si(graph->lp->eq[k][pos + j], 1);
3284 for (i = 0; i < n_edge; ++i) {
3285 k = isl_basic_set_alloc_inequality(graph->lp);
3286 if (k < 0)
3287 return -1;
3288 isl_seq_clr(graph->lp->ineq[k], 1 + total);
3289 isl_int_set_si(graph->lp->ineq[k][4 + i], -1);
3290 isl_int_set_si(graph->lp->ineq[k][0], 1);
3293 if (add_bound_coefficient_constraints(ctx, graph) < 0)
3294 return -1;
3295 if (add_all_constraints(graph) < 0)
3296 return -1;
3298 return 0;
3301 static __isl_give isl_schedule_node *compute_component_schedule(
3302 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph,
3303 int wcc);
3305 /* Comparison function for sorting the statements based on
3306 * the corresponding value in "r".
3308 static int smaller_value(const void *a, const void *b, void *data)
3310 isl_vec *r = data;
3311 const int *i1 = a;
3312 const int *i2 = b;
3314 return isl_int_cmp(r->el[*i1], r->el[*i2]);
3317 /* If the schedule_split_scaled option is set and if the linear
3318 * parts of the scheduling rows for all nodes in the graphs have
3319 * a non-trivial common divisor, then split off the remainder of the
3320 * constant term modulo this common divisor from the linear part.
3321 * Otherwise, insert a band node directly and continue with
3322 * the construction of the schedule.
3324 * If a non-trivial common divisor is found, then
3325 * the linear part is reduced and the remainder is enforced
3326 * by a sequence node with the children placed in the order
3327 * of this remainder.
3328 * In particular, we assign an scc index based on the remainder and
3329 * then rely on compute_component_schedule to insert the sequence and
3330 * to continue the schedule construction on each part.
3332 static __isl_give isl_schedule_node *split_scaled(
3333 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph)
3335 int i;
3336 int row;
3337 int scc;
3338 isl_ctx *ctx;
3339 isl_int gcd, gcd_i;
3340 isl_vec *r;
3341 int *order;
3343 if (!node)
3344 return NULL;
3346 ctx = isl_schedule_node_get_ctx(node);
3347 if (!ctx->opt->schedule_split_scaled)
3348 return compute_next_band(node, graph, 0);
3349 if (graph->n <= 1)
3350 return compute_next_band(node, graph, 0);
3352 isl_int_init(gcd);
3353 isl_int_init(gcd_i);
3355 isl_int_set_si(gcd, 0);
3357 row = isl_mat_rows(graph->node[0].sched) - 1;
3359 for (i = 0; i < graph->n; ++i) {
3360 struct isl_sched_node *node = &graph->node[i];
3361 int cols = isl_mat_cols(node->sched);
3363 isl_seq_gcd(node->sched->row[row] + 1, cols - 1, &gcd_i);
3364 isl_int_gcd(gcd, gcd, gcd_i);
3367 isl_int_clear(gcd_i);
3369 if (isl_int_cmp_si(gcd, 1) <= 0) {
3370 isl_int_clear(gcd);
3371 return compute_next_band(node, graph, 0);
3374 r = isl_vec_alloc(ctx, graph->n);
3375 order = isl_calloc_array(ctx, int, graph->n);
3376 if (!r || !order)
3377 goto error;
3379 for (i = 0; i < graph->n; ++i) {
3380 struct isl_sched_node *node = &graph->node[i];
3382 order[i] = i;
3383 isl_int_fdiv_r(r->el[i], node->sched->row[row][0], gcd);
3384 isl_int_fdiv_q(node->sched->row[row][0],
3385 node->sched->row[row][0], gcd);
3386 isl_int_mul(node->sched->row[row][0],
3387 node->sched->row[row][0], gcd);
3388 node->sched = isl_mat_scale_down_row(node->sched, row, gcd);
3389 if (!node->sched)
3390 goto error;
3393 if (isl_sort(order, graph->n, sizeof(order[0]), &smaller_value, r) < 0)
3394 goto error;
3396 scc = 0;
3397 for (i = 0; i < graph->n; ++i) {
3398 if (i > 0 && isl_int_ne(r->el[order[i - 1]], r->el[order[i]]))
3399 ++scc;
3400 graph->node[order[i]].scc = scc;
3402 graph->scc = ++scc;
3403 graph->weak = 0;
3405 isl_int_clear(gcd);
3406 isl_vec_free(r);
3407 free(order);
3409 if (update_edges(ctx, graph) < 0)
3410 return isl_schedule_node_free(node);
3411 node = insert_current_band(node, graph, 0);
3412 next_band(graph);
3414 node = isl_schedule_node_child(node, 0);
3415 node = compute_component_schedule(node, graph, 0);
3416 node = isl_schedule_node_parent(node);
3418 return node;
3419 error:
3420 isl_vec_free(r);
3421 free(order);
3422 isl_int_clear(gcd);
3423 return isl_schedule_node_free(node);
3426 /* Is the schedule row "sol" trivial on node "node"?
3427 * That is, is the solution zero on the dimensions orthogonal to
3428 * the previously found solutions?
3429 * Return 1 if the solution is trivial, 0 if it is not and -1 on error.
3431 * Each coefficient is represented as the difference between
3432 * two non-negative values in "sol". "sol" has been computed
3433 * in terms of the original iterators (i.e., without use of cmap).
3434 * We construct the schedule row s and write it as a linear
3435 * combination of (linear combinations of) previously computed schedule rows.
3436 * s = Q c or c = U s.
3437 * If the final entries of c are all zero, then the solution is trivial.
3439 static int is_trivial(struct isl_sched_node *node, __isl_keep isl_vec *sol)
3441 int i;
3442 int pos;
3443 int trivial;
3444 isl_ctx *ctx;
3445 isl_vec *node_sol;
3447 if (!sol)
3448 return -1;
3449 if (node->nvar == node->rank)
3450 return 0;
3452 ctx = isl_vec_get_ctx(sol);
3453 node_sol = isl_vec_alloc(ctx, node->nvar);
3454 if (!node_sol)
3455 return -1;
3457 pos = 1 + node->start + 1 + 2 * node->nparam;
3459 for (i = 0; i < node->nvar; ++i)
3460 isl_int_sub(node_sol->el[i],
3461 sol->el[pos + 2 * i + 1], sol->el[pos + 2 * i]);
3463 node_sol = isl_mat_vec_product(isl_mat_copy(node->cinv), node_sol);
3465 if (!node_sol)
3466 return -1;
3468 trivial = isl_seq_first_non_zero(node_sol->el + node->rank,
3469 node->nvar - node->rank) == -1;
3471 isl_vec_free(node_sol);
3473 return trivial;
3476 /* Is the schedule row "sol" trivial on any node where it should
3477 * not be trivial?
3478 * "sol" has been computed in terms of the original iterators
3479 * (i.e., without use of cmap).
3480 * Return 1 if any solution is trivial, 0 if they are not and -1 on error.
3482 static int is_any_trivial(struct isl_sched_graph *graph,
3483 __isl_keep isl_vec *sol)
3485 int i;
3487 for (i = 0; i < graph->n; ++i) {
3488 struct isl_sched_node *node = &graph->node[i];
3489 int trivial;
3491 if (!needs_row(graph, node))
3492 continue;
3493 trivial = is_trivial(node, sol);
3494 if (trivial < 0 || trivial)
3495 return trivial;
3498 return 0;
3501 /* Construct a schedule row for each node such that as many dependences
3502 * as possible are carried and then continue with the next band.
3504 * If the computed schedule row turns out to be trivial on one or
3505 * more nodes where it should not be trivial, then we throw it away
3506 * and try again on each component separately.
3508 * If there is only one component, then we accept the schedule row anyway,
3509 * but we do not consider it as a complete row and therefore do not
3510 * increment graph->n_row. Note that the ranks of the nodes that
3511 * do get a non-trivial schedule part will get updated regardless and
3512 * graph->maxvar is computed based on these ranks. The test for
3513 * whether more schedule rows are required in compute_schedule_wcc
3514 * is therefore not affected.
3516 * Insert a band corresponding to the schedule row at position "node"
3517 * of the schedule tree and continue with the construction of the schedule.
3518 * This insertion and the continued construction is performed by split_scaled
3519 * after optionally checking for non-trivial common divisors.
3521 static __isl_give isl_schedule_node *carry_dependences(
3522 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph)
3524 int i;
3525 int n_edge;
3526 int trivial;
3527 isl_ctx *ctx;
3528 isl_vec *sol;
3529 isl_basic_set *lp;
3531 if (!node)
3532 return NULL;
3534 n_edge = 0;
3535 for (i = 0; i < graph->n_edge; ++i)
3536 n_edge += graph->edge[i].map->n;
3538 ctx = isl_schedule_node_get_ctx(node);
3539 if (setup_carry_lp(ctx, graph) < 0)
3540 return isl_schedule_node_free(node);
3542 lp = isl_basic_set_copy(graph->lp);
3543 sol = isl_tab_basic_set_non_neg_lexmin(lp);
3544 if (!sol)
3545 return isl_schedule_node_free(node);
3547 if (sol->size == 0) {
3548 isl_vec_free(sol);
3549 isl_die(ctx, isl_error_internal,
3550 "error in schedule construction",
3551 return isl_schedule_node_free(node));
3554 isl_int_divexact(sol->el[1], sol->el[1], sol->el[0]);
3555 if (isl_int_cmp_si(sol->el[1], n_edge) >= 0) {
3556 isl_vec_free(sol);
3557 isl_die(ctx, isl_error_unknown,
3558 "unable to carry dependences",
3559 return isl_schedule_node_free(node));
3562 trivial = is_any_trivial(graph, sol);
3563 if (trivial < 0) {
3564 sol = isl_vec_free(sol);
3565 } else if (trivial && graph->scc > 1) {
3566 isl_vec_free(sol);
3567 return compute_component_schedule(node, graph, 1);
3570 if (update_schedule(graph, sol, 0, 0) < 0)
3571 return isl_schedule_node_free(node);
3572 if (trivial)
3573 graph->n_row--;
3575 return split_scaled(node, graph);
3578 /* Are there any (non-empty) (conditional) validity edges in the graph?
3580 static int has_validity_edges(struct isl_sched_graph *graph)
3582 int i;
3584 for (i = 0; i < graph->n_edge; ++i) {
3585 int empty;
3587 empty = isl_map_plain_is_empty(graph->edge[i].map);
3588 if (empty < 0)
3589 return -1;
3590 if (empty)
3591 continue;
3592 if (graph->edge[i].validity ||
3593 graph->edge[i].conditional_validity)
3594 return 1;
3597 return 0;
3600 /* Should we apply a Feautrier step?
3601 * That is, did the user request the Feautrier algorithm and are
3602 * there any validity dependences (left)?
3604 static int need_feautrier_step(isl_ctx *ctx, struct isl_sched_graph *graph)
3606 if (ctx->opt->schedule_algorithm != ISL_SCHEDULE_ALGORITHM_FEAUTRIER)
3607 return 0;
3609 return has_validity_edges(graph);
3612 /* Compute a schedule for a connected dependence graph using Feautrier's
3613 * multi-dimensional scheduling algorithm and return the updated schedule node.
3615 * The original algorithm is described in [1].
3616 * The main idea is to minimize the number of scheduling dimensions, by
3617 * trying to satisfy as many dependences as possible per scheduling dimension.
3619 * [1] P. Feautrier, Some Efficient Solutions to the Affine Scheduling
3620 * Problem, Part II: Multi-Dimensional Time.
3621 * In Intl. Journal of Parallel Programming, 1992.
3623 static __isl_give isl_schedule_node *compute_schedule_wcc_feautrier(
3624 isl_schedule_node *node, struct isl_sched_graph *graph)
3626 return carry_dependences(node, graph);
3629 /* Turn off the "local" bit on all (condition) edges.
3631 static void clear_local_edges(struct isl_sched_graph *graph)
3633 int i;
3635 for (i = 0; i < graph->n_edge; ++i)
3636 if (graph->edge[i].condition)
3637 graph->edge[i].local = 0;
3640 /* Does "graph" have both condition and conditional validity edges?
3642 static int need_condition_check(struct isl_sched_graph *graph)
3644 int i;
3645 int any_condition = 0;
3646 int any_conditional_validity = 0;
3648 for (i = 0; i < graph->n_edge; ++i) {
3649 if (graph->edge[i].condition)
3650 any_condition = 1;
3651 if (graph->edge[i].conditional_validity)
3652 any_conditional_validity = 1;
3655 return any_condition && any_conditional_validity;
3658 /* Does "graph" contain any coincidence edge?
3660 static int has_any_coincidence(struct isl_sched_graph *graph)
3662 int i;
3664 for (i = 0; i < graph->n_edge; ++i)
3665 if (graph->edge[i].coincidence)
3666 return 1;
3668 return 0;
3671 /* Extract the final schedule row as a map with the iteration domain
3672 * of "node" as domain.
3674 static __isl_give isl_map *final_row(struct isl_sched_node *node)
3676 isl_local_space *ls;
3677 isl_aff *aff;
3678 int row;
3680 row = isl_mat_rows(node->sched) - 1;
3681 ls = isl_local_space_from_space(isl_space_copy(node->space));
3682 aff = extract_schedule_row(ls, node, row);
3683 return isl_map_from_aff(aff);
3686 /* Is the conditional validity dependence in the edge with index "edge_index"
3687 * violated by the latest (i.e., final) row of the schedule?
3688 * That is, is i scheduled after j
3689 * for any conditional validity dependence i -> j?
3691 static int is_violated(struct isl_sched_graph *graph, int edge_index)
3693 isl_map *src_sched, *dst_sched, *map;
3694 struct isl_sched_edge *edge = &graph->edge[edge_index];
3695 int empty;
3697 src_sched = final_row(edge->src);
3698 dst_sched = final_row(edge->dst);
3699 map = isl_map_copy(edge->map);
3700 map = isl_map_apply_domain(map, src_sched);
3701 map = isl_map_apply_range(map, dst_sched);
3702 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_out, 0);
3703 empty = isl_map_is_empty(map);
3704 isl_map_free(map);
3706 if (empty < 0)
3707 return -1;
3709 return !empty;
3712 /* Does the domain of "umap" intersect "uset"?
3714 static int domain_intersects(__isl_keep isl_union_map *umap,
3715 __isl_keep isl_union_set *uset)
3717 int empty;
3719 umap = isl_union_map_copy(umap);
3720 umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(uset));
3721 empty = isl_union_map_is_empty(umap);
3722 isl_union_map_free(umap);
3724 return empty < 0 ? -1 : !empty;
3727 /* Does the range of "umap" intersect "uset"?
3729 static int range_intersects(__isl_keep isl_union_map *umap,
3730 __isl_keep isl_union_set *uset)
3732 int empty;
3734 umap = isl_union_map_copy(umap);
3735 umap = isl_union_map_intersect_range(umap, isl_union_set_copy(uset));
3736 empty = isl_union_map_is_empty(umap);
3737 isl_union_map_free(umap);
3739 return empty < 0 ? -1 : !empty;
3742 /* Are the condition dependences of "edge" local with respect to
3743 * the current schedule?
3745 * That is, are domain and range of the condition dependences mapped
3746 * to the same point?
3748 * In other words, is the condition false?
3750 static int is_condition_false(struct isl_sched_edge *edge)
3752 isl_union_map *umap;
3753 isl_map *map, *sched, *test;
3754 int local;
3756 umap = isl_union_map_copy(edge->tagged_condition);
3757 umap = isl_union_map_zip(umap);
3758 umap = isl_union_set_unwrap(isl_union_map_domain(umap));
3759 map = isl_map_from_union_map(umap);
3761 sched = node_extract_schedule(edge->src);
3762 map = isl_map_apply_domain(map, sched);
3763 sched = node_extract_schedule(edge->dst);
3764 map = isl_map_apply_range(map, sched);
3766 test = isl_map_identity(isl_map_get_space(map));
3767 local = isl_map_is_subset(map, test);
3768 isl_map_free(map);
3769 isl_map_free(test);
3771 return local;
3774 /* Does "graph" have any satisfied condition edges that
3775 * are adjacent to the conditional validity constraint with
3776 * domain "conditional_source" and range "conditional_sink"?
3778 * A satisfied condition is one that is not local.
3779 * If a condition was forced to be local already (i.e., marked as local)
3780 * then there is no need to check if it is in fact local.
3782 * Additionally, mark all adjacent condition edges found as local.
3784 static int has_adjacent_true_conditions(struct isl_sched_graph *graph,
3785 __isl_keep isl_union_set *conditional_source,
3786 __isl_keep isl_union_set *conditional_sink)
3788 int i;
3789 int any = 0;
3791 for (i = 0; i < graph->n_edge; ++i) {
3792 int adjacent, local;
3793 isl_union_map *condition;
3795 if (!graph->edge[i].condition)
3796 continue;
3797 if (graph->edge[i].local)
3798 continue;
3800 condition = graph->edge[i].tagged_condition;
3801 adjacent = domain_intersects(condition, conditional_sink);
3802 if (adjacent >= 0 && !adjacent)
3803 adjacent = range_intersects(condition,
3804 conditional_source);
3805 if (adjacent < 0)
3806 return -1;
3807 if (!adjacent)
3808 continue;
3810 graph->edge[i].local = 1;
3812 local = is_condition_false(&graph->edge[i]);
3813 if (local < 0)
3814 return -1;
3815 if (!local)
3816 any = 1;
3819 return any;
3822 /* Are there any violated conditional validity dependences with
3823 * adjacent condition dependences that are not local with respect
3824 * to the current schedule?
3825 * That is, is the conditional validity constraint violated?
3827 * Additionally, mark all those adjacent condition dependences as local.
3828 * We also mark those adjacent condition dependences that were not marked
3829 * as local before, but just happened to be local already. This ensures
3830 * that they remain local if the schedule is recomputed.
3832 * We first collect domain and range of all violated conditional validity
3833 * dependences and then check if there are any adjacent non-local
3834 * condition dependences.
3836 static int has_violated_conditional_constraint(isl_ctx *ctx,
3837 struct isl_sched_graph *graph)
3839 int i;
3840 int any = 0;
3841 isl_union_set *source, *sink;
3843 source = isl_union_set_empty(isl_space_params_alloc(ctx, 0));
3844 sink = isl_union_set_empty(isl_space_params_alloc(ctx, 0));
3845 for (i = 0; i < graph->n_edge; ++i) {
3846 isl_union_set *uset;
3847 isl_union_map *umap;
3848 int violated;
3850 if (!graph->edge[i].conditional_validity)
3851 continue;
3853 violated = is_violated(graph, i);
3854 if (violated < 0)
3855 goto error;
3856 if (!violated)
3857 continue;
3859 any = 1;
3861 umap = isl_union_map_copy(graph->edge[i].tagged_validity);
3862 uset = isl_union_map_domain(umap);
3863 source = isl_union_set_union(source, uset);
3864 source = isl_union_set_coalesce(source);
3866 umap = isl_union_map_copy(graph->edge[i].tagged_validity);
3867 uset = isl_union_map_range(umap);
3868 sink = isl_union_set_union(sink, uset);
3869 sink = isl_union_set_coalesce(sink);
3872 if (any)
3873 any = has_adjacent_true_conditions(graph, source, sink);
3875 isl_union_set_free(source);
3876 isl_union_set_free(sink);
3877 return any;
3878 error:
3879 isl_union_set_free(source);
3880 isl_union_set_free(sink);
3881 return -1;
3884 /* Compute a schedule for a connected dependence graph and return
3885 * the updated schedule node.
3887 * We try to find a sequence of as many schedule rows as possible that result
3888 * in non-negative dependence distances (independent of the previous rows
3889 * in the sequence, i.e., such that the sequence is tilable), with as
3890 * many of the initial rows as possible satisfying the coincidence constraints.
3891 * If we can't find any more rows we either
3892 * - split between SCCs and start over (assuming we found an interesting
3893 * pair of SCCs between which to split)
3894 * - continue with the next band (assuming the current band has at least
3895 * one row)
3896 * - try to carry as many dependences as possible and continue with the next
3897 * band
3898 * In each case, we first insert a band node in the schedule tree
3899 * if any rows have been computed.
3901 * If Feautrier's algorithm is selected, we first recursively try to satisfy
3902 * as many validity dependences as possible. When all validity dependences
3903 * are satisfied we extend the schedule to a full-dimensional schedule.
3905 * If we manage to complete the schedule, we insert a band node
3906 * (if any schedule rows were computed) and we finish off by topologically
3907 * sorting the statements based on the remaining dependences.
3909 * If ctx->opt->schedule_outer_coincidence is set, then we force the
3910 * outermost dimension to satisfy the coincidence constraints. If this
3911 * turns out to be impossible, we fall back on the general scheme above
3912 * and try to carry as many dependences as possible.
3914 * If "graph" contains both condition and conditional validity dependences,
3915 * then we need to check that that the conditional schedule constraint
3916 * is satisfied, i.e., there are no violated conditional validity dependences
3917 * that are adjacent to any non-local condition dependences.
3918 * If there are, then we mark all those adjacent condition dependences
3919 * as local and recompute the current band. Those dependences that
3920 * are marked local will then be forced to be local.
3921 * The initial computation is performed with no dependences marked as local.
3922 * If we are lucky, then there will be no violated conditional validity
3923 * dependences adjacent to any non-local condition dependences.
3924 * Otherwise, we mark some additional condition dependences as local and
3925 * recompute. We continue this process until there are no violations left or
3926 * until we are no longer able to compute a schedule.
3927 * Since there are only a finite number of dependences,
3928 * there will only be a finite number of iterations.
3930 static __isl_give isl_schedule_node *compute_schedule_wcc(
3931 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph)
3933 int has_coincidence;
3934 int use_coincidence;
3935 int force_coincidence = 0;
3936 int check_conditional;
3937 isl_ctx *ctx;
3939 if (!node)
3940 return NULL;
3942 ctx = isl_schedule_node_get_ctx(node);
3943 if (detect_sccs(ctx, graph) < 0)
3944 return isl_schedule_node_free(node);
3945 if (sort_sccs(graph) < 0)
3946 return isl_schedule_node_free(node);
3948 if (compute_maxvar(graph) < 0)
3949 return isl_schedule_node_free(node);
3951 if (need_feautrier_step(ctx, graph))
3952 return compute_schedule_wcc_feautrier(node, graph);
3954 clear_local_edges(graph);
3955 check_conditional = need_condition_check(graph);
3956 has_coincidence = has_any_coincidence(graph);
3958 if (ctx->opt->schedule_outer_coincidence)
3959 force_coincidence = 1;
3961 use_coincidence = has_coincidence;
3962 while (graph->n_row < graph->maxvar) {
3963 isl_vec *sol;
3964 int violated;
3965 int coincident;
3967 graph->src_scc = -1;
3968 graph->dst_scc = -1;
3970 if (setup_lp(ctx, graph, use_coincidence) < 0)
3971 return isl_schedule_node_free(node);
3972 sol = solve_lp(graph);
3973 if (!sol)
3974 return isl_schedule_node_free(node);
3975 if (sol->size == 0) {
3976 int empty = graph->n_total_row == graph->band_start;
3978 isl_vec_free(sol);
3979 if (use_coincidence && (!force_coincidence || !empty)) {
3980 use_coincidence = 0;
3981 continue;
3983 if (!ctx->opt->schedule_maximize_band_depth && !empty)
3984 return compute_next_band(node, graph, 1);
3985 if (graph->src_scc >= 0)
3986 return compute_split_schedule(node, graph);
3987 if (!empty)
3988 return compute_next_band(node, graph, 1);
3989 return carry_dependences(node, graph);
3991 coincident = !has_coincidence || use_coincidence;
3992 if (update_schedule(graph, sol, 1, coincident) < 0)
3993 return isl_schedule_node_free(node);
3995 if (!check_conditional)
3996 continue;
3997 violated = has_violated_conditional_constraint(ctx, graph);
3998 if (violated < 0)
3999 return isl_schedule_node_free(node);
4000 if (!violated)
4001 continue;
4002 if (reset_band(graph) < 0)
4003 return isl_schedule_node_free(node);
4004 use_coincidence = has_coincidence;
4007 if (graph->n_total_row > graph->band_start) {
4008 node = insert_current_band(node, graph, 1);
4009 node = isl_schedule_node_child(node, 0);
4011 node = sort_statements(node, graph);
4012 if (graph->n_total_row > graph->band_start)
4013 node = isl_schedule_node_parent(node);
4015 return node;
4018 /* Compute a schedule for each group of nodes identified by node->scc
4019 * separately and then combine them in a sequence node (or as set node
4020 * if graph->weak is set) inserted at position "node" of the schedule tree.
4021 * Return the updated schedule node.
4023 * If "wcc" is set then each of the groups belongs to a single
4024 * weakly connected component in the dependence graph so that
4025 * there is no need for compute_sub_schedule to look for weakly
4026 * connected components.
4028 static __isl_give isl_schedule_node *compute_component_schedule(
4029 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph,
4030 int wcc)
4032 int component, i;
4033 int n, n_edge;
4034 int orig_total_row;
4035 isl_ctx *ctx;
4036 isl_union_set_list *filters;
4038 if (!node)
4039 return NULL;
4040 ctx = isl_schedule_node_get_ctx(node);
4042 filters = extract_sccs(ctx, graph);
4043 if (graph->weak)
4044 node = isl_schedule_node_insert_set(node, filters);
4045 else
4046 node = isl_schedule_node_insert_sequence(node, filters);
4048 orig_total_row = graph->n_total_row;
4049 for (component = 0; component < graph->scc; ++component) {
4050 n = 0;
4051 for (i = 0; i < graph->n; ++i)
4052 if (graph->node[i].scc == component)
4053 n++;
4054 n_edge = 0;
4055 for (i = 0; i < graph->n_edge; ++i)
4056 if (graph->edge[i].src->scc == component &&
4057 graph->edge[i].dst->scc == component)
4058 n_edge++;
4060 node = isl_schedule_node_child(node, component);
4061 node = isl_schedule_node_child(node, 0);
4062 node = compute_sub_schedule(node, ctx, graph, n, n_edge,
4063 &node_scc_exactly,
4064 &edge_scc_exactly, component, wcc);
4065 node = isl_schedule_node_parent(node);
4066 node = isl_schedule_node_parent(node);
4067 graph->n_total_row = orig_total_row;
4070 return node;
4073 /* Compute a schedule for the given dependence graph and insert it at "node".
4074 * Return the updated schedule node.
4076 * We first check if the graph is connected (through validity and conditional
4077 * validity dependences) and, if not, compute a schedule
4078 * for each component separately.
4079 * If schedule_fuse is set to minimal fusion, then we check for strongly
4080 * connected components instead and compute a separate schedule for
4081 * each such strongly connected component.
4083 static __isl_give isl_schedule_node *compute_schedule(isl_schedule_node *node,
4084 struct isl_sched_graph *graph)
4086 isl_ctx *ctx;
4088 if (!node)
4089 return NULL;
4091 ctx = isl_schedule_node_get_ctx(node);
4092 if (ctx->opt->schedule_fuse == ISL_SCHEDULE_FUSE_MIN) {
4093 if (detect_sccs(ctx, graph) < 0)
4094 return isl_schedule_node_free(node);
4095 } else {
4096 if (detect_wccs(ctx, graph) < 0)
4097 return isl_schedule_node_free(node);
4100 if (graph->scc > 1)
4101 return compute_component_schedule(node, graph, 1);
4103 return compute_schedule_wcc(node, graph);
4106 /* Compute a schedule on sc->domain that respects the given schedule
4107 * constraints.
4109 * In particular, the schedule respects all the validity dependences.
4110 * If the default isl scheduling algorithm is used, it tries to minimize
4111 * the dependence distances over the proximity dependences.
4112 * If Feautrier's scheduling algorithm is used, the proximity dependence
4113 * distances are only minimized during the extension to a full-dimensional
4114 * schedule.
4116 * If there are any condition and conditional validity dependences,
4117 * then the conditional validity dependences may be violated inside
4118 * a tilable band, provided they have no adjacent non-local
4119 * condition dependences.
4121 * The context is included in the domain before the nodes of
4122 * the graphs are extracted in order to be able to exploit
4123 * any possible additional equalities.
4124 * However, the returned schedule contains the original domain
4125 * (before this intersection).
4127 __isl_give isl_schedule *isl_schedule_constraints_compute_schedule(
4128 __isl_take isl_schedule_constraints *sc)
4130 isl_ctx *ctx = isl_schedule_constraints_get_ctx(sc);
4131 struct isl_sched_graph graph = { 0 };
4132 isl_schedule *sched;
4133 isl_schedule_node *node;
4134 isl_union_set *domain;
4135 struct isl_extract_edge_data data;
4136 enum isl_edge_type i;
4137 int r;
4139 sc = isl_schedule_constraints_align_params(sc);
4140 if (!sc)
4141 return NULL;
4143 graph.n = isl_union_set_n_set(sc->domain);
4144 if (graph.n == 0) {
4145 isl_union_set *domain = isl_union_set_copy(sc->domain);
4146 sched = isl_schedule_from_domain(domain);
4147 goto done;
4149 if (graph_alloc(ctx, &graph, graph.n,
4150 isl_schedule_constraints_n_map(sc)) < 0)
4151 goto error;
4152 if (compute_max_row(&graph, sc) < 0)
4153 goto error;
4154 graph.root = 1;
4155 graph.n = 0;
4156 domain = isl_union_set_copy(sc->domain);
4157 domain = isl_union_set_intersect_params(domain,
4158 isl_set_copy(sc->context));
4159 r = isl_union_set_foreach_set(domain, &extract_node, &graph);
4160 isl_union_set_free(domain);
4161 if (r < 0)
4162 goto error;
4163 if (graph_init_table(ctx, &graph) < 0)
4164 goto error;
4165 for (i = isl_edge_first; i <= isl_edge_last; ++i)
4166 graph.max_edge[i] = isl_union_map_n_map(sc->constraint[i]);
4167 if (graph_init_edge_tables(ctx, &graph) < 0)
4168 goto error;
4169 graph.n_edge = 0;
4170 data.graph = &graph;
4171 for (i = isl_edge_first; i <= isl_edge_last; ++i) {
4172 data.type = i;
4173 if (isl_union_map_foreach_map(sc->constraint[i],
4174 &extract_edge, &data) < 0)
4175 goto error;
4178 node = isl_schedule_node_from_domain(isl_union_set_copy(sc->domain));
4179 node = isl_schedule_node_child(node, 0);
4180 node = compute_schedule(node, &graph);
4181 sched = isl_schedule_node_get_schedule(node);
4182 isl_schedule_node_free(node);
4184 done:
4185 graph_free(ctx, &graph);
4186 isl_schedule_constraints_free(sc);
4188 return sched;
4189 error:
4190 graph_free(ctx, &graph);
4191 isl_schedule_constraints_free(sc);
4192 return NULL;
4195 /* Compute a schedule for the given union of domains that respects
4196 * all the validity dependences and minimizes
4197 * the dependence distances over the proximity dependences.
4199 * This function is kept for backward compatibility.
4201 __isl_give isl_schedule *isl_union_set_compute_schedule(
4202 __isl_take isl_union_set *domain,
4203 __isl_take isl_union_map *validity,
4204 __isl_take isl_union_map *proximity)
4206 isl_schedule_constraints *sc;
4208 sc = isl_schedule_constraints_on_domain(domain);
4209 sc = isl_schedule_constraints_set_validity(sc, validity);
4210 sc = isl_schedule_constraints_set_proximity(sc, proximity);
4212 return isl_schedule_constraints_compute_schedule(sc);