isl_schedule_node.c: collect_filter_prefix: allow caller to initialize filter
[isl.git] / isl_scheduler.c
blob705eada95417c6626c46e643f4e4476a65001c63
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;
2368 nrow = isl_mat_rows(node->sched);
2369 if (node->compressed)
2370 space = isl_multi_aff_get_domain_space(node->decompress);
2371 else
2372 space = isl_space_copy(node->space);
2373 ls = isl_local_space_from_space(isl_space_copy(space));
2374 space = isl_space_from_domain(space);
2375 space = isl_space_add_dims(space, isl_dim_out, n);
2376 ma = isl_multi_aff_zero(space);
2378 for (i = first; i < first + n; ++i) {
2379 aff = extract_schedule_row(isl_local_space_copy(ls), node, i);
2380 ma = isl_multi_aff_set_aff(ma, i - first, aff);
2383 isl_local_space_free(ls);
2385 if (node->compressed)
2386 ma = isl_multi_aff_pullback_multi_aff(ma,
2387 isl_multi_aff_copy(node->compress));
2389 return ma;
2392 /* Convert node->sched into a multi_aff and return this multi_aff.
2394 * The result is defined over the uncompressed node domain.
2396 static __isl_give isl_multi_aff *node_extract_schedule_multi_aff(
2397 struct isl_sched_node *node)
2399 int nrow;
2401 nrow = isl_mat_rows(node->sched);
2402 return node_extract_partial_schedule_multi_aff(node, 0, nrow);
2405 /* Convert node->sched into a map and return this map.
2407 * The result is cached in node->sched_map, which needs to be released
2408 * whenever node->sched is updated.
2409 * It is defined over the uncompressed node domain.
2411 static __isl_give isl_map *node_extract_schedule(struct isl_sched_node *node)
2413 if (!node->sched_map) {
2414 isl_multi_aff *ma;
2416 ma = node_extract_schedule_multi_aff(node);
2417 node->sched_map = isl_map_from_multi_aff(ma);
2420 return isl_map_copy(node->sched_map);
2423 /* Construct a map that can be used to update a dependence relation
2424 * based on the current schedule.
2425 * That is, construct a map expressing that source and sink
2426 * are executed within the same iteration of the current schedule.
2427 * This map can then be intersected with the dependence relation.
2428 * This is not the most efficient way, but this shouldn't be a critical
2429 * operation.
2431 static __isl_give isl_map *specializer(struct isl_sched_node *src,
2432 struct isl_sched_node *dst)
2434 isl_map *src_sched, *dst_sched;
2436 src_sched = node_extract_schedule(src);
2437 dst_sched = node_extract_schedule(dst);
2438 return isl_map_apply_range(src_sched, isl_map_reverse(dst_sched));
2441 /* Intersect the domains of the nested relations in domain and range
2442 * of "umap" with "map".
2444 static __isl_give isl_union_map *intersect_domains(
2445 __isl_take isl_union_map *umap, __isl_keep isl_map *map)
2447 isl_union_set *uset;
2449 umap = isl_union_map_zip(umap);
2450 uset = isl_union_set_from_set(isl_map_wrap(isl_map_copy(map)));
2451 umap = isl_union_map_intersect_domain(umap, uset);
2452 umap = isl_union_map_zip(umap);
2453 return umap;
2456 /* Update the dependence relation of the given edge based
2457 * on the current schedule.
2458 * If the dependence is carried completely by the current schedule, then
2459 * it is removed from the edge_tables. It is kept in the list of edges
2460 * as otherwise all edge_tables would have to be recomputed.
2462 static int update_edge(struct isl_sched_graph *graph,
2463 struct isl_sched_edge *edge)
2465 isl_map *id;
2467 id = specializer(edge->src, edge->dst);
2468 edge->map = isl_map_intersect(edge->map, isl_map_copy(id));
2469 if (!edge->map)
2470 goto error;
2472 if (edge->tagged_condition) {
2473 edge->tagged_condition =
2474 intersect_domains(edge->tagged_condition, id);
2475 if (!edge->tagged_condition)
2476 goto error;
2478 if (edge->tagged_validity) {
2479 edge->tagged_validity =
2480 intersect_domains(edge->tagged_validity, id);
2481 if (!edge->tagged_validity)
2482 goto error;
2485 isl_map_free(id);
2486 if (isl_map_plain_is_empty(edge->map))
2487 graph_remove_edge(graph, edge);
2489 return 0;
2490 error:
2491 isl_map_free(id);
2492 return -1;
2495 /* Update the dependence relations of all edges based on the current schedule.
2497 static int update_edges(isl_ctx *ctx, struct isl_sched_graph *graph)
2499 int i;
2501 for (i = graph->n_edge - 1; i >= 0; --i) {
2502 if (update_edge(graph, &graph->edge[i]) < 0)
2503 return -1;
2506 return 0;
2509 static void next_band(struct isl_sched_graph *graph)
2511 graph->band_start = graph->n_total_row;
2514 /* Return the union of the universe domains of the nodes in "graph"
2515 * that satisfy "pred".
2517 static __isl_give isl_union_set *isl_sched_graph_domain(isl_ctx *ctx,
2518 struct isl_sched_graph *graph,
2519 int (*pred)(struct isl_sched_node *node, int data), int data)
2521 int i;
2522 isl_set *set;
2523 isl_union_set *dom;
2525 for (i = 0; i < graph->n; ++i)
2526 if (pred(&graph->node[i], data))
2527 break;
2529 if (i >= graph->n)
2530 isl_die(ctx, isl_error_internal,
2531 "empty component", return NULL);
2533 set = isl_set_universe(isl_space_copy(graph->node[i].space));
2534 dom = isl_union_set_from_set(set);
2536 for (i = i + 1; i < graph->n; ++i) {
2537 if (!pred(&graph->node[i], data))
2538 continue;
2539 set = isl_set_universe(isl_space_copy(graph->node[i].space));
2540 dom = isl_union_set_union(dom, isl_union_set_from_set(set));
2543 return dom;
2546 /* Return a list of unions of universe domains, where each element
2547 * in the list corresponds to an SCC (or WCC) indexed by node->scc.
2549 static __isl_give isl_union_set_list *extract_sccs(isl_ctx *ctx,
2550 struct isl_sched_graph *graph)
2552 int i;
2553 isl_union_set_list *filters;
2555 filters = isl_union_set_list_alloc(ctx, graph->scc);
2556 for (i = 0; i < graph->scc; ++i) {
2557 isl_union_set *dom;
2559 dom = isl_sched_graph_domain(ctx, graph, &node_scc_exactly, i);
2560 filters = isl_union_set_list_add(filters, dom);
2563 return filters;
2566 /* Return a list of two unions of universe domains, one for the SCCs up
2567 * to and including graph->src_scc and another for the other SCCS.
2569 static __isl_give isl_union_set_list *extract_split(isl_ctx *ctx,
2570 struct isl_sched_graph *graph)
2572 isl_union_set *dom;
2573 isl_union_set_list *filters;
2575 filters = isl_union_set_list_alloc(ctx, 2);
2576 dom = isl_sched_graph_domain(ctx, graph,
2577 &node_scc_at_most, graph->src_scc);
2578 filters = isl_union_set_list_add(filters, dom);
2579 dom = isl_sched_graph_domain(ctx, graph,
2580 &node_scc_at_least, graph->src_scc + 1);
2581 filters = isl_union_set_list_add(filters, dom);
2583 return filters;
2586 /* Topologically sort statements mapped to the same schedule iteration
2587 * and add insert a sequence node in front of "node"
2588 * corresponding to this order.
2590 static __isl_give isl_schedule_node *sort_statements(
2591 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph)
2593 isl_ctx *ctx;
2594 isl_union_set_list *filters;
2596 if (!node)
2597 return NULL;
2599 ctx = isl_schedule_node_get_ctx(node);
2600 if (graph->n < 1)
2601 isl_die(ctx, isl_error_internal,
2602 "graph should have at least one node",
2603 return isl_schedule_node_free(node));
2605 if (graph->n == 1)
2606 return node;
2608 if (update_edges(ctx, graph) < 0)
2609 return isl_schedule_node_free(node);
2611 if (graph->n_edge == 0)
2612 return node;
2614 if (detect_sccs(ctx, graph) < 0)
2615 return isl_schedule_node_free(node);
2617 filters = extract_sccs(ctx, graph);
2618 node = isl_schedule_node_insert_sequence(node, filters);
2620 return node;
2623 /* Copy nodes that satisfy node_pred from the src dependence graph
2624 * to the dst dependence graph.
2626 static int copy_nodes(struct isl_sched_graph *dst, struct isl_sched_graph *src,
2627 int (*node_pred)(struct isl_sched_node *node, int data), int data)
2629 int i;
2631 dst->n = 0;
2632 for (i = 0; i < src->n; ++i) {
2633 int j;
2635 if (!node_pred(&src->node[i], data))
2636 continue;
2638 j = dst->n;
2639 dst->node[j].space = isl_space_copy(src->node[i].space);
2640 dst->node[j].compressed = src->node[i].compressed;
2641 dst->node[j].hull = isl_set_copy(src->node[i].hull);
2642 dst->node[j].compress =
2643 isl_multi_aff_copy(src->node[i].compress);
2644 dst->node[j].decompress =
2645 isl_multi_aff_copy(src->node[i].decompress);
2646 dst->node[j].nvar = src->node[i].nvar;
2647 dst->node[j].nparam = src->node[i].nparam;
2648 dst->node[j].sched = isl_mat_copy(src->node[i].sched);
2649 dst->node[j].sched_map = isl_map_copy(src->node[i].sched_map);
2650 dst->node[j].coincident = src->node[i].coincident;
2651 dst->n++;
2653 if (!dst->node[j].space || !dst->node[j].sched)
2654 return -1;
2655 if (dst->node[j].compressed &&
2656 (!dst->node[j].hull || !dst->node[j].compress ||
2657 !dst->node[j].decompress))
2658 return -1;
2661 return 0;
2664 /* Copy non-empty edges that satisfy edge_pred from the src dependence graph
2665 * to the dst dependence graph.
2666 * If the source or destination node of the edge is not in the destination
2667 * graph, then it must be a backward proximity edge and it should simply
2668 * be ignored.
2670 static int copy_edges(isl_ctx *ctx, struct isl_sched_graph *dst,
2671 struct isl_sched_graph *src,
2672 int (*edge_pred)(struct isl_sched_edge *edge, int data), int data)
2674 int i;
2675 enum isl_edge_type t;
2677 dst->n_edge = 0;
2678 for (i = 0; i < src->n_edge; ++i) {
2679 struct isl_sched_edge *edge = &src->edge[i];
2680 isl_map *map;
2681 isl_union_map *tagged_condition;
2682 isl_union_map *tagged_validity;
2683 struct isl_sched_node *dst_src, *dst_dst;
2685 if (!edge_pred(edge, data))
2686 continue;
2688 if (isl_map_plain_is_empty(edge->map))
2689 continue;
2691 dst_src = graph_find_node(ctx, dst, edge->src->space);
2692 dst_dst = graph_find_node(ctx, dst, edge->dst->space);
2693 if (!dst_src || !dst_dst) {
2694 if (edge->validity || edge->conditional_validity)
2695 isl_die(ctx, isl_error_internal,
2696 "backward (conditional) validity edge",
2697 return -1);
2698 continue;
2701 map = isl_map_copy(edge->map);
2702 tagged_condition = isl_union_map_copy(edge->tagged_condition);
2703 tagged_validity = isl_union_map_copy(edge->tagged_validity);
2705 dst->edge[dst->n_edge].src = dst_src;
2706 dst->edge[dst->n_edge].dst = dst_dst;
2707 dst->edge[dst->n_edge].map = map;
2708 dst->edge[dst->n_edge].tagged_condition = tagged_condition;
2709 dst->edge[dst->n_edge].tagged_validity = tagged_validity;
2710 dst->edge[dst->n_edge].validity = edge->validity;
2711 dst->edge[dst->n_edge].proximity = edge->proximity;
2712 dst->edge[dst->n_edge].coincidence = edge->coincidence;
2713 dst->edge[dst->n_edge].condition = edge->condition;
2714 dst->edge[dst->n_edge].conditional_validity =
2715 edge->conditional_validity;
2716 dst->n_edge++;
2718 if (edge->tagged_condition && !tagged_condition)
2719 return -1;
2720 if (edge->tagged_validity && !tagged_validity)
2721 return -1;
2723 for (t = isl_edge_first; t <= isl_edge_last; ++t) {
2724 if (edge !=
2725 graph_find_edge(src, t, edge->src, edge->dst))
2726 continue;
2727 if (graph_edge_table_add(ctx, dst, t,
2728 &dst->edge[dst->n_edge - 1]) < 0)
2729 return -1;
2733 return 0;
2736 /* Compute the maximal number of variables over all nodes.
2737 * This is the maximal number of linearly independent schedule
2738 * rows that we need to compute.
2739 * Just in case we end up in a part of the dependence graph
2740 * with only lower-dimensional domains, we make sure we will
2741 * compute the required amount of extra linearly independent rows.
2743 static int compute_maxvar(struct isl_sched_graph *graph)
2745 int i;
2747 graph->maxvar = 0;
2748 for (i = 0; i < graph->n; ++i) {
2749 struct isl_sched_node *node = &graph->node[i];
2750 int nvar;
2752 if (node_update_cmap(node) < 0)
2753 return -1;
2754 nvar = node->nvar + graph->n_row - node->rank;
2755 if (nvar > graph->maxvar)
2756 graph->maxvar = nvar;
2759 return 0;
2762 static __isl_give isl_schedule_node *compute_schedule(isl_schedule_node *node,
2763 struct isl_sched_graph *graph);
2764 static __isl_give isl_schedule_node *compute_schedule_wcc(
2765 isl_schedule_node *node, struct isl_sched_graph *graph);
2767 /* Compute a schedule for a subgraph of "graph". In particular, for
2768 * the graph composed of nodes that satisfy node_pred and edges that
2769 * that satisfy edge_pred. The caller should precompute the number
2770 * of nodes and edges that satisfy these predicates and pass them along
2771 * as "n" and "n_edge".
2772 * If the subgraph is known to consist of a single component, then wcc should
2773 * be set and then we call compute_schedule_wcc on the constructed subgraph.
2774 * Otherwise, we call compute_schedule, which will check whether the subgraph
2775 * is connected.
2777 * The schedule is inserted at "node" and the updated schedule node
2778 * is returned.
2780 static __isl_give isl_schedule_node *compute_sub_schedule(
2781 __isl_take isl_schedule_node *node, isl_ctx *ctx,
2782 struct isl_sched_graph *graph, int n, int n_edge,
2783 int (*node_pred)(struct isl_sched_node *node, int data),
2784 int (*edge_pred)(struct isl_sched_edge *edge, int data),
2785 int data, int wcc)
2787 struct isl_sched_graph split = { 0 };
2788 int t;
2790 if (graph_alloc(ctx, &split, n, n_edge) < 0)
2791 goto error;
2792 if (copy_nodes(&split, graph, node_pred, data) < 0)
2793 goto error;
2794 if (graph_init_table(ctx, &split) < 0)
2795 goto error;
2796 for (t = 0; t <= isl_edge_last; ++t)
2797 split.max_edge[t] = graph->max_edge[t];
2798 if (graph_init_edge_tables(ctx, &split) < 0)
2799 goto error;
2800 if (copy_edges(ctx, &split, graph, edge_pred, data) < 0)
2801 goto error;
2802 split.n_row = graph->n_row;
2803 split.max_row = graph->max_row;
2804 split.n_total_row = graph->n_total_row;
2805 split.band_start = graph->band_start;
2807 if (wcc)
2808 node = compute_schedule_wcc(node, &split);
2809 else
2810 node = compute_schedule(node, &split);
2812 graph_free(ctx, &split);
2813 return node;
2814 error:
2815 graph_free(ctx, &split);
2816 return isl_schedule_node_free(node);
2819 static int edge_scc_exactly(struct isl_sched_edge *edge, int scc)
2821 return edge->src->scc == scc && edge->dst->scc == scc;
2824 static int edge_dst_scc_at_most(struct isl_sched_edge *edge, int scc)
2826 return edge->dst->scc <= scc;
2829 static int edge_src_scc_at_least(struct isl_sched_edge *edge, int scc)
2831 return edge->src->scc >= scc;
2834 /* Reset the current band by dropping all its schedule rows.
2836 static int reset_band(struct isl_sched_graph *graph)
2838 int i;
2839 int drop;
2841 drop = graph->n_total_row - graph->band_start;
2842 graph->n_total_row -= drop;
2843 graph->n_row -= drop;
2845 for (i = 0; i < graph->n; ++i) {
2846 struct isl_sched_node *node = &graph->node[i];
2848 isl_map_free(node->sched_map);
2849 node->sched_map = NULL;
2851 node->sched = isl_mat_drop_rows(node->sched,
2852 graph->band_start, drop);
2854 if (!node->sched)
2855 return -1;
2858 return 0;
2861 /* Split the current graph into two parts and compute a schedule for each
2862 * part individually. In particular, one part consists of all SCCs up
2863 * to and including graph->src_scc, while the other part contains the other
2864 * SCCS. The split is enforced by a sequence node inserted at position "node"
2865 * in the schedule tree. Return the updated schedule node.
2867 * The current band is reset. It would be possible to reuse
2868 * the previously computed rows as the first rows in the next
2869 * band, but recomputing them may result in better rows as we are looking
2870 * at a smaller part of the dependence graph.
2872 static __isl_give isl_schedule_node *compute_split_schedule(
2873 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph)
2875 int i, n, e1, e2;
2876 int orig_total_row;
2877 isl_ctx *ctx;
2878 isl_union_set_list *filters;
2880 if (!node)
2881 return NULL;
2883 if (reset_band(graph) < 0)
2884 return isl_schedule_node_free(node);
2886 n = 0;
2887 for (i = 0; i < graph->n; ++i) {
2888 struct isl_sched_node *node = &graph->node[i];
2889 int before = node->scc <= graph->src_scc;
2891 if (before)
2892 n++;
2895 e1 = e2 = 0;
2896 for (i = 0; i < graph->n_edge; ++i) {
2897 if (graph->edge[i].dst->scc <= graph->src_scc)
2898 e1++;
2899 if (graph->edge[i].src->scc > graph->src_scc)
2900 e2++;
2903 next_band(graph);
2905 ctx = isl_schedule_node_get_ctx(node);
2906 filters = extract_split(ctx, graph);
2907 node = isl_schedule_node_insert_sequence(node, filters);
2908 node = isl_schedule_node_child(node, 0);
2909 node = isl_schedule_node_child(node, 0);
2911 orig_total_row = graph->n_total_row;
2912 node = compute_sub_schedule(node, ctx, graph, n, e1,
2913 &node_scc_at_most, &edge_dst_scc_at_most,
2914 graph->src_scc, 0);
2915 node = isl_schedule_node_parent(node);
2916 node = isl_schedule_node_next_sibling(node);
2917 node = isl_schedule_node_child(node, 0);
2918 graph->n_total_row = orig_total_row;
2919 node = compute_sub_schedule(node, ctx, graph, graph->n - n, e2,
2920 &node_scc_at_least, &edge_src_scc_at_least,
2921 graph->src_scc + 1, 0);
2922 node = isl_schedule_node_parent(node);
2923 node = isl_schedule_node_parent(node);
2925 return node;
2928 /* Insert a band node at position "node" in the schedule tree corresponding
2929 * to the current band in "graph". Mark the band node permutable
2930 * if "permutable" is set.
2931 * The partial schedules and the coincidence property are extracted
2932 * from the graph nodes.
2933 * Return the updated schedule node.
2935 static __isl_give isl_schedule_node *insert_current_band(
2936 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph,
2937 int permutable)
2939 int i;
2940 int start, end, n;
2941 isl_multi_aff *ma;
2942 isl_multi_pw_aff *mpa;
2943 isl_multi_union_pw_aff *mupa;
2945 if (!node)
2946 return NULL;
2948 if (graph->n < 1)
2949 isl_die(isl_schedule_node_get_ctx(node), isl_error_internal,
2950 "graph should have at least one node",
2951 return isl_schedule_node_free(node));
2953 start = graph->band_start;
2954 end = graph->n_total_row;
2955 n = end - start;
2957 ma = node_extract_partial_schedule_multi_aff(&graph->node[0], start, n);
2958 mpa = isl_multi_pw_aff_from_multi_aff(ma);
2959 mupa = isl_multi_union_pw_aff_from_multi_pw_aff(mpa);
2961 for (i = 1; i < graph->n; ++i) {
2962 isl_multi_union_pw_aff *mupa_i;
2964 ma = node_extract_partial_schedule_multi_aff(&graph->node[i],
2965 start, n);
2966 mpa = isl_multi_pw_aff_from_multi_aff(ma);
2967 mupa_i = isl_multi_union_pw_aff_from_multi_pw_aff(mpa);
2968 mupa = isl_multi_union_pw_aff_union_add(mupa, mupa_i);
2970 node = isl_schedule_node_insert_partial_schedule(node, mupa);
2972 for (i = 0; i < n; ++i)
2973 node = isl_schedule_node_band_member_set_coincident(node, i,
2974 graph->node[0].coincident[start + i]);
2975 node = isl_schedule_node_band_set_permutable(node, permutable);
2977 return node;
2980 /* Update the dependence relations based on the current schedule,
2981 * add the current band to "node" and the continue with the computation
2982 * of the next band.
2983 * Return the updated schedule node.
2985 static __isl_give isl_schedule_node *compute_next_band(
2986 __isl_take isl_schedule_node *node,
2987 struct isl_sched_graph *graph, int permutable)
2989 isl_ctx *ctx;
2991 if (!node)
2992 return NULL;
2994 ctx = isl_schedule_node_get_ctx(node);
2995 if (update_edges(ctx, graph) < 0)
2996 return isl_schedule_node_free(node);
2997 node = insert_current_band(node, graph, permutable);
2998 next_band(graph);
3000 node = isl_schedule_node_child(node, 0);
3001 node = compute_schedule(node, graph);
3002 node = isl_schedule_node_parent(node);
3004 return node;
3007 /* Add constraints to graph->lp that force the dependence "map" (which
3008 * is part of the dependence relation of "edge")
3009 * to be respected and attempt to carry it, where the edge is one from
3010 * a node j to itself. "pos" is the sequence number of the given map.
3011 * That is, add constraints that enforce
3013 * (c_j_0 + c_j_n n + c_j_x y) - (c_j_0 + c_j_n n + c_j_x x)
3014 * = c_j_x (y - x) >= e_i
3016 * for each (x,y) in R.
3017 * We obtain general constraints on coefficients (c_0, c_n, c_x)
3018 * of valid constraints for (y - x) and then plug in (-e_i, 0, c_j_x),
3019 * with each coefficient in c_j_x represented as a pair of non-negative
3020 * coefficients.
3022 static int add_intra_constraints(struct isl_sched_graph *graph,
3023 struct isl_sched_edge *edge, __isl_take isl_map *map, int pos)
3025 unsigned total;
3026 isl_ctx *ctx = isl_map_get_ctx(map);
3027 isl_space *dim;
3028 isl_dim_map *dim_map;
3029 isl_basic_set *coef;
3030 struct isl_sched_node *node = edge->src;
3032 coef = intra_coefficients(graph, node, map);
3033 if (!coef)
3034 return -1;
3036 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
3038 total = isl_basic_set_total_dim(graph->lp);
3039 dim_map = isl_dim_map_alloc(ctx, total);
3040 isl_dim_map_range(dim_map, 3 + pos, 0, 0, 0, 1, -1);
3041 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 1, 2,
3042 isl_space_dim(dim, isl_dim_set), 1,
3043 node->nvar, -1);
3044 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 2, 2,
3045 isl_space_dim(dim, isl_dim_set), 1,
3046 node->nvar, 1);
3047 graph->lp = isl_basic_set_extend_constraints(graph->lp,
3048 coef->n_eq, coef->n_ineq);
3049 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
3050 coef, dim_map);
3051 isl_space_free(dim);
3053 return 0;
3056 /* Add constraints to graph->lp that force the dependence "map" (which
3057 * is part of the dependence relation of "edge")
3058 * to be respected and attempt to carry it, where the edge is one from
3059 * node j to node k. "pos" is the sequence number of the given map.
3060 * That is, add constraints that enforce
3062 * (c_k_0 + c_k_n n + c_k_x y) - (c_j_0 + c_j_n n + c_j_x x) >= e_i
3064 * for each (x,y) in R.
3065 * We obtain general constraints on coefficients (c_0, c_n, c_x)
3066 * of valid constraints for R and then plug in
3067 * (-e_i + c_k_0 - c_j_0, c_k_n - c_j_n, c_k_x - c_j_x)
3068 * with each coefficient (except e_i, c_k_0 and c_j_0)
3069 * represented as a pair of non-negative coefficients.
3071 static int add_inter_constraints(struct isl_sched_graph *graph,
3072 struct isl_sched_edge *edge, __isl_take isl_map *map, int pos)
3074 unsigned total;
3075 isl_ctx *ctx = isl_map_get_ctx(map);
3076 isl_space *dim;
3077 isl_dim_map *dim_map;
3078 isl_basic_set *coef;
3079 struct isl_sched_node *src = edge->src;
3080 struct isl_sched_node *dst = edge->dst;
3082 coef = inter_coefficients(graph, edge, map);
3083 if (!coef)
3084 return -1;
3086 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
3088 total = isl_basic_set_total_dim(graph->lp);
3089 dim_map = isl_dim_map_alloc(ctx, total);
3091 isl_dim_map_range(dim_map, 3 + pos, 0, 0, 0, 1, -1);
3093 isl_dim_map_range(dim_map, dst->start, 0, 0, 0, 1, 1);
3094 isl_dim_map_range(dim_map, dst->start + 1, 2, 1, 1, dst->nparam, -1);
3095 isl_dim_map_range(dim_map, dst->start + 2, 2, 1, 1, dst->nparam, 1);
3096 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 1, 2,
3097 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
3098 dst->nvar, -1);
3099 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 2, 2,
3100 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
3101 dst->nvar, 1);
3103 isl_dim_map_range(dim_map, src->start, 0, 0, 0, 1, -1);
3104 isl_dim_map_range(dim_map, src->start + 1, 2, 1, 1, src->nparam, 1);
3105 isl_dim_map_range(dim_map, src->start + 2, 2, 1, 1, src->nparam, -1);
3106 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 1, 2,
3107 isl_space_dim(dim, isl_dim_set), 1,
3108 src->nvar, 1);
3109 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 2, 2,
3110 isl_space_dim(dim, isl_dim_set), 1,
3111 src->nvar, -1);
3113 graph->lp = isl_basic_set_extend_constraints(graph->lp,
3114 coef->n_eq, coef->n_ineq);
3115 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
3116 coef, dim_map);
3117 isl_space_free(dim);
3119 return 0;
3122 /* Add constraints to graph->lp that force all (conditional) validity
3123 * dependences to be respected and attempt to carry them.
3125 static int add_all_constraints(struct isl_sched_graph *graph)
3127 int i, j;
3128 int pos;
3130 pos = 0;
3131 for (i = 0; i < graph->n_edge; ++i) {
3132 struct isl_sched_edge *edge= &graph->edge[i];
3134 if (!edge->validity && !edge->conditional_validity)
3135 continue;
3137 for (j = 0; j < edge->map->n; ++j) {
3138 isl_basic_map *bmap;
3139 isl_map *map;
3141 bmap = isl_basic_map_copy(edge->map->p[j]);
3142 map = isl_map_from_basic_map(bmap);
3144 if (edge->src == edge->dst &&
3145 add_intra_constraints(graph, edge, map, pos) < 0)
3146 return -1;
3147 if (edge->src != edge->dst &&
3148 add_inter_constraints(graph, edge, map, pos) < 0)
3149 return -1;
3150 ++pos;
3154 return 0;
3157 /* Count the number of equality and inequality constraints
3158 * that will be added to the carry_lp problem.
3159 * We count each edge exactly once.
3161 static int count_all_constraints(struct isl_sched_graph *graph,
3162 int *n_eq, int *n_ineq)
3164 int i, j;
3166 *n_eq = *n_ineq = 0;
3167 for (i = 0; i < graph->n_edge; ++i) {
3168 struct isl_sched_edge *edge= &graph->edge[i];
3169 for (j = 0; j < edge->map->n; ++j) {
3170 isl_basic_map *bmap;
3171 isl_map *map;
3173 bmap = isl_basic_map_copy(edge->map->p[j]);
3174 map = isl_map_from_basic_map(bmap);
3176 if (count_map_constraints(graph, edge, map,
3177 n_eq, n_ineq, 1, 0) < 0)
3178 return -1;
3182 return 0;
3185 /* Construct an LP problem for finding schedule coefficients
3186 * such that the schedule carries as many dependences as possible.
3187 * In particular, for each dependence i, we bound the dependence distance
3188 * from below by e_i, with 0 <= e_i <= 1 and then maximize the sum
3189 * of all e_i's. Dependence with e_i = 0 in the solution are simply
3190 * respected, while those with e_i > 0 (in practice e_i = 1) are carried.
3191 * Note that if the dependence relation is a union of basic maps,
3192 * then we have to consider each basic map individually as it may only
3193 * be possible to carry the dependences expressed by some of those
3194 * basic maps and not all off them.
3195 * Below, we consider each of those basic maps as a separate "edge".
3197 * All variables of the LP are non-negative. The actual coefficients
3198 * may be negative, so each coefficient is represented as the difference
3199 * of two non-negative variables. The negative part always appears
3200 * immediately before the positive part.
3201 * Other than that, the variables have the following order
3203 * - sum of (1 - e_i) over all edges
3204 * - sum of positive and negative parts of all c_n coefficients
3205 * (unconstrained when computing non-parametric schedules)
3206 * - sum of positive and negative parts of all c_x coefficients
3207 * - for each edge
3208 * - e_i
3209 * - for each node
3210 * - c_i_0
3211 * - positive and negative parts of c_i_n (if parametric)
3212 * - positive and negative parts of c_i_x
3214 * The constraints are those from the (validity) edges plus three equalities
3215 * to express the sums and n_edge inequalities to express e_i <= 1.
3217 static int setup_carry_lp(isl_ctx *ctx, struct isl_sched_graph *graph)
3219 int i, j;
3220 int k;
3221 isl_space *dim;
3222 unsigned total;
3223 int n_eq, n_ineq;
3224 int n_edge;
3226 n_edge = 0;
3227 for (i = 0; i < graph->n_edge; ++i)
3228 n_edge += graph->edge[i].map->n;
3230 total = 3 + n_edge;
3231 for (i = 0; i < graph->n; ++i) {
3232 struct isl_sched_node *node = &graph->node[graph->sorted[i]];
3233 node->start = total;
3234 total += 1 + 2 * (node->nparam + node->nvar);
3237 if (count_all_constraints(graph, &n_eq, &n_ineq) < 0)
3238 return -1;
3239 if (count_bound_coefficient_constraints(ctx, graph, &n_eq, &n_ineq) < 0)
3240 return -1;
3242 dim = isl_space_set_alloc(ctx, 0, total);
3243 isl_basic_set_free(graph->lp);
3244 n_eq += 3;
3245 n_ineq += n_edge;
3246 graph->lp = isl_basic_set_alloc_space(dim, 0, n_eq, n_ineq);
3247 graph->lp = isl_basic_set_set_rational(graph->lp);
3249 k = isl_basic_set_alloc_equality(graph->lp);
3250 if (k < 0)
3251 return -1;
3252 isl_seq_clr(graph->lp->eq[k], 1 + total);
3253 isl_int_set_si(graph->lp->eq[k][0], -n_edge);
3254 isl_int_set_si(graph->lp->eq[k][1], 1);
3255 for (i = 0; i < n_edge; ++i)
3256 isl_int_set_si(graph->lp->eq[k][4 + i], 1);
3258 k = isl_basic_set_alloc_equality(graph->lp);
3259 if (k < 0)
3260 return -1;
3261 isl_seq_clr(graph->lp->eq[k], 1 + total);
3262 isl_int_set_si(graph->lp->eq[k][2], -1);
3263 for (i = 0; i < graph->n; ++i) {
3264 int pos = 1 + graph->node[i].start + 1;
3266 for (j = 0; j < 2 * graph->node[i].nparam; ++j)
3267 isl_int_set_si(graph->lp->eq[k][pos + j], 1);
3270 k = isl_basic_set_alloc_equality(graph->lp);
3271 if (k < 0)
3272 return -1;
3273 isl_seq_clr(graph->lp->eq[k], 1 + total);
3274 isl_int_set_si(graph->lp->eq[k][3], -1);
3275 for (i = 0; i < graph->n; ++i) {
3276 struct isl_sched_node *node = &graph->node[i];
3277 int pos = 1 + node->start + 1 + 2 * node->nparam;
3279 for (j = 0; j < 2 * node->nvar; ++j)
3280 isl_int_set_si(graph->lp->eq[k][pos + j], 1);
3283 for (i = 0; i < n_edge; ++i) {
3284 k = isl_basic_set_alloc_inequality(graph->lp);
3285 if (k < 0)
3286 return -1;
3287 isl_seq_clr(graph->lp->ineq[k], 1 + total);
3288 isl_int_set_si(graph->lp->ineq[k][4 + i], -1);
3289 isl_int_set_si(graph->lp->ineq[k][0], 1);
3292 if (add_bound_coefficient_constraints(ctx, graph) < 0)
3293 return -1;
3294 if (add_all_constraints(graph) < 0)
3295 return -1;
3297 return 0;
3300 static __isl_give isl_schedule_node *compute_component_schedule(
3301 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph,
3302 int wcc);
3304 /* Comparison function for sorting the statements based on
3305 * the corresponding value in "r".
3307 static int smaller_value(const void *a, const void *b, void *data)
3309 isl_vec *r = data;
3310 const int *i1 = a;
3311 const int *i2 = b;
3313 return isl_int_cmp(r->el[*i1], r->el[*i2]);
3316 /* If the schedule_split_scaled option is set and if the linear
3317 * parts of the scheduling rows for all nodes in the graphs have
3318 * a non-trivial common divisor, then split off the remainder of the
3319 * constant term modulo this common divisor from the linear part.
3320 * Otherwise, insert a band node directly and continue with
3321 * the construction of the schedule.
3323 * If a non-trivial common divisor is found, then
3324 * the linear part is reduced and the remainder is enforced
3325 * by a sequence node with the children placed in the order
3326 * of this remainder.
3327 * In particular, we assign an scc index based on the remainder and
3328 * then rely on compute_component_schedule to insert the sequence and
3329 * to continue the schedule construction on each part.
3331 static __isl_give isl_schedule_node *split_scaled(
3332 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph)
3334 int i;
3335 int row;
3336 int scc;
3337 isl_ctx *ctx;
3338 isl_int gcd, gcd_i;
3339 isl_vec *r;
3340 int *order;
3342 if (!node)
3343 return NULL;
3345 ctx = isl_schedule_node_get_ctx(node);
3346 if (!ctx->opt->schedule_split_scaled)
3347 return compute_next_band(node, graph, 0);
3348 if (graph->n <= 1)
3349 return compute_next_band(node, graph, 0);
3351 isl_int_init(gcd);
3352 isl_int_init(gcd_i);
3354 isl_int_set_si(gcd, 0);
3356 row = isl_mat_rows(graph->node[0].sched) - 1;
3358 for (i = 0; i < graph->n; ++i) {
3359 struct isl_sched_node *node = &graph->node[i];
3360 int cols = isl_mat_cols(node->sched);
3362 isl_seq_gcd(node->sched->row[row] + 1, cols - 1, &gcd_i);
3363 isl_int_gcd(gcd, gcd, gcd_i);
3366 isl_int_clear(gcd_i);
3368 if (isl_int_cmp_si(gcd, 1) <= 0) {
3369 isl_int_clear(gcd);
3370 return compute_next_band(node, graph, 0);
3373 r = isl_vec_alloc(ctx, graph->n);
3374 order = isl_calloc_array(ctx, int, graph->n);
3375 if (!r || !order)
3376 goto error;
3378 for (i = 0; i < graph->n; ++i) {
3379 struct isl_sched_node *node = &graph->node[i];
3381 order[i] = i;
3382 isl_int_fdiv_r(r->el[i], node->sched->row[row][0], gcd);
3383 isl_int_fdiv_q(node->sched->row[row][0],
3384 node->sched->row[row][0], gcd);
3385 isl_int_mul(node->sched->row[row][0],
3386 node->sched->row[row][0], gcd);
3387 node->sched = isl_mat_scale_down_row(node->sched, row, gcd);
3388 if (!node->sched)
3389 goto error;
3392 if (isl_sort(order, graph->n, sizeof(order[0]), &smaller_value, r) < 0)
3393 goto error;
3395 scc = 0;
3396 for (i = 0; i < graph->n; ++i) {
3397 if (i > 0 && isl_int_ne(r->el[order[i - 1]], r->el[order[i]]))
3398 ++scc;
3399 graph->node[order[i]].scc = scc;
3401 graph->scc = ++scc;
3402 graph->weak = 0;
3404 isl_int_clear(gcd);
3405 isl_vec_free(r);
3406 free(order);
3408 if (update_edges(ctx, graph) < 0)
3409 return isl_schedule_node_free(node);
3410 node = insert_current_band(node, graph, 0);
3411 next_band(graph);
3413 node = isl_schedule_node_child(node, 0);
3414 node = compute_component_schedule(node, graph, 0);
3415 node = isl_schedule_node_parent(node);
3417 return node;
3418 error:
3419 isl_vec_free(r);
3420 free(order);
3421 isl_int_clear(gcd);
3422 return isl_schedule_node_free(node);
3425 /* Is the schedule row "sol" trivial on node "node"?
3426 * That is, is the solution zero on the dimensions orthogonal to
3427 * the previously found solutions?
3428 * Return 1 if the solution is trivial, 0 if it is not and -1 on error.
3430 * Each coefficient is represented as the difference between
3431 * two non-negative values in "sol". "sol" has been computed
3432 * in terms of the original iterators (i.e., without use of cmap).
3433 * We construct the schedule row s and write it as a linear
3434 * combination of (linear combinations of) previously computed schedule rows.
3435 * s = Q c or c = U s.
3436 * If the final entries of c are all zero, then the solution is trivial.
3438 static int is_trivial(struct isl_sched_node *node, __isl_keep isl_vec *sol)
3440 int i;
3441 int pos;
3442 int trivial;
3443 isl_ctx *ctx;
3444 isl_vec *node_sol;
3446 if (!sol)
3447 return -1;
3448 if (node->nvar == node->rank)
3449 return 0;
3451 ctx = isl_vec_get_ctx(sol);
3452 node_sol = isl_vec_alloc(ctx, node->nvar);
3453 if (!node_sol)
3454 return -1;
3456 pos = 1 + node->start + 1 + 2 * node->nparam;
3458 for (i = 0; i < node->nvar; ++i)
3459 isl_int_sub(node_sol->el[i],
3460 sol->el[pos + 2 * i + 1], sol->el[pos + 2 * i]);
3462 node_sol = isl_mat_vec_product(isl_mat_copy(node->cinv), node_sol);
3464 if (!node_sol)
3465 return -1;
3467 trivial = isl_seq_first_non_zero(node_sol->el + node->rank,
3468 node->nvar - node->rank) == -1;
3470 isl_vec_free(node_sol);
3472 return trivial;
3475 /* Is the schedule row "sol" trivial on any node where it should
3476 * not be trivial?
3477 * "sol" has been computed in terms of the original iterators
3478 * (i.e., without use of cmap).
3479 * Return 1 if any solution is trivial, 0 if they are not and -1 on error.
3481 static int is_any_trivial(struct isl_sched_graph *graph,
3482 __isl_keep isl_vec *sol)
3484 int i;
3486 for (i = 0; i < graph->n; ++i) {
3487 struct isl_sched_node *node = &graph->node[i];
3488 int trivial;
3490 if (!needs_row(graph, node))
3491 continue;
3492 trivial = is_trivial(node, sol);
3493 if (trivial < 0 || trivial)
3494 return trivial;
3497 return 0;
3500 /* Construct a schedule row for each node such that as many dependences
3501 * as possible are carried and then continue with the next band.
3503 * If the computed schedule row turns out to be trivial on one or
3504 * more nodes where it should not be trivial, then we throw it away
3505 * and try again on each component separately.
3507 * If there is only one component, then we accept the schedule row anyway,
3508 * but we do not consider it as a complete row and therefore do not
3509 * increment graph->n_row. Note that the ranks of the nodes that
3510 * do get a non-trivial schedule part will get updated regardless and
3511 * graph->maxvar is computed based on these ranks. The test for
3512 * whether more schedule rows are required in compute_schedule_wcc
3513 * is therefore not affected.
3515 * Insert a band corresponding to the schedule row at position "node"
3516 * of the schedule tree and continue with the construction of the schedule.
3517 * This insertion and the continued construction is performed by split_scaled
3518 * after optionally checking for non-trivial common divisors.
3520 static __isl_give isl_schedule_node *carry_dependences(
3521 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph)
3523 int i;
3524 int n_edge;
3525 int trivial;
3526 isl_ctx *ctx;
3527 isl_vec *sol;
3528 isl_basic_set *lp;
3530 if (!node)
3531 return NULL;
3533 n_edge = 0;
3534 for (i = 0; i < graph->n_edge; ++i)
3535 n_edge += graph->edge[i].map->n;
3537 ctx = isl_schedule_node_get_ctx(node);
3538 if (setup_carry_lp(ctx, graph) < 0)
3539 return isl_schedule_node_free(node);
3541 lp = isl_basic_set_copy(graph->lp);
3542 sol = isl_tab_basic_set_non_neg_lexmin(lp);
3543 if (!sol)
3544 return isl_schedule_node_free(node);
3546 if (sol->size == 0) {
3547 isl_vec_free(sol);
3548 isl_die(ctx, isl_error_internal,
3549 "error in schedule construction",
3550 return isl_schedule_node_free(node));
3553 isl_int_divexact(sol->el[1], sol->el[1], sol->el[0]);
3554 if (isl_int_cmp_si(sol->el[1], n_edge) >= 0) {
3555 isl_vec_free(sol);
3556 isl_die(ctx, isl_error_unknown,
3557 "unable to carry dependences",
3558 return isl_schedule_node_free(node));
3561 trivial = is_any_trivial(graph, sol);
3562 if (trivial < 0) {
3563 sol = isl_vec_free(sol);
3564 } else if (trivial && graph->scc > 1) {
3565 isl_vec_free(sol);
3566 return compute_component_schedule(node, graph, 1);
3569 if (update_schedule(graph, sol, 0, 0) < 0)
3570 return isl_schedule_node_free(node);
3571 if (trivial)
3572 graph->n_row--;
3574 return split_scaled(node, graph);
3577 /* Are there any (non-empty) (conditional) validity edges in the graph?
3579 static int has_validity_edges(struct isl_sched_graph *graph)
3581 int i;
3583 for (i = 0; i < graph->n_edge; ++i) {
3584 int empty;
3586 empty = isl_map_plain_is_empty(graph->edge[i].map);
3587 if (empty < 0)
3588 return -1;
3589 if (empty)
3590 continue;
3591 if (graph->edge[i].validity ||
3592 graph->edge[i].conditional_validity)
3593 return 1;
3596 return 0;
3599 /* Should we apply a Feautrier step?
3600 * That is, did the user request the Feautrier algorithm and are
3601 * there any validity dependences (left)?
3603 static int need_feautrier_step(isl_ctx *ctx, struct isl_sched_graph *graph)
3605 if (ctx->opt->schedule_algorithm != ISL_SCHEDULE_ALGORITHM_FEAUTRIER)
3606 return 0;
3608 return has_validity_edges(graph);
3611 /* Compute a schedule for a connected dependence graph using Feautrier's
3612 * multi-dimensional scheduling algorithm and return the updated schedule node.
3614 * The original algorithm is described in [1].
3615 * The main idea is to minimize the number of scheduling dimensions, by
3616 * trying to satisfy as many dependences as possible per scheduling dimension.
3618 * [1] P. Feautrier, Some Efficient Solutions to the Affine Scheduling
3619 * Problem, Part II: Multi-Dimensional Time.
3620 * In Intl. Journal of Parallel Programming, 1992.
3622 static __isl_give isl_schedule_node *compute_schedule_wcc_feautrier(
3623 isl_schedule_node *node, struct isl_sched_graph *graph)
3625 return carry_dependences(node, graph);
3628 /* Turn off the "local" bit on all (condition) edges.
3630 static void clear_local_edges(struct isl_sched_graph *graph)
3632 int i;
3634 for (i = 0; i < graph->n_edge; ++i)
3635 if (graph->edge[i].condition)
3636 graph->edge[i].local = 0;
3639 /* Does "graph" have both condition and conditional validity edges?
3641 static int need_condition_check(struct isl_sched_graph *graph)
3643 int i;
3644 int any_condition = 0;
3645 int any_conditional_validity = 0;
3647 for (i = 0; i < graph->n_edge; ++i) {
3648 if (graph->edge[i].condition)
3649 any_condition = 1;
3650 if (graph->edge[i].conditional_validity)
3651 any_conditional_validity = 1;
3654 return any_condition && any_conditional_validity;
3657 /* Does "graph" contain any coincidence edge?
3659 static int has_any_coincidence(struct isl_sched_graph *graph)
3661 int i;
3663 for (i = 0; i < graph->n_edge; ++i)
3664 if (graph->edge[i].coincidence)
3665 return 1;
3667 return 0;
3670 /* Extract the final schedule row as a map with the iteration domain
3671 * of "node" as domain.
3673 static __isl_give isl_map *final_row(struct isl_sched_node *node)
3675 isl_local_space *ls;
3676 isl_aff *aff;
3677 int row;
3679 row = isl_mat_rows(node->sched) - 1;
3680 ls = isl_local_space_from_space(isl_space_copy(node->space));
3681 aff = extract_schedule_row(ls, node, row);
3682 return isl_map_from_aff(aff);
3685 /* Is the conditional validity dependence in the edge with index "edge_index"
3686 * violated by the latest (i.e., final) row of the schedule?
3687 * That is, is i scheduled after j
3688 * for any conditional validity dependence i -> j?
3690 static int is_violated(struct isl_sched_graph *graph, int edge_index)
3692 isl_map *src_sched, *dst_sched, *map;
3693 struct isl_sched_edge *edge = &graph->edge[edge_index];
3694 int empty;
3696 src_sched = final_row(edge->src);
3697 dst_sched = final_row(edge->dst);
3698 map = isl_map_copy(edge->map);
3699 map = isl_map_apply_domain(map, src_sched);
3700 map = isl_map_apply_range(map, dst_sched);
3701 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_out, 0);
3702 empty = isl_map_is_empty(map);
3703 isl_map_free(map);
3705 if (empty < 0)
3706 return -1;
3708 return !empty;
3711 /* Does the domain of "umap" intersect "uset"?
3713 static int domain_intersects(__isl_keep isl_union_map *umap,
3714 __isl_keep isl_union_set *uset)
3716 int empty;
3718 umap = isl_union_map_copy(umap);
3719 umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(uset));
3720 empty = isl_union_map_is_empty(umap);
3721 isl_union_map_free(umap);
3723 return empty < 0 ? -1 : !empty;
3726 /* Does the range of "umap" intersect "uset"?
3728 static int range_intersects(__isl_keep isl_union_map *umap,
3729 __isl_keep isl_union_set *uset)
3731 int empty;
3733 umap = isl_union_map_copy(umap);
3734 umap = isl_union_map_intersect_range(umap, isl_union_set_copy(uset));
3735 empty = isl_union_map_is_empty(umap);
3736 isl_union_map_free(umap);
3738 return empty < 0 ? -1 : !empty;
3741 /* Are the condition dependences of "edge" local with respect to
3742 * the current schedule?
3744 * That is, are domain and range of the condition dependences mapped
3745 * to the same point?
3747 * In other words, is the condition false?
3749 static int is_condition_false(struct isl_sched_edge *edge)
3751 isl_union_map *umap;
3752 isl_map *map, *sched, *test;
3753 int local;
3755 umap = isl_union_map_copy(edge->tagged_condition);
3756 umap = isl_union_map_zip(umap);
3757 umap = isl_union_set_unwrap(isl_union_map_domain(umap));
3758 map = isl_map_from_union_map(umap);
3760 sched = node_extract_schedule(edge->src);
3761 map = isl_map_apply_domain(map, sched);
3762 sched = node_extract_schedule(edge->dst);
3763 map = isl_map_apply_range(map, sched);
3765 test = isl_map_identity(isl_map_get_space(map));
3766 local = isl_map_is_subset(map, test);
3767 isl_map_free(map);
3768 isl_map_free(test);
3770 return local;
3773 /* Does "graph" have any satisfied condition edges that
3774 * are adjacent to the conditional validity constraint with
3775 * domain "conditional_source" and range "conditional_sink"?
3777 * A satisfied condition is one that is not local.
3778 * If a condition was forced to be local already (i.e., marked as local)
3779 * then there is no need to check if it is in fact local.
3781 * Additionally, mark all adjacent condition edges found as local.
3783 static int has_adjacent_true_conditions(struct isl_sched_graph *graph,
3784 __isl_keep isl_union_set *conditional_source,
3785 __isl_keep isl_union_set *conditional_sink)
3787 int i;
3788 int any = 0;
3790 for (i = 0; i < graph->n_edge; ++i) {
3791 int adjacent, local;
3792 isl_union_map *condition;
3794 if (!graph->edge[i].condition)
3795 continue;
3796 if (graph->edge[i].local)
3797 continue;
3799 condition = graph->edge[i].tagged_condition;
3800 adjacent = domain_intersects(condition, conditional_sink);
3801 if (adjacent >= 0 && !adjacent)
3802 adjacent = range_intersects(condition,
3803 conditional_source);
3804 if (adjacent < 0)
3805 return -1;
3806 if (!adjacent)
3807 continue;
3809 graph->edge[i].local = 1;
3811 local = is_condition_false(&graph->edge[i]);
3812 if (local < 0)
3813 return -1;
3814 if (!local)
3815 any = 1;
3818 return any;
3821 /* Are there any violated conditional validity dependences with
3822 * adjacent condition dependences that are not local with respect
3823 * to the current schedule?
3824 * That is, is the conditional validity constraint violated?
3826 * Additionally, mark all those adjacent condition dependences as local.
3827 * We also mark those adjacent condition dependences that were not marked
3828 * as local before, but just happened to be local already. This ensures
3829 * that they remain local if the schedule is recomputed.
3831 * We first collect domain and range of all violated conditional validity
3832 * dependences and then check if there are any adjacent non-local
3833 * condition dependences.
3835 static int has_violated_conditional_constraint(isl_ctx *ctx,
3836 struct isl_sched_graph *graph)
3838 int i;
3839 int any = 0;
3840 isl_union_set *source, *sink;
3842 source = isl_union_set_empty(isl_space_params_alloc(ctx, 0));
3843 sink = isl_union_set_empty(isl_space_params_alloc(ctx, 0));
3844 for (i = 0; i < graph->n_edge; ++i) {
3845 isl_union_set *uset;
3846 isl_union_map *umap;
3847 int violated;
3849 if (!graph->edge[i].conditional_validity)
3850 continue;
3852 violated = is_violated(graph, i);
3853 if (violated < 0)
3854 goto error;
3855 if (!violated)
3856 continue;
3858 any = 1;
3860 umap = isl_union_map_copy(graph->edge[i].tagged_validity);
3861 uset = isl_union_map_domain(umap);
3862 source = isl_union_set_union(source, uset);
3863 source = isl_union_set_coalesce(source);
3865 umap = isl_union_map_copy(graph->edge[i].tagged_validity);
3866 uset = isl_union_map_range(umap);
3867 sink = isl_union_set_union(sink, uset);
3868 sink = isl_union_set_coalesce(sink);
3871 if (any)
3872 any = has_adjacent_true_conditions(graph, source, sink);
3874 isl_union_set_free(source);
3875 isl_union_set_free(sink);
3876 return any;
3877 error:
3878 isl_union_set_free(source);
3879 isl_union_set_free(sink);
3880 return -1;
3883 /* Compute a schedule for a connected dependence graph and return
3884 * the updated schedule node.
3886 * We try to find a sequence of as many schedule rows as possible that result
3887 * in non-negative dependence distances (independent of the previous rows
3888 * in the sequence, i.e., such that the sequence is tilable), with as
3889 * many of the initial rows as possible satisfying the coincidence constraints.
3890 * If we can't find any more rows we either
3891 * - split between SCCs and start over (assuming we found an interesting
3892 * pair of SCCs between which to split)
3893 * - continue with the next band (assuming the current band has at least
3894 * one row)
3895 * - try to carry as many dependences as possible and continue with the next
3896 * band
3897 * In each case, we first insert a band node in the schedule tree
3898 * if any rows have been computed.
3900 * If Feautrier's algorithm is selected, we first recursively try to satisfy
3901 * as many validity dependences as possible. When all validity dependences
3902 * are satisfied we extend the schedule to a full-dimensional schedule.
3904 * If we manage to complete the schedule, we insert a band node
3905 * (if any schedule rows were computed) and we finish off by topologically
3906 * sorting the statements based on the remaining dependences.
3908 * If ctx->opt->schedule_outer_coincidence is set, then we force the
3909 * outermost dimension to satisfy the coincidence constraints. If this
3910 * turns out to be impossible, we fall back on the general scheme above
3911 * and try to carry as many dependences as possible.
3913 * If "graph" contains both condition and conditional validity dependences,
3914 * then we need to check that that the conditional schedule constraint
3915 * is satisfied, i.e., there are no violated conditional validity dependences
3916 * that are adjacent to any non-local condition dependences.
3917 * If there are, then we mark all those adjacent condition dependences
3918 * as local and recompute the current band. Those dependences that
3919 * are marked local will then be forced to be local.
3920 * The initial computation is performed with no dependences marked as local.
3921 * If we are lucky, then there will be no violated conditional validity
3922 * dependences adjacent to any non-local condition dependences.
3923 * Otherwise, we mark some additional condition dependences as local and
3924 * recompute. We continue this process until there are no violations left or
3925 * until we are no longer able to compute a schedule.
3926 * Since there are only a finite number of dependences,
3927 * there will only be a finite number of iterations.
3929 static __isl_give isl_schedule_node *compute_schedule_wcc(
3930 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph)
3932 int has_coincidence;
3933 int use_coincidence;
3934 int force_coincidence = 0;
3935 int check_conditional;
3936 isl_ctx *ctx;
3938 if (!node)
3939 return NULL;
3941 ctx = isl_schedule_node_get_ctx(node);
3942 if (detect_sccs(ctx, graph) < 0)
3943 return isl_schedule_node_free(node);
3944 if (sort_sccs(graph) < 0)
3945 return isl_schedule_node_free(node);
3947 if (compute_maxvar(graph) < 0)
3948 return isl_schedule_node_free(node);
3950 if (need_feautrier_step(ctx, graph))
3951 return compute_schedule_wcc_feautrier(node, graph);
3953 clear_local_edges(graph);
3954 check_conditional = need_condition_check(graph);
3955 has_coincidence = has_any_coincidence(graph);
3957 if (ctx->opt->schedule_outer_coincidence)
3958 force_coincidence = 1;
3960 use_coincidence = has_coincidence;
3961 while (graph->n_row < graph->maxvar) {
3962 isl_vec *sol;
3963 int violated;
3964 int coincident;
3966 graph->src_scc = -1;
3967 graph->dst_scc = -1;
3969 if (setup_lp(ctx, graph, use_coincidence) < 0)
3970 return isl_schedule_node_free(node);
3971 sol = solve_lp(graph);
3972 if (!sol)
3973 return isl_schedule_node_free(node);
3974 if (sol->size == 0) {
3975 int empty = graph->n_total_row == graph->band_start;
3977 isl_vec_free(sol);
3978 if (use_coincidence && (!force_coincidence || !empty)) {
3979 use_coincidence = 0;
3980 continue;
3982 if (!ctx->opt->schedule_maximize_band_depth && !empty)
3983 return compute_next_band(node, graph, 1);
3984 if (graph->src_scc >= 0)
3985 return compute_split_schedule(node, graph);
3986 if (!empty)
3987 return compute_next_band(node, graph, 1);
3988 return carry_dependences(node, graph);
3990 coincident = !has_coincidence || use_coincidence;
3991 if (update_schedule(graph, sol, 1, coincident) < 0)
3992 return isl_schedule_node_free(node);
3994 if (!check_conditional)
3995 continue;
3996 violated = has_violated_conditional_constraint(ctx, graph);
3997 if (violated < 0)
3998 return isl_schedule_node_free(node);
3999 if (!violated)
4000 continue;
4001 if (reset_band(graph) < 0)
4002 return isl_schedule_node_free(node);
4003 use_coincidence = has_coincidence;
4006 if (graph->n_total_row > graph->band_start) {
4007 node = insert_current_band(node, graph, 1);
4008 node = isl_schedule_node_child(node, 0);
4010 node = sort_statements(node, graph);
4011 if (graph->n_total_row > graph->band_start)
4012 node = isl_schedule_node_parent(node);
4014 return node;
4017 /* Compute a schedule for each group of nodes identified by node->scc
4018 * separately and then combine them in a sequence node (or as set node
4019 * if graph->weak is set) inserted at position "node" of the schedule tree.
4020 * Return the updated schedule node.
4022 * If "wcc" is set then each of the groups belongs to a single
4023 * weakly connected component in the dependence graph so that
4024 * there is no need for compute_sub_schedule to look for weakly
4025 * connected components.
4027 static __isl_give isl_schedule_node *compute_component_schedule(
4028 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph,
4029 int wcc)
4031 int component, i;
4032 int n, n_edge;
4033 int orig_total_row;
4034 isl_ctx *ctx;
4035 isl_union_set_list *filters;
4037 if (!node)
4038 return NULL;
4039 ctx = isl_schedule_node_get_ctx(node);
4041 filters = extract_sccs(ctx, graph);
4042 if (graph->weak)
4043 node = isl_schedule_node_insert_set(node, filters);
4044 else
4045 node = isl_schedule_node_insert_sequence(node, filters);
4047 orig_total_row = graph->n_total_row;
4048 for (component = 0; component < graph->scc; ++component) {
4049 n = 0;
4050 for (i = 0; i < graph->n; ++i)
4051 if (graph->node[i].scc == component)
4052 n++;
4053 n_edge = 0;
4054 for (i = 0; i < graph->n_edge; ++i)
4055 if (graph->edge[i].src->scc == component &&
4056 graph->edge[i].dst->scc == component)
4057 n_edge++;
4059 node = isl_schedule_node_child(node, component);
4060 node = isl_schedule_node_child(node, 0);
4061 node = compute_sub_schedule(node, ctx, graph, n, n_edge,
4062 &node_scc_exactly,
4063 &edge_scc_exactly, component, wcc);
4064 node = isl_schedule_node_parent(node);
4065 node = isl_schedule_node_parent(node);
4066 graph->n_total_row = orig_total_row;
4069 return node;
4072 /* Compute a schedule for the given dependence graph and insert it at "node".
4073 * Return the updated schedule node.
4075 * We first check if the graph is connected (through validity and conditional
4076 * validity dependences) and, if not, compute a schedule
4077 * for each component separately.
4078 * If schedule_fuse is set to minimal fusion, then we check for strongly
4079 * connected components instead and compute a separate schedule for
4080 * each such strongly connected component.
4082 static __isl_give isl_schedule_node *compute_schedule(isl_schedule_node *node,
4083 struct isl_sched_graph *graph)
4085 isl_ctx *ctx;
4087 if (!node)
4088 return NULL;
4090 ctx = isl_schedule_node_get_ctx(node);
4091 if (ctx->opt->schedule_fuse == ISL_SCHEDULE_FUSE_MIN) {
4092 if (detect_sccs(ctx, graph) < 0)
4093 return isl_schedule_node_free(node);
4094 } else {
4095 if (detect_wccs(ctx, graph) < 0)
4096 return isl_schedule_node_free(node);
4099 if (graph->scc > 1)
4100 return compute_component_schedule(node, graph, 1);
4102 return compute_schedule_wcc(node, graph);
4105 /* Compute a schedule on sc->domain that respects the given schedule
4106 * constraints.
4108 * In particular, the schedule respects all the validity dependences.
4109 * If the default isl scheduling algorithm is used, it tries to minimize
4110 * the dependence distances over the proximity dependences.
4111 * If Feautrier's scheduling algorithm is used, the proximity dependence
4112 * distances are only minimized during the extension to a full-dimensional
4113 * schedule.
4115 * If there are any condition and conditional validity dependences,
4116 * then the conditional validity dependences may be violated inside
4117 * a tilable band, provided they have no adjacent non-local
4118 * condition dependences.
4120 * The context is included in the domain before the nodes of
4121 * the graphs are extracted in order to be able to exploit
4122 * any possible additional equalities.
4123 * However, the returned schedule contains the original domain
4124 * (before this intersection).
4126 __isl_give isl_schedule *isl_schedule_constraints_compute_schedule(
4127 __isl_take isl_schedule_constraints *sc)
4129 isl_ctx *ctx = isl_schedule_constraints_get_ctx(sc);
4130 struct isl_sched_graph graph = { 0 };
4131 isl_schedule *sched;
4132 isl_schedule_node *node;
4133 isl_union_set *domain;
4134 struct isl_extract_edge_data data;
4135 enum isl_edge_type i;
4136 int r;
4138 sc = isl_schedule_constraints_align_params(sc);
4139 if (!sc)
4140 return NULL;
4142 graph.n = isl_union_set_n_set(sc->domain);
4143 if (graph.n == 0) {
4144 isl_union_set *domain = isl_union_set_copy(sc->domain);
4145 sched = isl_schedule_from_domain(domain);
4146 goto done;
4148 if (graph_alloc(ctx, &graph, graph.n,
4149 isl_schedule_constraints_n_map(sc)) < 0)
4150 goto error;
4151 if (compute_max_row(&graph, sc) < 0)
4152 goto error;
4153 graph.root = 1;
4154 graph.n = 0;
4155 domain = isl_union_set_copy(sc->domain);
4156 domain = isl_union_set_intersect_params(domain,
4157 isl_set_copy(sc->context));
4158 r = isl_union_set_foreach_set(domain, &extract_node, &graph);
4159 isl_union_set_free(domain);
4160 if (r < 0)
4161 goto error;
4162 if (graph_init_table(ctx, &graph) < 0)
4163 goto error;
4164 for (i = isl_edge_first; i <= isl_edge_last; ++i)
4165 graph.max_edge[i] = isl_union_map_n_map(sc->constraint[i]);
4166 if (graph_init_edge_tables(ctx, &graph) < 0)
4167 goto error;
4168 graph.n_edge = 0;
4169 data.graph = &graph;
4170 for (i = isl_edge_first; i <= isl_edge_last; ++i) {
4171 data.type = i;
4172 if (isl_union_map_foreach_map(sc->constraint[i],
4173 &extract_edge, &data) < 0)
4174 goto error;
4177 node = isl_schedule_node_from_domain(isl_union_set_copy(sc->domain));
4178 node = isl_schedule_node_child(node, 0);
4179 node = compute_schedule(node, &graph);
4180 sched = isl_schedule_node_get_schedule(node);
4181 isl_schedule_node_free(node);
4183 done:
4184 graph_free(ctx, &graph);
4185 isl_schedule_constraints_free(sc);
4187 return sched;
4188 error:
4189 graph_free(ctx, &graph);
4190 isl_schedule_constraints_free(sc);
4191 return NULL;
4194 /* Compute a schedule for the given union of domains that respects
4195 * all the validity dependences and minimizes
4196 * the dependence distances over the proximity dependences.
4198 * This function is kept for backward compatibility.
4200 __isl_give isl_schedule *isl_union_set_compute_schedule(
4201 __isl_take isl_union_set *domain,
4202 __isl_take isl_union_map *validity,
4203 __isl_take isl_union_map *proximity)
4205 isl_schedule_constraints *sc;
4207 sc = isl_schedule_constraints_on_domain(domain);
4208 sc = isl_schedule_constraints_set_validity(sc, validity);
4209 sc = isl_schedule_constraints_set_proximity(sc, proximity);
4211 return isl_schedule_constraints_compute_schedule(sc);