explicitly mark isl_basic_set_add as deprecated
[isl.git] / isl_scheduler.c
blob856c9851fcd844279f27aa585ba7dc002b75d70c
1 /*
2 * Copyright 2011 INRIA Saclay
3 * Copyright 2012-2014 Ecole Normale Superieure
4 * Copyright 2015 Sven Verdoolaege
6 * Use of this software is governed by the MIT license
8 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
9 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
10 * 91893 Orsay, France
11 * and Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
14 #include <isl_ctx_private.h>
15 #include <isl_map_private.h>
16 #include <isl_space_private.h>
17 #include <isl_aff_private.h>
18 #include <isl/hash.h>
19 #include <isl/constraint.h>
20 #include <isl/schedule.h>
21 #include <isl/schedule_node.h>
22 #include <isl_mat_private.h>
23 #include <isl_vec_private.h>
24 #include <isl/set.h>
25 #include <isl/union_set.h>
26 #include <isl_seq.h>
27 #include <isl_tab.h>
28 #include <isl_dim_map.h>
29 #include <isl/map_to_basic_set.h>
30 #include <isl_sort.h>
31 #include <isl_options_private.h>
32 #include <isl_tarjan.h>
33 #include <isl_morph.h>
36 * The scheduling algorithm implemented in this file was inspired by
37 * Bondhugula et al., "Automatic Transformations for Communication-Minimized
38 * Parallelization and Locality Optimization in the Polyhedral Model".
41 enum isl_edge_type {
42 isl_edge_validity = 0,
43 isl_edge_first = isl_edge_validity,
44 isl_edge_coincidence,
45 isl_edge_condition,
46 isl_edge_conditional_validity,
47 isl_edge_proximity,
48 isl_edge_last = isl_edge_proximity
51 /* The constraints that need to be satisfied by a schedule on "domain".
53 * "context" specifies extra constraints on the parameters.
55 * "validity" constraints map domain elements i to domain elements
56 * that should be scheduled after i. (Hard constraint)
57 * "proximity" constraints map domain elements i to domains elements
58 * that should be scheduled as early as possible after i (or before i).
59 * (Soft constraint)
61 * "condition" and "conditional_validity" constraints map possibly "tagged"
62 * domain elements i -> s to "tagged" domain elements j -> t.
63 * The elements of the "conditional_validity" constraints, but without the
64 * tags (i.e., the elements i -> j) are treated as validity constraints,
65 * except that during the construction of a tilable band,
66 * the elements of the "conditional_validity" constraints may be violated
67 * provided that all adjacent elements of the "condition" constraints
68 * are local within the band.
69 * A dependence is local within a band if domain and range are mapped
70 * to the same schedule point by the band.
72 struct isl_schedule_constraints {
73 isl_union_set *domain;
74 isl_set *context;
76 isl_union_map *constraint[isl_edge_last + 1];
79 __isl_give isl_schedule_constraints *isl_schedule_constraints_copy(
80 __isl_keep isl_schedule_constraints *sc)
82 isl_ctx *ctx;
83 isl_schedule_constraints *sc_copy;
84 enum isl_edge_type i;
86 ctx = isl_union_set_get_ctx(sc->domain);
87 sc_copy = isl_calloc_type(ctx, struct isl_schedule_constraints);
88 if (!sc_copy)
89 return NULL;
91 sc_copy->domain = isl_union_set_copy(sc->domain);
92 sc_copy->context = isl_set_copy(sc->context);
93 if (!sc_copy->domain || !sc_copy->context)
94 return isl_schedule_constraints_free(sc_copy);
96 for (i = isl_edge_first; i <= isl_edge_last; ++i) {
97 sc_copy->constraint[i] = isl_union_map_copy(sc->constraint[i]);
98 if (!sc_copy->constraint[i])
99 return isl_schedule_constraints_free(sc_copy);
102 return sc_copy;
106 /* Construct an isl_schedule_constraints object for computing a schedule
107 * on "domain". The initial object does not impose any constraints.
109 __isl_give isl_schedule_constraints *isl_schedule_constraints_on_domain(
110 __isl_take isl_union_set *domain)
112 isl_ctx *ctx;
113 isl_space *space;
114 isl_schedule_constraints *sc;
115 isl_union_map *empty;
116 enum isl_edge_type i;
118 if (!domain)
119 return NULL;
121 ctx = isl_union_set_get_ctx(domain);
122 sc = isl_calloc_type(ctx, struct isl_schedule_constraints);
123 if (!sc)
124 goto error;
126 space = isl_union_set_get_space(domain);
127 sc->domain = domain;
128 sc->context = isl_set_universe(isl_space_copy(space));
129 empty = isl_union_map_empty(space);
130 for (i = isl_edge_first; i <= isl_edge_last; ++i) {
131 sc->constraint[i] = isl_union_map_copy(empty);
132 if (!sc->constraint[i])
133 sc->domain = isl_union_set_free(sc->domain);
135 isl_union_map_free(empty);
137 if (!sc->domain || !sc->context)
138 return isl_schedule_constraints_free(sc);
140 return sc;
141 error:
142 isl_union_set_free(domain);
143 return NULL;
146 /* Replace the context of "sc" by "context".
148 __isl_give isl_schedule_constraints *isl_schedule_constraints_set_context(
149 __isl_take isl_schedule_constraints *sc, __isl_take isl_set *context)
151 if (!sc || !context)
152 goto error;
154 isl_set_free(sc->context);
155 sc->context = context;
157 return sc;
158 error:
159 isl_schedule_constraints_free(sc);
160 isl_set_free(context);
161 return NULL;
164 /* Replace the validity constraints of "sc" by "validity".
166 __isl_give isl_schedule_constraints *isl_schedule_constraints_set_validity(
167 __isl_take isl_schedule_constraints *sc,
168 __isl_take isl_union_map *validity)
170 if (!sc || !validity)
171 goto error;
173 isl_union_map_free(sc->constraint[isl_edge_validity]);
174 sc->constraint[isl_edge_validity] = validity;
176 return sc;
177 error:
178 isl_schedule_constraints_free(sc);
179 isl_union_map_free(validity);
180 return NULL;
183 /* Replace the coincidence constraints of "sc" by "coincidence".
185 __isl_give isl_schedule_constraints *isl_schedule_constraints_set_coincidence(
186 __isl_take isl_schedule_constraints *sc,
187 __isl_take isl_union_map *coincidence)
189 if (!sc || !coincidence)
190 goto error;
192 isl_union_map_free(sc->constraint[isl_edge_coincidence]);
193 sc->constraint[isl_edge_coincidence] = coincidence;
195 return sc;
196 error:
197 isl_schedule_constraints_free(sc);
198 isl_union_map_free(coincidence);
199 return NULL;
202 /* Replace the proximity constraints of "sc" by "proximity".
204 __isl_give isl_schedule_constraints *isl_schedule_constraints_set_proximity(
205 __isl_take isl_schedule_constraints *sc,
206 __isl_take isl_union_map *proximity)
208 if (!sc || !proximity)
209 goto error;
211 isl_union_map_free(sc->constraint[isl_edge_proximity]);
212 sc->constraint[isl_edge_proximity] = proximity;
214 return sc;
215 error:
216 isl_schedule_constraints_free(sc);
217 isl_union_map_free(proximity);
218 return NULL;
221 /* Replace the conditional validity constraints of "sc" by "condition"
222 * and "validity".
224 __isl_give isl_schedule_constraints *
225 isl_schedule_constraints_set_conditional_validity(
226 __isl_take isl_schedule_constraints *sc,
227 __isl_take isl_union_map *condition,
228 __isl_take isl_union_map *validity)
230 if (!sc || !condition || !validity)
231 goto error;
233 isl_union_map_free(sc->constraint[isl_edge_condition]);
234 sc->constraint[isl_edge_condition] = condition;
235 isl_union_map_free(sc->constraint[isl_edge_conditional_validity]);
236 sc->constraint[isl_edge_conditional_validity] = validity;
238 return sc;
239 error:
240 isl_schedule_constraints_free(sc);
241 isl_union_map_free(condition);
242 isl_union_map_free(validity);
243 return NULL;
246 __isl_null isl_schedule_constraints *isl_schedule_constraints_free(
247 __isl_take isl_schedule_constraints *sc)
249 enum isl_edge_type i;
251 if (!sc)
252 return NULL;
254 isl_union_set_free(sc->domain);
255 isl_set_free(sc->context);
256 for (i = isl_edge_first; i <= isl_edge_last; ++i)
257 isl_union_map_free(sc->constraint[i]);
259 free(sc);
261 return NULL;
264 isl_ctx *isl_schedule_constraints_get_ctx(
265 __isl_keep isl_schedule_constraints *sc)
267 return sc ? isl_union_set_get_ctx(sc->domain) : NULL;
270 /* Return the validity constraints of "sc".
272 __isl_give isl_union_map *isl_schedule_constraints_get_validity(
273 __isl_keep isl_schedule_constraints *sc)
275 if (!sc)
276 return NULL;
278 return isl_union_map_copy(sc->constraint[isl_edge_validity]);
281 /* Return the coincidence constraints of "sc".
283 __isl_give isl_union_map *isl_schedule_constraints_get_coincidence(
284 __isl_keep isl_schedule_constraints *sc)
286 if (!sc)
287 return NULL;
289 return isl_union_map_copy(sc->constraint[isl_edge_coincidence]);
292 /* Return the conditional validity constraints of "sc".
294 __isl_give isl_union_map *isl_schedule_constraints_get_conditional_validity(
295 __isl_keep isl_schedule_constraints *sc)
297 if (!sc)
298 return NULL;
300 return
301 isl_union_map_copy(sc->constraint[isl_edge_conditional_validity]);
304 /* Return the conditions for the conditional validity constraints of "sc".
306 __isl_give isl_union_map *
307 isl_schedule_constraints_get_conditional_validity_condition(
308 __isl_keep isl_schedule_constraints *sc)
310 if (!sc)
311 return NULL;
313 return isl_union_map_copy(sc->constraint[isl_edge_condition]);
316 void isl_schedule_constraints_dump(__isl_keep isl_schedule_constraints *sc)
318 if (!sc)
319 return;
321 fprintf(stderr, "domain: ");
322 isl_union_set_dump(sc->domain);
323 fprintf(stderr, "context: ");
324 isl_set_dump(sc->context);
325 fprintf(stderr, "validity: ");
326 isl_union_map_dump(sc->constraint[isl_edge_validity]);
327 fprintf(stderr, "proximity: ");
328 isl_union_map_dump(sc->constraint[isl_edge_proximity]);
329 fprintf(stderr, "coincidence: ");
330 isl_union_map_dump(sc->constraint[isl_edge_coincidence]);
331 fprintf(stderr, "condition: ");
332 isl_union_map_dump(sc->constraint[isl_edge_condition]);
333 fprintf(stderr, "conditional_validity: ");
334 isl_union_map_dump(sc->constraint[isl_edge_conditional_validity]);
337 /* Align the parameters of the fields of "sc".
339 static __isl_give isl_schedule_constraints *
340 isl_schedule_constraints_align_params(__isl_take isl_schedule_constraints *sc)
342 isl_space *space;
343 enum isl_edge_type i;
345 if (!sc)
346 return NULL;
348 space = isl_union_set_get_space(sc->domain);
349 space = isl_space_align_params(space, isl_set_get_space(sc->context));
350 for (i = isl_edge_first; i <= isl_edge_last; ++i)
351 space = isl_space_align_params(space,
352 isl_union_map_get_space(sc->constraint[i]));
354 for (i = isl_edge_first; i <= isl_edge_last; ++i) {
355 sc->constraint[i] = isl_union_map_align_params(
356 sc->constraint[i], isl_space_copy(space));
357 if (!sc->constraint[i])
358 space = isl_space_free(space);
360 sc->context = isl_set_align_params(sc->context, isl_space_copy(space));
361 sc->domain = isl_union_set_align_params(sc->domain, space);
362 if (!sc->context || !sc->domain)
363 return isl_schedule_constraints_free(sc);
365 return sc;
368 /* Return the total number of isl_maps in the constraints of "sc".
370 static __isl_give int isl_schedule_constraints_n_map(
371 __isl_keep isl_schedule_constraints *sc)
373 enum isl_edge_type i;
374 int n = 0;
376 for (i = isl_edge_first; i <= isl_edge_last; ++i)
377 n += isl_union_map_n_map(sc->constraint[i]);
379 return n;
382 /* Internal information about a node that is used during the construction
383 * of a schedule.
384 * space represents the space in which the domain lives
385 * sched is a matrix representation of the schedule being constructed
386 * for this node; if compressed is set, then this schedule is
387 * defined over the compressed domain space
388 * sched_map is an isl_map representation of the same (partial) schedule
389 * sched_map may be NULL; if compressed is set, then this map
390 * is defined over the uncompressed domain space
391 * rank is the number of linearly independent rows in the linear part
392 * of sched
393 * the columns of cmap represent a change of basis for the schedule
394 * coefficients; the first rank columns span the linear part of
395 * the schedule rows
396 * cinv is the inverse of cmap.
397 * start is the first variable in the LP problem in the sequences that
398 * represents the schedule coefficients of this node
399 * nvar is the dimension of the domain
400 * nparam is the number of parameters or 0 if we are not constructing
401 * a parametric schedule
403 * If compressed is set, then hull represents the constraints
404 * that were used to derive the compression, while compress and
405 * decompress map the original space to the compressed space and
406 * vice versa.
408 * scc is the index of SCC (or WCC) this node belongs to
410 * coincident contains a boolean for each of the rows of the schedule,
411 * indicating whether the corresponding scheduling dimension satisfies
412 * the coincidence constraints in the sense that the corresponding
413 * dependence distances are zero.
415 struct isl_sched_node {
416 isl_space *space;
417 int compressed;
418 isl_set *hull;
419 isl_multi_aff *compress;
420 isl_multi_aff *decompress;
421 isl_mat *sched;
422 isl_map *sched_map;
423 int rank;
424 isl_mat *cmap;
425 isl_mat *cinv;
426 int start;
427 int nvar;
428 int nparam;
430 int scc;
432 int *coincident;
435 static int node_has_space(const void *entry, const void *val)
437 struct isl_sched_node *node = (struct isl_sched_node *)entry;
438 isl_space *dim = (isl_space *)val;
440 return isl_space_is_equal(node->space, dim);
443 static int node_scc_exactly(struct isl_sched_node *node, int scc)
445 return node->scc == scc;
448 static int node_scc_at_most(struct isl_sched_node *node, int scc)
450 return node->scc <= scc;
453 static int node_scc_at_least(struct isl_sched_node *node, int scc)
455 return node->scc >= scc;
458 /* An edge in the dependence graph. An edge may be used to
459 * ensure validity of the generated schedule, to minimize the dependence
460 * distance or both
462 * map is the dependence relation, with i -> j in the map if j depends on i
463 * tagged_condition and tagged_validity contain the union of all tagged
464 * condition or conditional validity dependence relations that
465 * specialize the dependence relation "map"; that is,
466 * if (i -> a) -> (j -> b) is an element of "tagged_condition"
467 * or "tagged_validity", then i -> j is an element of "map".
468 * If these fields are NULL, then they represent the empty relation.
469 * src is the source node
470 * dst is the sink node
471 * validity is set if the edge is used to ensure correctness
472 * coincidence is used to enforce zero dependence distances
473 * proximity is set if the edge is used to minimize dependence distances
474 * condition is set if the edge represents a condition
475 * for a conditional validity schedule constraint
476 * local can only be set for condition edges and indicates that
477 * the dependence distance over the edge should be zero
478 * conditional_validity is set if the edge is used to conditionally
479 * ensure correctness
481 * For validity edges, start and end mark the sequence of inequality
482 * constraints in the LP problem that encode the validity constraint
483 * corresponding to this edge.
485 struct isl_sched_edge {
486 isl_map *map;
487 isl_union_map *tagged_condition;
488 isl_union_map *tagged_validity;
490 struct isl_sched_node *src;
491 struct isl_sched_node *dst;
493 unsigned validity : 1;
494 unsigned coincidence : 1;
495 unsigned proximity : 1;
496 unsigned local : 1;
497 unsigned condition : 1;
498 unsigned conditional_validity : 1;
500 int start;
501 int end;
504 /* Internal information about the dependence graph used during
505 * the construction of the schedule.
507 * intra_hmap is a cache, mapping dependence relations to their dual,
508 * for dependences from a node to itself
509 * inter_hmap is a cache, mapping dependence relations to their dual,
510 * for dependences between distinct nodes
511 * if compression is involved then the key for these maps
512 * it the original, uncompressed dependence relation, while
513 * the value is the dual of the compressed dependence relation.
515 * n is the number of nodes
516 * node is the list of nodes
517 * maxvar is the maximal number of variables over all nodes
518 * max_row is the allocated number of rows in the schedule
519 * n_row is the current (maximal) number of linearly independent
520 * rows in the node schedules
521 * n_total_row is the current number of rows in the node schedules
522 * band_start is the starting row in the node schedules of the current band
523 * root is set if this graph is the original dependence graph,
524 * without any splitting
526 * sorted contains a list of node indices sorted according to the
527 * SCC to which a node belongs
529 * n_edge is the number of edges
530 * edge is the list of edges
531 * max_edge contains the maximal number of edges of each type;
532 * in particular, it contains the number of edges in the inital graph.
533 * edge_table contains pointers into the edge array, hashed on the source
534 * and sink spaces; there is one such table for each type;
535 * a given edge may be referenced from more than one table
536 * if the corresponding relation appears in more than of the
537 * sets of dependences
539 * node_table contains pointers into the node array, hashed on the space
541 * region contains a list of variable sequences that should be non-trivial
543 * lp contains the (I)LP problem used to obtain new schedule rows
545 * src_scc and dst_scc are the source and sink SCCs of an edge with
546 * conflicting constraints
548 * scc represents the number of components
549 * weak is set if the components are weakly connected
551 struct isl_sched_graph {
552 isl_map_to_basic_set *intra_hmap;
553 isl_map_to_basic_set *inter_hmap;
555 struct isl_sched_node *node;
556 int n;
557 int maxvar;
558 int max_row;
559 int n_row;
561 int *sorted;
563 int n_total_row;
564 int band_start;
566 int root;
568 struct isl_sched_edge *edge;
569 int n_edge;
570 int max_edge[isl_edge_last + 1];
571 struct isl_hash_table *edge_table[isl_edge_last + 1];
573 struct isl_hash_table *node_table;
574 struct isl_region *region;
576 isl_basic_set *lp;
578 int src_scc;
579 int dst_scc;
581 int scc;
582 int weak;
585 /* Initialize node_table based on the list of nodes.
587 static int graph_init_table(isl_ctx *ctx, struct isl_sched_graph *graph)
589 int i;
591 graph->node_table = isl_hash_table_alloc(ctx, graph->n);
592 if (!graph->node_table)
593 return -1;
595 for (i = 0; i < graph->n; ++i) {
596 struct isl_hash_table_entry *entry;
597 uint32_t hash;
599 hash = isl_space_get_hash(graph->node[i].space);
600 entry = isl_hash_table_find(ctx, graph->node_table, hash,
601 &node_has_space,
602 graph->node[i].space, 1);
603 if (!entry)
604 return -1;
605 entry->data = &graph->node[i];
608 return 0;
611 /* Return a pointer to the node that lives within the given space,
612 * or NULL if there is no such node.
614 static struct isl_sched_node *graph_find_node(isl_ctx *ctx,
615 struct isl_sched_graph *graph, __isl_keep isl_space *dim)
617 struct isl_hash_table_entry *entry;
618 uint32_t hash;
620 hash = isl_space_get_hash(dim);
621 entry = isl_hash_table_find(ctx, graph->node_table, hash,
622 &node_has_space, dim, 0);
624 return entry ? entry->data : NULL;
627 static int edge_has_src_and_dst(const void *entry, const void *val)
629 const struct isl_sched_edge *edge = entry;
630 const struct isl_sched_edge *temp = val;
632 return edge->src == temp->src && edge->dst == temp->dst;
635 /* Add the given edge to graph->edge_table[type].
637 static int graph_edge_table_add(isl_ctx *ctx, struct isl_sched_graph *graph,
638 enum isl_edge_type type, struct isl_sched_edge *edge)
640 struct isl_hash_table_entry *entry;
641 uint32_t hash;
643 hash = isl_hash_init();
644 hash = isl_hash_builtin(hash, edge->src);
645 hash = isl_hash_builtin(hash, edge->dst);
646 entry = isl_hash_table_find(ctx, graph->edge_table[type], hash,
647 &edge_has_src_and_dst, edge, 1);
648 if (!entry)
649 return -1;
650 entry->data = edge;
652 return 0;
655 /* Allocate the edge_tables based on the maximal number of edges of
656 * each type.
658 static int graph_init_edge_tables(isl_ctx *ctx, struct isl_sched_graph *graph)
660 int i;
662 for (i = 0; i <= isl_edge_last; ++i) {
663 graph->edge_table[i] = isl_hash_table_alloc(ctx,
664 graph->max_edge[i]);
665 if (!graph->edge_table[i])
666 return -1;
669 return 0;
672 /* If graph->edge_table[type] contains an edge from the given source
673 * to the given destination, then return the hash table entry of this edge.
674 * Otherwise, return NULL.
676 static struct isl_hash_table_entry *graph_find_edge_entry(
677 struct isl_sched_graph *graph,
678 enum isl_edge_type type,
679 struct isl_sched_node *src, struct isl_sched_node *dst)
681 isl_ctx *ctx = isl_space_get_ctx(src->space);
682 uint32_t hash;
683 struct isl_sched_edge temp = { .src = src, .dst = dst };
685 hash = isl_hash_init();
686 hash = isl_hash_builtin(hash, temp.src);
687 hash = isl_hash_builtin(hash, temp.dst);
688 return isl_hash_table_find(ctx, graph->edge_table[type], hash,
689 &edge_has_src_and_dst, &temp, 0);
693 /* If graph->edge_table[type] contains an edge from the given source
694 * to the given destination, then return this edge.
695 * Otherwise, return NULL.
697 static struct isl_sched_edge *graph_find_edge(struct isl_sched_graph *graph,
698 enum isl_edge_type type,
699 struct isl_sched_node *src, struct isl_sched_node *dst)
701 struct isl_hash_table_entry *entry;
703 entry = graph_find_edge_entry(graph, type, src, dst);
704 if (!entry)
705 return NULL;
707 return entry->data;
710 /* Check whether the dependence graph has an edge of the given type
711 * between the given two nodes.
713 static int graph_has_edge(struct isl_sched_graph *graph,
714 enum isl_edge_type type,
715 struct isl_sched_node *src, struct isl_sched_node *dst)
717 struct isl_sched_edge *edge;
718 int empty;
720 edge = graph_find_edge(graph, type, src, dst);
721 if (!edge)
722 return 0;
724 empty = isl_map_plain_is_empty(edge->map);
725 if (empty < 0)
726 return -1;
728 return !empty;
731 /* Look for any edge with the same src, dst and map fields as "model".
733 * Return the matching edge if one can be found.
734 * Return "model" if no matching edge is found.
735 * Return NULL on error.
737 static struct isl_sched_edge *graph_find_matching_edge(
738 struct isl_sched_graph *graph, struct isl_sched_edge *model)
740 enum isl_edge_type i;
741 struct isl_sched_edge *edge;
743 for (i = isl_edge_first; i <= isl_edge_last; ++i) {
744 int is_equal;
746 edge = graph_find_edge(graph, i, model->src, model->dst);
747 if (!edge)
748 continue;
749 is_equal = isl_map_plain_is_equal(model->map, edge->map);
750 if (is_equal < 0)
751 return NULL;
752 if (is_equal)
753 return edge;
756 return model;
759 /* Remove the given edge from all the edge_tables that refer to it.
761 static void graph_remove_edge(struct isl_sched_graph *graph,
762 struct isl_sched_edge *edge)
764 isl_ctx *ctx = isl_map_get_ctx(edge->map);
765 enum isl_edge_type i;
767 for (i = isl_edge_first; i <= isl_edge_last; ++i) {
768 struct isl_hash_table_entry *entry;
770 entry = graph_find_edge_entry(graph, i, edge->src, edge->dst);
771 if (!entry)
772 continue;
773 if (entry->data != edge)
774 continue;
775 isl_hash_table_remove(ctx, graph->edge_table[i], entry);
779 /* Check whether the dependence graph has any edge
780 * between the given two nodes.
782 static int graph_has_any_edge(struct isl_sched_graph *graph,
783 struct isl_sched_node *src, struct isl_sched_node *dst)
785 enum isl_edge_type i;
786 int r;
788 for (i = isl_edge_first; i <= isl_edge_last; ++i) {
789 r = graph_has_edge(graph, i, src, dst);
790 if (r < 0 || r)
791 return r;
794 return r;
797 /* Check whether the dependence graph has a validity edge
798 * between the given two nodes.
800 * Conditional validity edges are essentially validity edges that
801 * can be ignored if the corresponding condition edges are iteration private.
802 * Here, we are only checking for the presence of validity
803 * edges, so we need to consider the conditional validity edges too.
804 * In particular, this function is used during the detection
805 * of strongly connected components and we cannot ignore
806 * conditional validity edges during this detection.
808 static int graph_has_validity_edge(struct isl_sched_graph *graph,
809 struct isl_sched_node *src, struct isl_sched_node *dst)
811 int r;
813 r = graph_has_edge(graph, isl_edge_validity, src, dst);
814 if (r < 0 || r)
815 return r;
817 return graph_has_edge(graph, isl_edge_conditional_validity, src, dst);
820 static int graph_alloc(isl_ctx *ctx, struct isl_sched_graph *graph,
821 int n_node, int n_edge)
823 int i;
825 graph->n = n_node;
826 graph->n_edge = n_edge;
827 graph->node = isl_calloc_array(ctx, struct isl_sched_node, graph->n);
828 graph->sorted = isl_calloc_array(ctx, int, graph->n);
829 graph->region = isl_alloc_array(ctx, struct isl_region, graph->n);
830 graph->edge = isl_calloc_array(ctx,
831 struct isl_sched_edge, graph->n_edge);
833 graph->intra_hmap = isl_map_to_basic_set_alloc(ctx, 2 * n_edge);
834 graph->inter_hmap = isl_map_to_basic_set_alloc(ctx, 2 * n_edge);
836 if (!graph->node || !graph->region || (graph->n_edge && !graph->edge) ||
837 !graph->sorted)
838 return -1;
840 for(i = 0; i < graph->n; ++i)
841 graph->sorted[i] = i;
843 return 0;
846 static void graph_free(isl_ctx *ctx, struct isl_sched_graph *graph)
848 int i;
850 isl_map_to_basic_set_free(graph->intra_hmap);
851 isl_map_to_basic_set_free(graph->inter_hmap);
853 if (graph->node)
854 for (i = 0; i < graph->n; ++i) {
855 isl_space_free(graph->node[i].space);
856 isl_set_free(graph->node[i].hull);
857 isl_multi_aff_free(graph->node[i].compress);
858 isl_multi_aff_free(graph->node[i].decompress);
859 isl_mat_free(graph->node[i].sched);
860 isl_map_free(graph->node[i].sched_map);
861 isl_mat_free(graph->node[i].cmap);
862 isl_mat_free(graph->node[i].cinv);
863 if (graph->root)
864 free(graph->node[i].coincident);
866 free(graph->node);
867 free(graph->sorted);
868 if (graph->edge)
869 for (i = 0; i < graph->n_edge; ++i) {
870 isl_map_free(graph->edge[i].map);
871 isl_union_map_free(graph->edge[i].tagged_condition);
872 isl_union_map_free(graph->edge[i].tagged_validity);
874 free(graph->edge);
875 free(graph->region);
876 for (i = 0; i <= isl_edge_last; ++i)
877 isl_hash_table_free(ctx, graph->edge_table[i]);
878 isl_hash_table_free(ctx, graph->node_table);
879 isl_basic_set_free(graph->lp);
882 /* For each "set" on which this function is called, increment
883 * graph->n by one and update graph->maxvar.
885 static int init_n_maxvar(__isl_take isl_set *set, void *user)
887 struct isl_sched_graph *graph = user;
888 int nvar = isl_set_dim(set, isl_dim_set);
890 graph->n++;
891 if (nvar > graph->maxvar)
892 graph->maxvar = nvar;
894 isl_set_free(set);
896 return 0;
899 /* Add the number of basic maps in "map" to *n.
901 static int add_n_basic_map(__isl_take isl_map *map, void *user)
903 int *n = user;
905 *n += isl_map_n_basic_map(map);
906 isl_map_free(map);
908 return 0;
911 /* Compute the number of rows that should be allocated for the schedule.
912 * In particular, we need one row for each variable or one row
913 * for each basic map in the dependences.
914 * Note that it is practically impossible to exhaust both
915 * the number of dependences and the number of variables.
917 static int compute_max_row(struct isl_sched_graph *graph,
918 __isl_keep isl_schedule_constraints *sc)
920 enum isl_edge_type i;
921 int n_edge;
923 graph->n = 0;
924 graph->maxvar = 0;
925 if (isl_union_set_foreach_set(sc->domain, &init_n_maxvar, graph) < 0)
926 return -1;
927 n_edge = 0;
928 for (i = isl_edge_first; i <= isl_edge_last; ++i)
929 if (isl_union_map_foreach_map(sc->constraint[i],
930 &add_n_basic_map, &n_edge) < 0)
931 return -1;
932 graph->max_row = n_edge + graph->maxvar;
934 return 0;
937 /* Does "bset" have any defining equalities for its set variables?
939 static int has_any_defining_equality(__isl_keep isl_basic_set *bset)
941 int i, n;
943 if (!bset)
944 return -1;
946 n = isl_basic_set_dim(bset, isl_dim_set);
947 for (i = 0; i < n; ++i) {
948 int has;
950 has = isl_basic_set_has_defining_equality(bset, isl_dim_set, i,
951 NULL);
952 if (has < 0 || has)
953 return has;
956 return 0;
959 /* Add a new node to the graph representing the given space.
960 * "nvar" is the (possibly compressed) number of variables and
961 * may be smaller than then number of set variables in "space"
962 * if "compressed" is set.
963 * If "compressed" is set, then "hull" represents the constraints
964 * that were used to derive the compression, while "compress" and
965 * "decompress" map the original space to the compressed space and
966 * vice versa.
967 * If "compressed" is not set, then "hull", "compress" and "decompress"
968 * should be NULL.
970 static int add_node(struct isl_sched_graph *graph, __isl_take isl_space *space,
971 int nvar, int compressed, __isl_take isl_set *hull,
972 __isl_take isl_multi_aff *compress,
973 __isl_take isl_multi_aff *decompress)
975 int nparam;
976 isl_ctx *ctx;
977 isl_mat *sched;
978 int *coincident;
980 if (!space)
981 return -1;
983 ctx = isl_space_get_ctx(space);
984 nparam = isl_space_dim(space, isl_dim_param);
985 if (!ctx->opt->schedule_parametric)
986 nparam = 0;
987 sched = isl_mat_alloc(ctx, 0, 1 + nparam + nvar);
988 graph->node[graph->n].space = space;
989 graph->node[graph->n].nvar = nvar;
990 graph->node[graph->n].nparam = nparam;
991 graph->node[graph->n].sched = sched;
992 graph->node[graph->n].sched_map = NULL;
993 coincident = isl_calloc_array(ctx, int, graph->max_row);
994 graph->node[graph->n].coincident = coincident;
995 graph->node[graph->n].compressed = compressed;
996 graph->node[graph->n].hull = hull;
997 graph->node[graph->n].compress = compress;
998 graph->node[graph->n].decompress = decompress;
999 graph->n++;
1001 if (!space || !sched || (graph->max_row && !coincident))
1002 return -1;
1003 if (compressed && (!hull || !compress || !decompress))
1004 return -1;
1006 return 0;
1009 /* Add a new node to the graph representing the given set.
1011 * If any of the set variables is defined by an equality, then
1012 * we perform variable compression such that we can perform
1013 * the scheduling on the compressed domain.
1015 static int extract_node(__isl_take isl_set *set, void *user)
1017 int nvar;
1018 int has_equality;
1019 isl_space *space;
1020 isl_basic_set *hull;
1021 isl_set *hull_set;
1022 isl_morph *morph;
1023 isl_multi_aff *compress, *decompress;
1024 struct isl_sched_graph *graph = user;
1026 space = isl_set_get_space(set);
1027 hull = isl_set_affine_hull(set);
1028 hull = isl_basic_set_remove_divs(hull);
1029 nvar = isl_space_dim(space, isl_dim_set);
1030 has_equality = has_any_defining_equality(hull);
1032 if (has_equality < 0)
1033 goto error;
1034 if (!has_equality) {
1035 isl_basic_set_free(hull);
1036 return add_node(graph, space, nvar, 0, NULL, NULL, NULL);
1039 morph = isl_basic_set_variable_compression(hull, isl_dim_set);
1040 nvar = isl_morph_ran_dim(morph, isl_dim_set);
1041 compress = isl_morph_get_var_multi_aff(morph);
1042 morph = isl_morph_inverse(morph);
1043 decompress = isl_morph_get_var_multi_aff(morph);
1044 isl_morph_free(morph);
1046 hull_set = isl_set_from_basic_set(hull);
1047 return add_node(graph, space, nvar, 1, hull_set, compress, decompress);
1048 error:
1049 isl_basic_set_free(hull);
1050 isl_space_free(space);
1051 return -1;
1054 struct isl_extract_edge_data {
1055 enum isl_edge_type type;
1056 struct isl_sched_graph *graph;
1059 /* Merge edge2 into edge1, freeing the contents of edge2.
1060 * "type" is the type of the schedule constraint from which edge2 was
1061 * extracted.
1062 * Return 0 on success and -1 on failure.
1064 * edge1 and edge2 are assumed to have the same value for the map field.
1066 static int merge_edge(enum isl_edge_type type, struct isl_sched_edge *edge1,
1067 struct isl_sched_edge *edge2)
1069 edge1->validity |= edge2->validity;
1070 edge1->coincidence |= edge2->coincidence;
1071 edge1->proximity |= edge2->proximity;
1072 edge1->condition |= edge2->condition;
1073 edge1->conditional_validity |= edge2->conditional_validity;
1074 isl_map_free(edge2->map);
1076 if (type == isl_edge_condition) {
1077 if (!edge1->tagged_condition)
1078 edge1->tagged_condition = edge2->tagged_condition;
1079 else
1080 edge1->tagged_condition =
1081 isl_union_map_union(edge1->tagged_condition,
1082 edge2->tagged_condition);
1085 if (type == isl_edge_conditional_validity) {
1086 if (!edge1->tagged_validity)
1087 edge1->tagged_validity = edge2->tagged_validity;
1088 else
1089 edge1->tagged_validity =
1090 isl_union_map_union(edge1->tagged_validity,
1091 edge2->tagged_validity);
1094 if (type == isl_edge_condition && !edge1->tagged_condition)
1095 return -1;
1096 if (type == isl_edge_conditional_validity && !edge1->tagged_validity)
1097 return -1;
1099 return 0;
1102 /* Insert dummy tags in domain and range of "map".
1104 * In particular, if "map" is of the form
1106 * A -> B
1108 * then return
1110 * [A -> dummy_tag] -> [B -> dummy_tag]
1112 * where the dummy_tags are identical and equal to any dummy tags
1113 * introduced by any other call to this function.
1115 static __isl_give isl_map *insert_dummy_tags(__isl_take isl_map *map)
1117 static char dummy;
1118 isl_ctx *ctx;
1119 isl_id *id;
1120 isl_space *space;
1121 isl_set *domain, *range;
1123 ctx = isl_map_get_ctx(map);
1125 id = isl_id_alloc(ctx, NULL, &dummy);
1126 space = isl_space_params(isl_map_get_space(map));
1127 space = isl_space_set_from_params(space);
1128 space = isl_space_set_tuple_id(space, isl_dim_set, id);
1129 space = isl_space_map_from_set(space);
1131 domain = isl_map_wrap(map);
1132 range = isl_map_wrap(isl_map_universe(space));
1133 map = isl_map_from_domain_and_range(domain, range);
1134 map = isl_map_zip(map);
1136 return map;
1139 /* Given that at least one of "src" or "dst" is compressed, return
1140 * a map between the spaces of these nodes restricted to the affine
1141 * hull that was used in the compression.
1143 static __isl_give isl_map *extract_hull(struct isl_sched_node *src,
1144 struct isl_sched_node *dst)
1146 isl_set *dom, *ran;
1148 if (src->compressed)
1149 dom = isl_set_copy(src->hull);
1150 else
1151 dom = isl_set_universe(isl_space_copy(src->space));
1152 if (dst->compressed)
1153 ran = isl_set_copy(dst->hull);
1154 else
1155 ran = isl_set_universe(isl_space_copy(dst->space));
1157 return isl_map_from_domain_and_range(dom, ran);
1160 /* Intersect the domains of the nested relations in domain and range
1161 * of "tagged" with "map".
1163 static __isl_give isl_map *map_intersect_domains(__isl_take isl_map *tagged,
1164 __isl_keep isl_map *map)
1166 isl_set *set;
1168 tagged = isl_map_zip(tagged);
1169 set = isl_map_wrap(isl_map_copy(map));
1170 tagged = isl_map_intersect_domain(tagged, set);
1171 tagged = isl_map_zip(tagged);
1172 return tagged;
1175 /* Add a new edge to the graph based on the given map
1176 * and add it to data->graph->edge_table[data->type].
1177 * If a dependence relation of a given type happens to be identical
1178 * to one of the dependence relations of a type that was added before,
1179 * then we don't create a new edge, but instead mark the original edge
1180 * as also representing a dependence of the current type.
1182 * Edges of type isl_edge_condition or isl_edge_conditional_validity
1183 * may be specified as "tagged" dependence relations. That is, "map"
1184 * may contain elements (i -> a) -> (j -> b), where i -> j denotes
1185 * the dependence on iterations and a and b are tags.
1186 * edge->map is set to the relation containing the elements i -> j,
1187 * while edge->tagged_condition and edge->tagged_validity contain
1188 * the union of all the "map" relations
1189 * for which extract_edge is called that result in the same edge->map.
1191 * If the source or the destination node is compressed, then
1192 * intersect both "map" and "tagged" with the constraints that
1193 * were used to construct the compression.
1194 * This ensures that there are no schedule constraints defined
1195 * outside of these domains, while the scheduler no longer has
1196 * any control over those outside parts.
1198 static int extract_edge(__isl_take isl_map *map, void *user)
1200 isl_ctx *ctx = isl_map_get_ctx(map);
1201 struct isl_extract_edge_data *data = user;
1202 struct isl_sched_graph *graph = data->graph;
1203 struct isl_sched_node *src, *dst;
1204 isl_space *dim;
1205 struct isl_sched_edge *edge;
1206 isl_map *tagged = NULL;
1208 if (data->type == isl_edge_condition ||
1209 data->type == isl_edge_conditional_validity) {
1210 if (isl_map_can_zip(map)) {
1211 tagged = isl_map_copy(map);
1212 map = isl_set_unwrap(isl_map_domain(isl_map_zip(map)));
1213 } else {
1214 tagged = insert_dummy_tags(isl_map_copy(map));
1218 dim = isl_space_domain(isl_map_get_space(map));
1219 src = graph_find_node(ctx, graph, dim);
1220 isl_space_free(dim);
1221 dim = isl_space_range(isl_map_get_space(map));
1222 dst = graph_find_node(ctx, graph, dim);
1223 isl_space_free(dim);
1225 if (!src || !dst) {
1226 isl_map_free(map);
1227 isl_map_free(tagged);
1228 return 0;
1231 if (src->compressed || dst->compressed) {
1232 isl_map *hull;
1233 hull = extract_hull(src, dst);
1234 if (tagged)
1235 tagged = map_intersect_domains(tagged, hull);
1236 map = isl_map_intersect(map, hull);
1239 graph->edge[graph->n_edge].src = src;
1240 graph->edge[graph->n_edge].dst = dst;
1241 graph->edge[graph->n_edge].map = map;
1242 graph->edge[graph->n_edge].validity = 0;
1243 graph->edge[graph->n_edge].coincidence = 0;
1244 graph->edge[graph->n_edge].proximity = 0;
1245 graph->edge[graph->n_edge].condition = 0;
1246 graph->edge[graph->n_edge].local = 0;
1247 graph->edge[graph->n_edge].conditional_validity = 0;
1248 graph->edge[graph->n_edge].tagged_condition = NULL;
1249 graph->edge[graph->n_edge].tagged_validity = NULL;
1250 if (data->type == isl_edge_validity)
1251 graph->edge[graph->n_edge].validity = 1;
1252 if (data->type == isl_edge_coincidence)
1253 graph->edge[graph->n_edge].coincidence = 1;
1254 if (data->type == isl_edge_proximity)
1255 graph->edge[graph->n_edge].proximity = 1;
1256 if (data->type == isl_edge_condition) {
1257 graph->edge[graph->n_edge].condition = 1;
1258 graph->edge[graph->n_edge].tagged_condition =
1259 isl_union_map_from_map(tagged);
1261 if (data->type == isl_edge_conditional_validity) {
1262 graph->edge[graph->n_edge].conditional_validity = 1;
1263 graph->edge[graph->n_edge].tagged_validity =
1264 isl_union_map_from_map(tagged);
1267 edge = graph_find_matching_edge(graph, &graph->edge[graph->n_edge]);
1268 if (!edge) {
1269 graph->n_edge++;
1270 return -1;
1272 if (edge == &graph->edge[graph->n_edge])
1273 return graph_edge_table_add(ctx, graph, data->type,
1274 &graph->edge[graph->n_edge++]);
1276 if (merge_edge(data->type, edge, &graph->edge[graph->n_edge]) < 0)
1277 return -1;
1279 return graph_edge_table_add(ctx, graph, data->type, edge);
1282 /* Check whether there is any dependence from node[j] to node[i]
1283 * or from node[i] to node[j].
1285 static int node_follows_weak(int i, int j, void *user)
1287 int f;
1288 struct isl_sched_graph *graph = user;
1290 f = graph_has_any_edge(graph, &graph->node[j], &graph->node[i]);
1291 if (f < 0 || f)
1292 return f;
1293 return graph_has_any_edge(graph, &graph->node[i], &graph->node[j]);
1296 /* Check whether there is a (conditional) validity dependence from node[j]
1297 * to node[i], forcing node[i] to follow node[j].
1299 static int node_follows_strong(int i, int j, void *user)
1301 struct isl_sched_graph *graph = user;
1303 return graph_has_validity_edge(graph, &graph->node[j], &graph->node[i]);
1306 /* Use Tarjan's algorithm for computing the strongly connected components
1307 * in the dependence graph (only validity edges).
1308 * If weak is set, we consider the graph to be undirected and
1309 * we effectively compute the (weakly) connected components.
1310 * Additionally, we also consider other edges when weak is set.
1312 static int detect_ccs(isl_ctx *ctx, struct isl_sched_graph *graph, int weak)
1314 int i, n;
1315 struct isl_tarjan_graph *g = NULL;
1317 g = isl_tarjan_graph_init(ctx, graph->n,
1318 weak ? &node_follows_weak : &node_follows_strong, graph);
1319 if (!g)
1320 return -1;
1322 graph->weak = weak;
1323 graph->scc = 0;
1324 i = 0;
1325 n = graph->n;
1326 while (n) {
1327 while (g->order[i] != -1) {
1328 graph->node[g->order[i]].scc = graph->scc;
1329 --n;
1330 ++i;
1332 ++i;
1333 graph->scc++;
1336 isl_tarjan_graph_free(g);
1338 return 0;
1341 /* Apply Tarjan's algorithm to detect the strongly connected components
1342 * in the dependence graph.
1344 static int detect_sccs(isl_ctx *ctx, struct isl_sched_graph *graph)
1346 return detect_ccs(ctx, graph, 0);
1349 /* Apply Tarjan's algorithm to detect the (weakly) connected components
1350 * in the dependence graph.
1352 static int detect_wccs(isl_ctx *ctx, struct isl_sched_graph *graph)
1354 return detect_ccs(ctx, graph, 1);
1357 static int cmp_scc(const void *a, const void *b, void *data)
1359 struct isl_sched_graph *graph = data;
1360 const int *i1 = a;
1361 const int *i2 = b;
1363 return graph->node[*i1].scc - graph->node[*i2].scc;
1366 /* Sort the elements of graph->sorted according to the corresponding SCCs.
1368 static int sort_sccs(struct isl_sched_graph *graph)
1370 return isl_sort(graph->sorted, graph->n, sizeof(int), &cmp_scc, graph);
1373 /* Given a dependence relation R from "node" to itself,
1374 * construct the set of coefficients of valid constraints for elements
1375 * in that dependence relation.
1376 * In particular, the result contains tuples of coefficients
1377 * c_0, c_n, c_x such that
1379 * c_0 + c_n n + c_x y - c_x x >= 0 for each (x,y) in R
1381 * or, equivalently,
1383 * c_0 + c_n n + c_x d >= 0 for each d in delta R = { y - x | (x,y) in R }
1385 * We choose here to compute the dual of delta R.
1386 * Alternatively, we could have computed the dual of R, resulting
1387 * in a set of tuples c_0, c_n, c_x, c_y, and then
1388 * plugged in (c_0, c_n, c_x, -c_x).
1390 * If "node" has been compressed, then the dependence relation
1391 * is also compressed before the set of coefficients is computed.
1393 static __isl_give isl_basic_set *intra_coefficients(
1394 struct isl_sched_graph *graph, struct isl_sched_node *node,
1395 __isl_take isl_map *map)
1397 isl_set *delta;
1398 isl_map *key;
1399 isl_basic_set *coef;
1401 if (isl_map_to_basic_set_has(graph->intra_hmap, map))
1402 return isl_map_to_basic_set_get(graph->intra_hmap, map);
1404 key = isl_map_copy(map);
1405 if (node->compressed) {
1406 map = isl_map_preimage_domain_multi_aff(map,
1407 isl_multi_aff_copy(node->decompress));
1408 map = isl_map_preimage_range_multi_aff(map,
1409 isl_multi_aff_copy(node->decompress));
1411 delta = isl_set_remove_divs(isl_map_deltas(map));
1412 coef = isl_set_coefficients(delta);
1413 graph->intra_hmap = isl_map_to_basic_set_set(graph->intra_hmap, key,
1414 isl_basic_set_copy(coef));
1416 return coef;
1419 /* Given a dependence relation R, construct the set of coefficients
1420 * of valid constraints for elements in that dependence relation.
1421 * In particular, the result contains tuples of coefficients
1422 * c_0, c_n, c_x, c_y such that
1424 * c_0 + c_n n + c_x x + c_y y >= 0 for each (x,y) in R
1426 * If the source or destination nodes of "edge" have been compressed,
1427 * then the dependence relation is also compressed before
1428 * the set of coefficients is computed.
1430 static __isl_give isl_basic_set *inter_coefficients(
1431 struct isl_sched_graph *graph, struct isl_sched_edge *edge,
1432 __isl_take isl_map *map)
1434 isl_set *set;
1435 isl_map *key;
1436 isl_basic_set *coef;
1438 if (isl_map_to_basic_set_has(graph->inter_hmap, map))
1439 return isl_map_to_basic_set_get(graph->inter_hmap, map);
1441 key = isl_map_copy(map);
1442 if (edge->src->compressed)
1443 map = isl_map_preimage_domain_multi_aff(map,
1444 isl_multi_aff_copy(edge->src->decompress));
1445 if (edge->dst->compressed)
1446 map = isl_map_preimage_range_multi_aff(map,
1447 isl_multi_aff_copy(edge->dst->decompress));
1448 set = isl_map_wrap(isl_map_remove_divs(map));
1449 coef = isl_set_coefficients(set);
1450 graph->inter_hmap = isl_map_to_basic_set_set(graph->inter_hmap, key,
1451 isl_basic_set_copy(coef));
1453 return coef;
1456 /* Add constraints to graph->lp that force validity for the given
1457 * dependence from a node i to itself.
1458 * That is, add constraints that enforce
1460 * (c_i_0 + c_i_n n + c_i_x y) - (c_i_0 + c_i_n n + c_i_x x)
1461 * = c_i_x (y - x) >= 0
1463 * for each (x,y) in R.
1464 * We obtain general constraints on coefficients (c_0, c_n, c_x)
1465 * of valid constraints for (y - x) and then plug in (0, 0, c_i_x^+ - c_i_x^-),
1466 * where c_i_x = c_i_x^+ - c_i_x^-, with c_i_x^+ and c_i_x^- non-negative.
1467 * In graph->lp, the c_i_x^- appear before their c_i_x^+ counterpart.
1469 * Actually, we do not construct constraints for the c_i_x themselves,
1470 * but for the coefficients of c_i_x written as a linear combination
1471 * of the columns in node->cmap.
1473 static int add_intra_validity_constraints(struct isl_sched_graph *graph,
1474 struct isl_sched_edge *edge)
1476 unsigned total;
1477 isl_map *map = isl_map_copy(edge->map);
1478 isl_ctx *ctx = isl_map_get_ctx(map);
1479 isl_space *dim;
1480 isl_dim_map *dim_map;
1481 isl_basic_set *coef;
1482 struct isl_sched_node *node = edge->src;
1484 coef = intra_coefficients(graph, node, map);
1486 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
1488 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
1489 isl_space_dim(dim, isl_dim_set), isl_mat_copy(node->cmap));
1490 if (!coef)
1491 goto error;
1493 total = isl_basic_set_total_dim(graph->lp);
1494 dim_map = isl_dim_map_alloc(ctx, total);
1495 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 1, 2,
1496 isl_space_dim(dim, isl_dim_set), 1,
1497 node->nvar, -1);
1498 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 2, 2,
1499 isl_space_dim(dim, isl_dim_set), 1,
1500 node->nvar, 1);
1501 graph->lp = isl_basic_set_extend_constraints(graph->lp,
1502 coef->n_eq, coef->n_ineq);
1503 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
1504 coef, dim_map);
1505 isl_space_free(dim);
1507 return 0;
1508 error:
1509 isl_space_free(dim);
1510 return -1;
1513 /* Add constraints to graph->lp that force validity for the given
1514 * dependence from node i to node j.
1515 * That is, add constraints that enforce
1517 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x) >= 0
1519 * for each (x,y) in R.
1520 * We obtain general constraints on coefficients (c_0, c_n, c_x, c_y)
1521 * of valid constraints for R and then plug in
1522 * (c_j_0 - c_i_0, c_j_n^+ - c_j_n^- - (c_i_n^+ - c_i_n^-),
1523 * c_j_x^+ - c_j_x^- - (c_i_x^+ - c_i_x^-)),
1524 * where c_* = c_*^+ - c_*^-, with c_*^+ and c_*^- non-negative.
1525 * In graph->lp, the c_*^- appear before their c_*^+ counterpart.
1527 * Actually, we do not construct constraints for the c_*_x themselves,
1528 * but for the coefficients of c_*_x written as a linear combination
1529 * of the columns in node->cmap.
1531 static int add_inter_validity_constraints(struct isl_sched_graph *graph,
1532 struct isl_sched_edge *edge)
1534 unsigned total;
1535 isl_map *map = isl_map_copy(edge->map);
1536 isl_ctx *ctx = isl_map_get_ctx(map);
1537 isl_space *dim;
1538 isl_dim_map *dim_map;
1539 isl_basic_set *coef;
1540 struct isl_sched_node *src = edge->src;
1541 struct isl_sched_node *dst = edge->dst;
1543 coef = inter_coefficients(graph, edge, map);
1545 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
1547 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
1548 isl_space_dim(dim, isl_dim_set), isl_mat_copy(src->cmap));
1549 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
1550 isl_space_dim(dim, isl_dim_set) + src->nvar,
1551 isl_mat_copy(dst->cmap));
1552 if (!coef)
1553 goto error;
1555 total = isl_basic_set_total_dim(graph->lp);
1556 dim_map = isl_dim_map_alloc(ctx, total);
1558 isl_dim_map_range(dim_map, dst->start, 0, 0, 0, 1, 1);
1559 isl_dim_map_range(dim_map, dst->start + 1, 2, 1, 1, dst->nparam, -1);
1560 isl_dim_map_range(dim_map, dst->start + 2, 2, 1, 1, dst->nparam, 1);
1561 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 1, 2,
1562 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
1563 dst->nvar, -1);
1564 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 2, 2,
1565 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
1566 dst->nvar, 1);
1568 isl_dim_map_range(dim_map, src->start, 0, 0, 0, 1, -1);
1569 isl_dim_map_range(dim_map, src->start + 1, 2, 1, 1, src->nparam, 1);
1570 isl_dim_map_range(dim_map, src->start + 2, 2, 1, 1, src->nparam, -1);
1571 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 1, 2,
1572 isl_space_dim(dim, isl_dim_set), 1,
1573 src->nvar, 1);
1574 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 2, 2,
1575 isl_space_dim(dim, isl_dim_set), 1,
1576 src->nvar, -1);
1578 edge->start = graph->lp->n_ineq;
1579 graph->lp = isl_basic_set_extend_constraints(graph->lp,
1580 coef->n_eq, coef->n_ineq);
1581 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
1582 coef, dim_map);
1583 if (!graph->lp)
1584 goto error;
1585 isl_space_free(dim);
1586 edge->end = graph->lp->n_ineq;
1588 return 0;
1589 error:
1590 isl_space_free(dim);
1591 return -1;
1594 /* Add constraints to graph->lp that bound the dependence distance for the given
1595 * dependence from a node i to itself.
1596 * If s = 1, we add the constraint
1598 * c_i_x (y - x) <= m_0 + m_n n
1600 * or
1602 * -c_i_x (y - x) + m_0 + m_n n >= 0
1604 * for each (x,y) in R.
1605 * If s = -1, we add the constraint
1607 * -c_i_x (y - x) <= m_0 + m_n n
1609 * or
1611 * c_i_x (y - x) + m_0 + m_n n >= 0
1613 * for each (x,y) in R.
1614 * We obtain general constraints on coefficients (c_0, c_n, c_x)
1615 * of valid constraints for (y - x) and then plug in (m_0, m_n, -s * c_i_x),
1616 * with each coefficient (except m_0) represented as a pair of non-negative
1617 * coefficients.
1619 * Actually, we do not construct constraints for the c_i_x themselves,
1620 * but for the coefficients of c_i_x written as a linear combination
1621 * of the columns in node->cmap.
1624 * If "local" is set, then we add constraints
1626 * c_i_x (y - x) <= 0
1628 * or
1630 * -c_i_x (y - x) <= 0
1632 * instead, forcing the dependence distance to be (less than or) equal to 0.
1633 * That is, we plug in (0, 0, -s * c_i_x),
1634 * Note that dependences marked local are treated as validity constraints
1635 * by add_all_validity_constraints and therefore also have
1636 * their distances bounded by 0 from below.
1638 static int add_intra_proximity_constraints(struct isl_sched_graph *graph,
1639 struct isl_sched_edge *edge, int s, int local)
1641 unsigned total;
1642 unsigned nparam;
1643 isl_map *map = isl_map_copy(edge->map);
1644 isl_ctx *ctx = isl_map_get_ctx(map);
1645 isl_space *dim;
1646 isl_dim_map *dim_map;
1647 isl_basic_set *coef;
1648 struct isl_sched_node *node = edge->src;
1650 coef = intra_coefficients(graph, node, map);
1652 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
1654 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
1655 isl_space_dim(dim, isl_dim_set), isl_mat_copy(node->cmap));
1656 if (!coef)
1657 goto error;
1659 nparam = isl_space_dim(node->space, isl_dim_param);
1660 total = isl_basic_set_total_dim(graph->lp);
1661 dim_map = isl_dim_map_alloc(ctx, total);
1663 if (!local) {
1664 isl_dim_map_range(dim_map, 1, 0, 0, 0, 1, 1);
1665 isl_dim_map_range(dim_map, 4, 2, 1, 1, nparam, -1);
1666 isl_dim_map_range(dim_map, 5, 2, 1, 1, nparam, 1);
1668 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 1, 2,
1669 isl_space_dim(dim, isl_dim_set), 1,
1670 node->nvar, s);
1671 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 2, 2,
1672 isl_space_dim(dim, isl_dim_set), 1,
1673 node->nvar, -s);
1674 graph->lp = isl_basic_set_extend_constraints(graph->lp,
1675 coef->n_eq, coef->n_ineq);
1676 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
1677 coef, dim_map);
1678 isl_space_free(dim);
1680 return 0;
1681 error:
1682 isl_space_free(dim);
1683 return -1;
1686 /* Add constraints to graph->lp that bound the dependence distance for the given
1687 * dependence from node i to node j.
1688 * If s = 1, we add the constraint
1690 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x)
1691 * <= m_0 + m_n n
1693 * or
1695 * -(c_j_0 + c_j_n n + c_j_x y) + (c_i_0 + c_i_n n + c_i_x x) +
1696 * m_0 + m_n n >= 0
1698 * for each (x,y) in R.
1699 * If s = -1, we add the constraint
1701 * -((c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x))
1702 * <= m_0 + m_n n
1704 * or
1706 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x) +
1707 * m_0 + m_n n >= 0
1709 * for each (x,y) in R.
1710 * We obtain general constraints on coefficients (c_0, c_n, c_x, c_y)
1711 * of valid constraints for R and then plug in
1712 * (m_0 - s*c_j_0 + s*c_i_0, m_n - s*c_j_n + s*c_i_n,
1713 * -s*c_j_x+s*c_i_x)
1714 * with each coefficient (except m_0, c_j_0 and c_i_0)
1715 * represented as a pair of non-negative coefficients.
1717 * Actually, we do not construct constraints for the c_*_x themselves,
1718 * but for the coefficients of c_*_x written as a linear combination
1719 * of the columns in node->cmap.
1722 * If "local" is set, then we add constraints
1724 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x) <= 0
1726 * or
1728 * -((c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x)) <= 0
1730 * instead, forcing the dependence distance to be (less than or) equal to 0.
1731 * That is, we plug in
1732 * (-s*c_j_0 + s*c_i_0, -s*c_j_n + s*c_i_n, -s*c_j_x+s*c_i_x).
1733 * Note that dependences marked local are treated as validity constraints
1734 * by add_all_validity_constraints and therefore also have
1735 * their distances bounded by 0 from below.
1737 static int add_inter_proximity_constraints(struct isl_sched_graph *graph,
1738 struct isl_sched_edge *edge, int s, int local)
1740 unsigned total;
1741 unsigned nparam;
1742 isl_map *map = isl_map_copy(edge->map);
1743 isl_ctx *ctx = isl_map_get_ctx(map);
1744 isl_space *dim;
1745 isl_dim_map *dim_map;
1746 isl_basic_set *coef;
1747 struct isl_sched_node *src = edge->src;
1748 struct isl_sched_node *dst = edge->dst;
1750 coef = inter_coefficients(graph, edge, map);
1752 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
1754 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
1755 isl_space_dim(dim, isl_dim_set), isl_mat_copy(src->cmap));
1756 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
1757 isl_space_dim(dim, isl_dim_set) + src->nvar,
1758 isl_mat_copy(dst->cmap));
1759 if (!coef)
1760 goto error;
1762 nparam = isl_space_dim(src->space, isl_dim_param);
1763 total = isl_basic_set_total_dim(graph->lp);
1764 dim_map = isl_dim_map_alloc(ctx, total);
1766 if (!local) {
1767 isl_dim_map_range(dim_map, 1, 0, 0, 0, 1, 1);
1768 isl_dim_map_range(dim_map, 4, 2, 1, 1, nparam, -1);
1769 isl_dim_map_range(dim_map, 5, 2, 1, 1, nparam, 1);
1772 isl_dim_map_range(dim_map, dst->start, 0, 0, 0, 1, -s);
1773 isl_dim_map_range(dim_map, dst->start + 1, 2, 1, 1, dst->nparam, s);
1774 isl_dim_map_range(dim_map, dst->start + 2, 2, 1, 1, dst->nparam, -s);
1775 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 1, 2,
1776 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
1777 dst->nvar, s);
1778 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 2, 2,
1779 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
1780 dst->nvar, -s);
1782 isl_dim_map_range(dim_map, src->start, 0, 0, 0, 1, s);
1783 isl_dim_map_range(dim_map, src->start + 1, 2, 1, 1, src->nparam, -s);
1784 isl_dim_map_range(dim_map, src->start + 2, 2, 1, 1, src->nparam, s);
1785 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 1, 2,
1786 isl_space_dim(dim, isl_dim_set), 1,
1787 src->nvar, -s);
1788 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 2, 2,
1789 isl_space_dim(dim, isl_dim_set), 1,
1790 src->nvar, s);
1792 graph->lp = isl_basic_set_extend_constraints(graph->lp,
1793 coef->n_eq, coef->n_ineq);
1794 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
1795 coef, dim_map);
1796 isl_space_free(dim);
1798 return 0;
1799 error:
1800 isl_space_free(dim);
1801 return -1;
1804 /* Add all validity constraints to graph->lp.
1806 * An edge that is forced to be local needs to have its dependence
1807 * distances equal to zero. We take care of bounding them by 0 from below
1808 * here. add_all_proximity_constraints takes care of bounding them by 0
1809 * from above.
1811 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
1812 * Otherwise, we ignore them.
1814 static int add_all_validity_constraints(struct isl_sched_graph *graph,
1815 int use_coincidence)
1817 int i;
1819 for (i = 0; i < graph->n_edge; ++i) {
1820 struct isl_sched_edge *edge= &graph->edge[i];
1821 int local;
1823 local = edge->local || (edge->coincidence && use_coincidence);
1824 if (!edge->validity && !local)
1825 continue;
1826 if (edge->src != edge->dst)
1827 continue;
1828 if (add_intra_validity_constraints(graph, edge) < 0)
1829 return -1;
1832 for (i = 0; i < graph->n_edge; ++i) {
1833 struct isl_sched_edge *edge = &graph->edge[i];
1834 int local;
1836 local = edge->local || (edge->coincidence && use_coincidence);
1837 if (!edge->validity && !local)
1838 continue;
1839 if (edge->src == edge->dst)
1840 continue;
1841 if (add_inter_validity_constraints(graph, edge) < 0)
1842 return -1;
1845 return 0;
1848 /* Add constraints to graph->lp that bound the dependence distance
1849 * for all dependence relations.
1850 * If a given proximity dependence is identical to a validity
1851 * dependence, then the dependence distance is already bounded
1852 * from below (by zero), so we only need to bound the distance
1853 * from above. (This includes the case of "local" dependences
1854 * which are treated as validity dependence by add_all_validity_constraints.)
1855 * Otherwise, we need to bound the distance both from above and from below.
1857 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
1858 * Otherwise, we ignore them.
1860 static int add_all_proximity_constraints(struct isl_sched_graph *graph,
1861 int use_coincidence)
1863 int i;
1865 for (i = 0; i < graph->n_edge; ++i) {
1866 struct isl_sched_edge *edge= &graph->edge[i];
1867 int local;
1869 local = edge->local || (edge->coincidence && use_coincidence);
1870 if (!edge->proximity && !local)
1871 continue;
1872 if (edge->src == edge->dst &&
1873 add_intra_proximity_constraints(graph, edge, 1, local) < 0)
1874 return -1;
1875 if (edge->src != edge->dst &&
1876 add_inter_proximity_constraints(graph, edge, 1, local) < 0)
1877 return -1;
1878 if (edge->validity || local)
1879 continue;
1880 if (edge->src == edge->dst &&
1881 add_intra_proximity_constraints(graph, edge, -1, 0) < 0)
1882 return -1;
1883 if (edge->src != edge->dst &&
1884 add_inter_proximity_constraints(graph, edge, -1, 0) < 0)
1885 return -1;
1888 return 0;
1891 /* Compute a basis for the rows in the linear part of the schedule
1892 * and extend this basis to a full basis. The remaining rows
1893 * can then be used to force linear independence from the rows
1894 * in the schedule.
1896 * In particular, given the schedule rows S, we compute
1898 * S = H Q
1899 * S U = H
1901 * with H the Hermite normal form of S. That is, all but the
1902 * first rank columns of H are zero and so each row in S is
1903 * a linear combination of the first rank rows of Q.
1904 * The matrix Q is then transposed because we will write the
1905 * coefficients of the next schedule row as a column vector s
1906 * and express this s as a linear combination s = Q c of the
1907 * computed basis.
1908 * Similarly, the matrix U is transposed such that we can
1909 * compute the coefficients c = U s from a schedule row s.
1911 static int node_update_cmap(struct isl_sched_node *node)
1913 isl_mat *H, *U, *Q;
1914 int n_row = isl_mat_rows(node->sched);
1916 H = isl_mat_sub_alloc(node->sched, 0, n_row,
1917 1 + node->nparam, node->nvar);
1919 H = isl_mat_left_hermite(H, 0, &U, &Q);
1920 isl_mat_free(node->cmap);
1921 isl_mat_free(node->cinv);
1922 node->cmap = isl_mat_transpose(Q);
1923 node->cinv = isl_mat_transpose(U);
1924 node->rank = isl_mat_initial_non_zero_cols(H);
1925 isl_mat_free(H);
1927 if (!node->cmap || !node->cinv || node->rank < 0)
1928 return -1;
1929 return 0;
1932 /* How many times should we count the constraints in "edge"?
1934 * If carry is set, then we are counting the number of
1935 * (validity or conditional validity) constraints that will be added
1936 * in setup_carry_lp and we count each edge exactly once.
1938 * Otherwise, we count as follows
1939 * validity -> 1 (>= 0)
1940 * validity+proximity -> 2 (>= 0 and upper bound)
1941 * proximity -> 2 (lower and upper bound)
1942 * local(+any) -> 2 (>= 0 and <= 0)
1944 * If an edge is only marked conditional_validity then it counts
1945 * as zero since it is only checked afterwards.
1947 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
1948 * Otherwise, we ignore them.
1950 static int edge_multiplicity(struct isl_sched_edge *edge, int carry,
1951 int use_coincidence)
1953 if (carry && !edge->validity && !edge->conditional_validity)
1954 return 0;
1955 if (carry)
1956 return 1;
1957 if (edge->proximity || edge->local)
1958 return 2;
1959 if (use_coincidence && edge->coincidence)
1960 return 2;
1961 if (edge->validity)
1962 return 1;
1963 return 0;
1966 /* Count the number of equality and inequality constraints
1967 * that will be added for the given map.
1969 * "use_coincidence" is set if we should take into account coincidence edges.
1971 static int count_map_constraints(struct isl_sched_graph *graph,
1972 struct isl_sched_edge *edge, __isl_take isl_map *map,
1973 int *n_eq, int *n_ineq, int carry, int use_coincidence)
1975 isl_basic_set *coef;
1976 int f = edge_multiplicity(edge, carry, use_coincidence);
1978 if (f == 0) {
1979 isl_map_free(map);
1980 return 0;
1983 if (edge->src == edge->dst)
1984 coef = intra_coefficients(graph, edge->src, map);
1985 else
1986 coef = inter_coefficients(graph, edge, map);
1987 if (!coef)
1988 return -1;
1989 *n_eq += f * coef->n_eq;
1990 *n_ineq += f * coef->n_ineq;
1991 isl_basic_set_free(coef);
1993 return 0;
1996 /* Count the number of equality and inequality constraints
1997 * that will be added to the main lp problem.
1998 * We count as follows
1999 * validity -> 1 (>= 0)
2000 * validity+proximity -> 2 (>= 0 and upper bound)
2001 * proximity -> 2 (lower and upper bound)
2002 * local(+any) -> 2 (>= 0 and <= 0)
2004 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
2005 * Otherwise, we ignore them.
2007 static int count_constraints(struct isl_sched_graph *graph,
2008 int *n_eq, int *n_ineq, int use_coincidence)
2010 int i;
2012 *n_eq = *n_ineq = 0;
2013 for (i = 0; i < graph->n_edge; ++i) {
2014 struct isl_sched_edge *edge= &graph->edge[i];
2015 isl_map *map = isl_map_copy(edge->map);
2017 if (count_map_constraints(graph, edge, map, n_eq, n_ineq,
2018 0, use_coincidence) < 0)
2019 return -1;
2022 return 0;
2025 /* Count the number of constraints that will be added by
2026 * add_bound_coefficient_constraints and increment *n_eq and *n_ineq
2027 * accordingly.
2029 * In practice, add_bound_coefficient_constraints only adds inequalities.
2031 static int count_bound_coefficient_constraints(isl_ctx *ctx,
2032 struct isl_sched_graph *graph, int *n_eq, int *n_ineq)
2034 int i;
2036 if (ctx->opt->schedule_max_coefficient == -1)
2037 return 0;
2039 for (i = 0; i < graph->n; ++i)
2040 *n_ineq += 2 * graph->node[i].nparam + 2 * graph->node[i].nvar;
2042 return 0;
2045 /* Add constraints that bound the values of the variable and parameter
2046 * coefficients of the schedule.
2048 * The maximal value of the coefficients is defined by the option
2049 * 'schedule_max_coefficient'.
2051 static int add_bound_coefficient_constraints(isl_ctx *ctx,
2052 struct isl_sched_graph *graph)
2054 int i, j, k;
2055 int max_coefficient;
2056 int total;
2058 max_coefficient = ctx->opt->schedule_max_coefficient;
2060 if (max_coefficient == -1)
2061 return 0;
2063 total = isl_basic_set_total_dim(graph->lp);
2065 for (i = 0; i < graph->n; ++i) {
2066 struct isl_sched_node *node = &graph->node[i];
2067 for (j = 0; j < 2 * node->nparam + 2 * node->nvar; ++j) {
2068 int dim;
2069 k = isl_basic_set_alloc_inequality(graph->lp);
2070 if (k < 0)
2071 return -1;
2072 dim = 1 + node->start + 1 + j;
2073 isl_seq_clr(graph->lp->ineq[k], 1 + total);
2074 isl_int_set_si(graph->lp->ineq[k][dim], -1);
2075 isl_int_set_si(graph->lp->ineq[k][0], max_coefficient);
2079 return 0;
2082 /* Construct an ILP problem for finding schedule coefficients
2083 * that result in non-negative, but small dependence distances
2084 * over all dependences.
2085 * In particular, the dependence distances over proximity edges
2086 * are bounded by m_0 + m_n n and we compute schedule coefficients
2087 * with small values (preferably zero) of m_n and m_0.
2089 * All variables of the ILP are non-negative. The actual coefficients
2090 * may be negative, so each coefficient is represented as the difference
2091 * of two non-negative variables. The negative part always appears
2092 * immediately before the positive part.
2093 * Other than that, the variables have the following order
2095 * - sum of positive and negative parts of m_n coefficients
2096 * - m_0
2097 * - sum of positive and negative parts of all c_n coefficients
2098 * (unconstrained when computing non-parametric schedules)
2099 * - sum of positive and negative parts of all c_x coefficients
2100 * - positive and negative parts of m_n coefficients
2101 * - for each node
2102 * - c_i_0
2103 * - positive and negative parts of c_i_n (if parametric)
2104 * - positive and negative parts of c_i_x
2106 * The c_i_x are not represented directly, but through the columns of
2107 * node->cmap. That is, the computed values are for variable t_i_x
2108 * such that c_i_x = Q t_i_x with Q equal to node->cmap.
2110 * The constraints are those from the edges plus two or three equalities
2111 * to express the sums.
2113 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
2114 * Otherwise, we ignore them.
2116 static int setup_lp(isl_ctx *ctx, struct isl_sched_graph *graph,
2117 int use_coincidence)
2119 int i, j;
2120 int k;
2121 unsigned nparam;
2122 unsigned total;
2123 isl_space *dim;
2124 int parametric;
2125 int param_pos;
2126 int n_eq, n_ineq;
2127 int max_constant_term;
2129 max_constant_term = ctx->opt->schedule_max_constant_term;
2131 parametric = ctx->opt->schedule_parametric;
2132 nparam = isl_space_dim(graph->node[0].space, isl_dim_param);
2133 param_pos = 4;
2134 total = param_pos + 2 * nparam;
2135 for (i = 0; i < graph->n; ++i) {
2136 struct isl_sched_node *node = &graph->node[graph->sorted[i]];
2137 if (node_update_cmap(node) < 0)
2138 return -1;
2139 node->start = total;
2140 total += 1 + 2 * (node->nparam + node->nvar);
2143 if (count_constraints(graph, &n_eq, &n_ineq, use_coincidence) < 0)
2144 return -1;
2145 if (count_bound_coefficient_constraints(ctx, graph, &n_eq, &n_ineq) < 0)
2146 return -1;
2148 dim = isl_space_set_alloc(ctx, 0, total);
2149 isl_basic_set_free(graph->lp);
2150 n_eq += 2 + parametric;
2151 if (max_constant_term != -1)
2152 n_ineq += graph->n;
2154 graph->lp = isl_basic_set_alloc_space(dim, 0, n_eq, n_ineq);
2156 k = isl_basic_set_alloc_equality(graph->lp);
2157 if (k < 0)
2158 return -1;
2159 isl_seq_clr(graph->lp->eq[k], 1 + total);
2160 isl_int_set_si(graph->lp->eq[k][1], -1);
2161 for (i = 0; i < 2 * nparam; ++i)
2162 isl_int_set_si(graph->lp->eq[k][1 + param_pos + i], 1);
2164 if (parametric) {
2165 k = isl_basic_set_alloc_equality(graph->lp);
2166 if (k < 0)
2167 return -1;
2168 isl_seq_clr(graph->lp->eq[k], 1 + total);
2169 isl_int_set_si(graph->lp->eq[k][3], -1);
2170 for (i = 0; i < graph->n; ++i) {
2171 int pos = 1 + graph->node[i].start + 1;
2173 for (j = 0; j < 2 * graph->node[i].nparam; ++j)
2174 isl_int_set_si(graph->lp->eq[k][pos + j], 1);
2178 k = isl_basic_set_alloc_equality(graph->lp);
2179 if (k < 0)
2180 return -1;
2181 isl_seq_clr(graph->lp->eq[k], 1 + total);
2182 isl_int_set_si(graph->lp->eq[k][4], -1);
2183 for (i = 0; i < graph->n; ++i) {
2184 struct isl_sched_node *node = &graph->node[i];
2185 int pos = 1 + node->start + 1 + 2 * node->nparam;
2187 for (j = 0; j < 2 * node->nvar; ++j)
2188 isl_int_set_si(graph->lp->eq[k][pos + j], 1);
2191 if (max_constant_term != -1)
2192 for (i = 0; i < graph->n; ++i) {
2193 struct isl_sched_node *node = &graph->node[i];
2194 k = isl_basic_set_alloc_inequality(graph->lp);
2195 if (k < 0)
2196 return -1;
2197 isl_seq_clr(graph->lp->ineq[k], 1 + total);
2198 isl_int_set_si(graph->lp->ineq[k][1 + node->start], -1);
2199 isl_int_set_si(graph->lp->ineq[k][0], max_constant_term);
2202 if (add_bound_coefficient_constraints(ctx, graph) < 0)
2203 return -1;
2204 if (add_all_validity_constraints(graph, use_coincidence) < 0)
2205 return -1;
2206 if (add_all_proximity_constraints(graph, use_coincidence) < 0)
2207 return -1;
2209 return 0;
2212 /* Analyze the conflicting constraint found by
2213 * isl_tab_basic_set_non_trivial_lexmin. If it corresponds to the validity
2214 * constraint of one of the edges between distinct nodes, living, moreover
2215 * in distinct SCCs, then record the source and sink SCC as this may
2216 * be a good place to cut between SCCs.
2218 static int check_conflict(int con, void *user)
2220 int i;
2221 struct isl_sched_graph *graph = user;
2223 if (graph->src_scc >= 0)
2224 return 0;
2226 con -= graph->lp->n_eq;
2228 if (con >= graph->lp->n_ineq)
2229 return 0;
2231 for (i = 0; i < graph->n_edge; ++i) {
2232 if (!graph->edge[i].validity)
2233 continue;
2234 if (graph->edge[i].src == graph->edge[i].dst)
2235 continue;
2236 if (graph->edge[i].src->scc == graph->edge[i].dst->scc)
2237 continue;
2238 if (graph->edge[i].start > con)
2239 continue;
2240 if (graph->edge[i].end <= con)
2241 continue;
2242 graph->src_scc = graph->edge[i].src->scc;
2243 graph->dst_scc = graph->edge[i].dst->scc;
2246 return 0;
2249 /* Check whether the next schedule row of the given node needs to be
2250 * non-trivial. Lower-dimensional domains may have some trivial rows,
2251 * but as soon as the number of remaining required non-trivial rows
2252 * is as large as the number or remaining rows to be computed,
2253 * all remaining rows need to be non-trivial.
2255 static int needs_row(struct isl_sched_graph *graph, struct isl_sched_node *node)
2257 return node->nvar - node->rank >= graph->maxvar - graph->n_row;
2260 /* Solve the ILP problem constructed in setup_lp.
2261 * For each node such that all the remaining rows of its schedule
2262 * need to be non-trivial, we construct a non-triviality region.
2263 * This region imposes that the next row is independent of previous rows.
2264 * In particular the coefficients c_i_x are represented by t_i_x
2265 * variables with c_i_x = Q t_i_x and Q a unimodular matrix such that
2266 * its first columns span the rows of the previously computed part
2267 * of the schedule. The non-triviality region enforces that at least
2268 * one of the remaining components of t_i_x is non-zero, i.e.,
2269 * that the new schedule row depends on at least one of the remaining
2270 * columns of Q.
2272 static __isl_give isl_vec *solve_lp(struct isl_sched_graph *graph)
2274 int i;
2275 isl_vec *sol;
2276 isl_basic_set *lp;
2278 for (i = 0; i < graph->n; ++i) {
2279 struct isl_sched_node *node = &graph->node[i];
2280 int skip = node->rank;
2281 graph->region[i].pos = node->start + 1 + 2*(node->nparam+skip);
2282 if (needs_row(graph, node))
2283 graph->region[i].len = 2 * (node->nvar - skip);
2284 else
2285 graph->region[i].len = 0;
2287 lp = isl_basic_set_copy(graph->lp);
2288 sol = isl_tab_basic_set_non_trivial_lexmin(lp, 2, graph->n,
2289 graph->region, &check_conflict, graph);
2290 return sol;
2293 /* Update the schedules of all nodes based on the given solution
2294 * of the LP problem.
2295 * The new row is added to the current band.
2296 * All possibly negative coefficients are encoded as a difference
2297 * of two non-negative variables, so we need to perform the subtraction
2298 * here. Moreover, if use_cmap is set, then the solution does
2299 * not refer to the actual coefficients c_i_x, but instead to variables
2300 * t_i_x such that c_i_x = Q t_i_x and Q is equal to node->cmap.
2301 * In this case, we then also need to perform this multiplication
2302 * to obtain the values of c_i_x.
2304 * If coincident is set, then the caller guarantees that the new
2305 * row satisfies the coincidence constraints.
2307 static int update_schedule(struct isl_sched_graph *graph,
2308 __isl_take isl_vec *sol, int use_cmap, int coincident)
2310 int i, j;
2311 isl_vec *csol = NULL;
2313 if (!sol)
2314 goto error;
2315 if (sol->size == 0)
2316 isl_die(sol->ctx, isl_error_internal,
2317 "no solution found", goto error);
2318 if (graph->n_total_row >= graph->max_row)
2319 isl_die(sol->ctx, isl_error_internal,
2320 "too many schedule rows", goto error);
2322 for (i = 0; i < graph->n; ++i) {
2323 struct isl_sched_node *node = &graph->node[i];
2324 int pos = node->start;
2325 int row = isl_mat_rows(node->sched);
2327 isl_vec_free(csol);
2328 csol = isl_vec_alloc(sol->ctx, node->nvar);
2329 if (!csol)
2330 goto error;
2332 isl_map_free(node->sched_map);
2333 node->sched_map = NULL;
2334 node->sched = isl_mat_add_rows(node->sched, 1);
2335 if (!node->sched)
2336 goto error;
2337 node->sched = isl_mat_set_element(node->sched, row, 0,
2338 sol->el[1 + pos]);
2339 for (j = 0; j < node->nparam + node->nvar; ++j)
2340 isl_int_sub(sol->el[1 + pos + 1 + 2 * j + 1],
2341 sol->el[1 + pos + 1 + 2 * j + 1],
2342 sol->el[1 + pos + 1 + 2 * j]);
2343 for (j = 0; j < node->nparam; ++j)
2344 node->sched = isl_mat_set_element(node->sched,
2345 row, 1 + j, sol->el[1+pos+1+2*j+1]);
2346 for (j = 0; j < node->nvar; ++j)
2347 isl_int_set(csol->el[j],
2348 sol->el[1+pos+1+2*(node->nparam+j)+1]);
2349 if (use_cmap)
2350 csol = isl_mat_vec_product(isl_mat_copy(node->cmap),
2351 csol);
2352 if (!csol)
2353 goto error;
2354 for (j = 0; j < node->nvar; ++j)
2355 node->sched = isl_mat_set_element(node->sched,
2356 row, 1 + node->nparam + j, csol->el[j]);
2357 node->coincident[graph->n_total_row] = coincident;
2359 isl_vec_free(sol);
2360 isl_vec_free(csol);
2362 graph->n_row++;
2363 graph->n_total_row++;
2365 return 0;
2366 error:
2367 isl_vec_free(sol);
2368 isl_vec_free(csol);
2369 return -1;
2372 /* Convert row "row" of node->sched into an isl_aff living in "ls"
2373 * and return this isl_aff.
2375 static __isl_give isl_aff *extract_schedule_row(__isl_take isl_local_space *ls,
2376 struct isl_sched_node *node, int row)
2378 int j;
2379 isl_int v;
2380 isl_aff *aff;
2382 isl_int_init(v);
2384 aff = isl_aff_zero_on_domain(ls);
2385 isl_mat_get_element(node->sched, row, 0, &v);
2386 aff = isl_aff_set_constant(aff, v);
2387 for (j = 0; j < node->nparam; ++j) {
2388 isl_mat_get_element(node->sched, row, 1 + j, &v);
2389 aff = isl_aff_set_coefficient(aff, isl_dim_param, j, v);
2391 for (j = 0; j < node->nvar; ++j) {
2392 isl_mat_get_element(node->sched, row, 1 + node->nparam + j, &v);
2393 aff = isl_aff_set_coefficient(aff, isl_dim_in, j, v);
2396 isl_int_clear(v);
2398 return aff;
2401 /* Convert the "n" rows starting at "first" of node->sched into a multi_aff
2402 * and return this multi_aff.
2404 * The result is defined over the uncompressed node domain.
2406 static __isl_give isl_multi_aff *node_extract_partial_schedule_multi_aff(
2407 struct isl_sched_node *node, int first, int n)
2409 int i;
2410 isl_space *space;
2411 isl_local_space *ls;
2412 isl_aff *aff;
2413 isl_multi_aff *ma;
2414 int nrow;
2416 nrow = isl_mat_rows(node->sched);
2417 if (node->compressed)
2418 space = isl_multi_aff_get_domain_space(node->decompress);
2419 else
2420 space = isl_space_copy(node->space);
2421 ls = isl_local_space_from_space(isl_space_copy(space));
2422 space = isl_space_from_domain(space);
2423 space = isl_space_add_dims(space, isl_dim_out, n);
2424 ma = isl_multi_aff_zero(space);
2426 for (i = first; i < first + n; ++i) {
2427 aff = extract_schedule_row(isl_local_space_copy(ls), node, i);
2428 ma = isl_multi_aff_set_aff(ma, i - first, aff);
2431 isl_local_space_free(ls);
2433 if (node->compressed)
2434 ma = isl_multi_aff_pullback_multi_aff(ma,
2435 isl_multi_aff_copy(node->compress));
2437 return ma;
2440 /* Convert node->sched into a multi_aff and return this multi_aff.
2442 * The result is defined over the uncompressed node domain.
2444 static __isl_give isl_multi_aff *node_extract_schedule_multi_aff(
2445 struct isl_sched_node *node)
2447 int nrow;
2449 nrow = isl_mat_rows(node->sched);
2450 return node_extract_partial_schedule_multi_aff(node, 0, nrow);
2453 /* Convert node->sched into a map and return this map.
2455 * The result is cached in node->sched_map, which needs to be released
2456 * whenever node->sched is updated.
2457 * It is defined over the uncompressed node domain.
2459 static __isl_give isl_map *node_extract_schedule(struct isl_sched_node *node)
2461 if (!node->sched_map) {
2462 isl_multi_aff *ma;
2464 ma = node_extract_schedule_multi_aff(node);
2465 node->sched_map = isl_map_from_multi_aff(ma);
2468 return isl_map_copy(node->sched_map);
2471 /* Construct a map that can be used to update a dependence relation
2472 * based on the current schedule.
2473 * That is, construct a map expressing that source and sink
2474 * are executed within the same iteration of the current schedule.
2475 * This map can then be intersected with the dependence relation.
2476 * This is not the most efficient way, but this shouldn't be a critical
2477 * operation.
2479 static __isl_give isl_map *specializer(struct isl_sched_node *src,
2480 struct isl_sched_node *dst)
2482 isl_map *src_sched, *dst_sched;
2484 src_sched = node_extract_schedule(src);
2485 dst_sched = node_extract_schedule(dst);
2486 return isl_map_apply_range(src_sched, isl_map_reverse(dst_sched));
2489 /* Intersect the domains of the nested relations in domain and range
2490 * of "umap" with "map".
2492 static __isl_give isl_union_map *intersect_domains(
2493 __isl_take isl_union_map *umap, __isl_keep isl_map *map)
2495 isl_union_set *uset;
2497 umap = isl_union_map_zip(umap);
2498 uset = isl_union_set_from_set(isl_map_wrap(isl_map_copy(map)));
2499 umap = isl_union_map_intersect_domain(umap, uset);
2500 umap = isl_union_map_zip(umap);
2501 return umap;
2504 /* Update the dependence relation of the given edge based
2505 * on the current schedule.
2506 * If the dependence is carried completely by the current schedule, then
2507 * it is removed from the edge_tables. It is kept in the list of edges
2508 * as otherwise all edge_tables would have to be recomputed.
2510 static int update_edge(struct isl_sched_graph *graph,
2511 struct isl_sched_edge *edge)
2513 int empty;
2514 isl_map *id;
2516 id = specializer(edge->src, edge->dst);
2517 edge->map = isl_map_intersect(edge->map, isl_map_copy(id));
2518 if (!edge->map)
2519 goto error;
2521 if (edge->tagged_condition) {
2522 edge->tagged_condition =
2523 intersect_domains(edge->tagged_condition, id);
2524 if (!edge->tagged_condition)
2525 goto error;
2527 if (edge->tagged_validity) {
2528 edge->tagged_validity =
2529 intersect_domains(edge->tagged_validity, id);
2530 if (!edge->tagged_validity)
2531 goto error;
2534 empty = isl_map_plain_is_empty(edge->map);
2535 if (empty < 0)
2536 goto error;
2537 if (empty)
2538 graph_remove_edge(graph, edge);
2540 isl_map_free(id);
2541 return 0;
2542 error:
2543 isl_map_free(id);
2544 return -1;
2547 /* Does the domain of "umap" intersect "uset"?
2549 static int domain_intersects(__isl_keep isl_union_map *umap,
2550 __isl_keep isl_union_set *uset)
2552 int empty;
2554 umap = isl_union_map_copy(umap);
2555 umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(uset));
2556 empty = isl_union_map_is_empty(umap);
2557 isl_union_map_free(umap);
2559 return empty < 0 ? -1 : !empty;
2562 /* Does the range of "umap" intersect "uset"?
2564 static int range_intersects(__isl_keep isl_union_map *umap,
2565 __isl_keep isl_union_set *uset)
2567 int empty;
2569 umap = isl_union_map_copy(umap);
2570 umap = isl_union_map_intersect_range(umap, isl_union_set_copy(uset));
2571 empty = isl_union_map_is_empty(umap);
2572 isl_union_map_free(umap);
2574 return empty < 0 ? -1 : !empty;
2577 /* Are the condition dependences of "edge" local with respect to
2578 * the current schedule?
2580 * That is, are domain and range of the condition dependences mapped
2581 * to the same point?
2583 * In other words, is the condition false?
2585 static int is_condition_false(struct isl_sched_edge *edge)
2587 isl_union_map *umap;
2588 isl_map *map, *sched, *test;
2589 int empty, local;
2591 empty = isl_union_map_is_empty(edge->tagged_condition);
2592 if (empty < 0 || empty)
2593 return empty;
2595 umap = isl_union_map_copy(edge->tagged_condition);
2596 umap = isl_union_map_zip(umap);
2597 umap = isl_union_set_unwrap(isl_union_map_domain(umap));
2598 map = isl_map_from_union_map(umap);
2600 sched = node_extract_schedule(edge->src);
2601 map = isl_map_apply_domain(map, sched);
2602 sched = node_extract_schedule(edge->dst);
2603 map = isl_map_apply_range(map, sched);
2605 test = isl_map_identity(isl_map_get_space(map));
2606 local = isl_map_is_subset(map, test);
2607 isl_map_free(map);
2608 isl_map_free(test);
2610 return local;
2613 /* For each conditional validity constraint that is adjacent
2614 * to a condition with domain in condition_source or range in condition_sink,
2615 * turn it into an unconditional validity constraint.
2617 static int unconditionalize_adjacent_validity(struct isl_sched_graph *graph,
2618 __isl_take isl_union_set *condition_source,
2619 __isl_take isl_union_set *condition_sink)
2621 int i;
2623 condition_source = isl_union_set_coalesce(condition_source);
2624 condition_sink = isl_union_set_coalesce(condition_sink);
2626 for (i = 0; i < graph->n_edge; ++i) {
2627 int adjacent;
2628 isl_union_map *validity;
2630 if (!graph->edge[i].conditional_validity)
2631 continue;
2632 if (graph->edge[i].validity)
2633 continue;
2635 validity = graph->edge[i].tagged_validity;
2636 adjacent = domain_intersects(validity, condition_sink);
2637 if (adjacent >= 0 && !adjacent)
2638 adjacent = range_intersects(validity, condition_source);
2639 if (adjacent < 0)
2640 goto error;
2641 if (!adjacent)
2642 continue;
2644 graph->edge[i].validity = 1;
2647 isl_union_set_free(condition_source);
2648 isl_union_set_free(condition_sink);
2649 return 0;
2650 error:
2651 isl_union_set_free(condition_source);
2652 isl_union_set_free(condition_sink);
2653 return -1;
2656 /* Update the dependence relations of all edges based on the current schedule
2657 * and enforce conditional validity constraints that are adjacent
2658 * to satisfied condition constraints.
2660 * First check if any of the condition constraints are satisfied
2661 * (i.e., not local to the outer schedule) and keep track of
2662 * their domain and range.
2663 * Then update all dependence relations (which removes the non-local
2664 * constraints).
2665 * Finally, if any condition constraints turned out to be satisfied,
2666 * then turn all adjacent conditional validity constraints into
2667 * unconditional validity constraints.
2669 static int update_edges(isl_ctx *ctx, struct isl_sched_graph *graph)
2671 int i;
2672 int any = 0;
2673 isl_union_set *source, *sink;
2675 source = isl_union_set_empty(isl_space_params_alloc(ctx, 0));
2676 sink = isl_union_set_empty(isl_space_params_alloc(ctx, 0));
2677 for (i = 0; i < graph->n_edge; ++i) {
2678 int local;
2679 isl_union_set *uset;
2680 isl_union_map *umap;
2682 if (!graph->edge[i].condition)
2683 continue;
2684 if (graph->edge[i].local)
2685 continue;
2686 local = is_condition_false(&graph->edge[i]);
2687 if (local < 0)
2688 goto error;
2689 if (local)
2690 continue;
2692 any = 1;
2694 umap = isl_union_map_copy(graph->edge[i].tagged_condition);
2695 uset = isl_union_map_domain(umap);
2696 source = isl_union_set_union(source, uset);
2698 umap = isl_union_map_copy(graph->edge[i].tagged_condition);
2699 uset = isl_union_map_range(umap);
2700 sink = isl_union_set_union(sink, uset);
2703 for (i = graph->n_edge - 1; i >= 0; --i) {
2704 if (update_edge(graph, &graph->edge[i]) < 0)
2705 goto error;
2708 if (any)
2709 return unconditionalize_adjacent_validity(graph, source, sink);
2711 isl_union_set_free(source);
2712 isl_union_set_free(sink);
2713 return 0;
2714 error:
2715 isl_union_set_free(source);
2716 isl_union_set_free(sink);
2717 return -1;
2720 static void next_band(struct isl_sched_graph *graph)
2722 graph->band_start = graph->n_total_row;
2725 /* Return the union of the universe domains of the nodes in "graph"
2726 * that satisfy "pred".
2728 static __isl_give isl_union_set *isl_sched_graph_domain(isl_ctx *ctx,
2729 struct isl_sched_graph *graph,
2730 int (*pred)(struct isl_sched_node *node, int data), int data)
2732 int i;
2733 isl_set *set;
2734 isl_union_set *dom;
2736 for (i = 0; i < graph->n; ++i)
2737 if (pred(&graph->node[i], data))
2738 break;
2740 if (i >= graph->n)
2741 isl_die(ctx, isl_error_internal,
2742 "empty component", return NULL);
2744 set = isl_set_universe(isl_space_copy(graph->node[i].space));
2745 dom = isl_union_set_from_set(set);
2747 for (i = i + 1; i < graph->n; ++i) {
2748 if (!pred(&graph->node[i], data))
2749 continue;
2750 set = isl_set_universe(isl_space_copy(graph->node[i].space));
2751 dom = isl_union_set_union(dom, isl_union_set_from_set(set));
2754 return dom;
2757 /* Return a list of unions of universe domains, where each element
2758 * in the list corresponds to an SCC (or WCC) indexed by node->scc.
2760 static __isl_give isl_union_set_list *extract_sccs(isl_ctx *ctx,
2761 struct isl_sched_graph *graph)
2763 int i;
2764 isl_union_set_list *filters;
2766 filters = isl_union_set_list_alloc(ctx, graph->scc);
2767 for (i = 0; i < graph->scc; ++i) {
2768 isl_union_set *dom;
2770 dom = isl_sched_graph_domain(ctx, graph, &node_scc_exactly, i);
2771 filters = isl_union_set_list_add(filters, dom);
2774 return filters;
2777 /* Return a list of two unions of universe domains, one for the SCCs up
2778 * to and including graph->src_scc and another for the other SCCS.
2780 static __isl_give isl_union_set_list *extract_split(isl_ctx *ctx,
2781 struct isl_sched_graph *graph)
2783 isl_union_set *dom;
2784 isl_union_set_list *filters;
2786 filters = isl_union_set_list_alloc(ctx, 2);
2787 dom = isl_sched_graph_domain(ctx, graph,
2788 &node_scc_at_most, graph->src_scc);
2789 filters = isl_union_set_list_add(filters, dom);
2790 dom = isl_sched_graph_domain(ctx, graph,
2791 &node_scc_at_least, graph->src_scc + 1);
2792 filters = isl_union_set_list_add(filters, dom);
2794 return filters;
2797 /* Topologically sort statements mapped to the same schedule iteration
2798 * and add insert a sequence node in front of "node"
2799 * corresponding to this order.
2801 static __isl_give isl_schedule_node *sort_statements(
2802 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph)
2804 isl_ctx *ctx;
2805 isl_union_set_list *filters;
2807 if (!node)
2808 return NULL;
2810 ctx = isl_schedule_node_get_ctx(node);
2811 if (graph->n < 1)
2812 isl_die(ctx, isl_error_internal,
2813 "graph should have at least one node",
2814 return isl_schedule_node_free(node));
2816 if (graph->n == 1)
2817 return node;
2819 if (update_edges(ctx, graph) < 0)
2820 return isl_schedule_node_free(node);
2822 if (graph->n_edge == 0)
2823 return node;
2825 if (detect_sccs(ctx, graph) < 0)
2826 return isl_schedule_node_free(node);
2828 filters = extract_sccs(ctx, graph);
2829 node = isl_schedule_node_insert_sequence(node, filters);
2831 return node;
2834 /* Copy nodes that satisfy node_pred from the src dependence graph
2835 * to the dst dependence graph.
2837 static int copy_nodes(struct isl_sched_graph *dst, struct isl_sched_graph *src,
2838 int (*node_pred)(struct isl_sched_node *node, int data), int data)
2840 int i;
2842 dst->n = 0;
2843 for (i = 0; i < src->n; ++i) {
2844 int j;
2846 if (!node_pred(&src->node[i], data))
2847 continue;
2849 j = dst->n;
2850 dst->node[j].space = isl_space_copy(src->node[i].space);
2851 dst->node[j].compressed = src->node[i].compressed;
2852 dst->node[j].hull = isl_set_copy(src->node[i].hull);
2853 dst->node[j].compress =
2854 isl_multi_aff_copy(src->node[i].compress);
2855 dst->node[j].decompress =
2856 isl_multi_aff_copy(src->node[i].decompress);
2857 dst->node[j].nvar = src->node[i].nvar;
2858 dst->node[j].nparam = src->node[i].nparam;
2859 dst->node[j].sched = isl_mat_copy(src->node[i].sched);
2860 dst->node[j].sched_map = isl_map_copy(src->node[i].sched_map);
2861 dst->node[j].coincident = src->node[i].coincident;
2862 dst->n++;
2864 if (!dst->node[j].space || !dst->node[j].sched)
2865 return -1;
2866 if (dst->node[j].compressed &&
2867 (!dst->node[j].hull || !dst->node[j].compress ||
2868 !dst->node[j].decompress))
2869 return -1;
2872 return 0;
2875 /* Copy non-empty edges that satisfy edge_pred from the src dependence graph
2876 * to the dst dependence graph.
2877 * If the source or destination node of the edge is not in the destination
2878 * graph, then it must be a backward proximity edge and it should simply
2879 * be ignored.
2881 static int copy_edges(isl_ctx *ctx, struct isl_sched_graph *dst,
2882 struct isl_sched_graph *src,
2883 int (*edge_pred)(struct isl_sched_edge *edge, int data), int data)
2885 int i;
2886 enum isl_edge_type t;
2888 dst->n_edge = 0;
2889 for (i = 0; i < src->n_edge; ++i) {
2890 struct isl_sched_edge *edge = &src->edge[i];
2891 isl_map *map;
2892 isl_union_map *tagged_condition;
2893 isl_union_map *tagged_validity;
2894 struct isl_sched_node *dst_src, *dst_dst;
2896 if (!edge_pred(edge, data))
2897 continue;
2899 if (isl_map_plain_is_empty(edge->map))
2900 continue;
2902 dst_src = graph_find_node(ctx, dst, edge->src->space);
2903 dst_dst = graph_find_node(ctx, dst, edge->dst->space);
2904 if (!dst_src || !dst_dst) {
2905 if (edge->validity || edge->conditional_validity)
2906 isl_die(ctx, isl_error_internal,
2907 "backward (conditional) validity edge",
2908 return -1);
2909 continue;
2912 map = isl_map_copy(edge->map);
2913 tagged_condition = isl_union_map_copy(edge->tagged_condition);
2914 tagged_validity = isl_union_map_copy(edge->tagged_validity);
2916 dst->edge[dst->n_edge].src = dst_src;
2917 dst->edge[dst->n_edge].dst = dst_dst;
2918 dst->edge[dst->n_edge].map = map;
2919 dst->edge[dst->n_edge].tagged_condition = tagged_condition;
2920 dst->edge[dst->n_edge].tagged_validity = tagged_validity;
2921 dst->edge[dst->n_edge].validity = edge->validity;
2922 dst->edge[dst->n_edge].proximity = edge->proximity;
2923 dst->edge[dst->n_edge].coincidence = edge->coincidence;
2924 dst->edge[dst->n_edge].condition = edge->condition;
2925 dst->edge[dst->n_edge].conditional_validity =
2926 edge->conditional_validity;
2927 dst->n_edge++;
2929 if (edge->tagged_condition && !tagged_condition)
2930 return -1;
2931 if (edge->tagged_validity && !tagged_validity)
2932 return -1;
2934 for (t = isl_edge_first; t <= isl_edge_last; ++t) {
2935 if (edge !=
2936 graph_find_edge(src, t, edge->src, edge->dst))
2937 continue;
2938 if (graph_edge_table_add(ctx, dst, t,
2939 &dst->edge[dst->n_edge - 1]) < 0)
2940 return -1;
2944 return 0;
2947 /* Compute the maximal number of variables over all nodes.
2948 * This is the maximal number of linearly independent schedule
2949 * rows that we need to compute.
2950 * Just in case we end up in a part of the dependence graph
2951 * with only lower-dimensional domains, we make sure we will
2952 * compute the required amount of extra linearly independent rows.
2954 static int compute_maxvar(struct isl_sched_graph *graph)
2956 int i;
2958 graph->maxvar = 0;
2959 for (i = 0; i < graph->n; ++i) {
2960 struct isl_sched_node *node = &graph->node[i];
2961 int nvar;
2963 if (node_update_cmap(node) < 0)
2964 return -1;
2965 nvar = node->nvar + graph->n_row - node->rank;
2966 if (nvar > graph->maxvar)
2967 graph->maxvar = nvar;
2970 return 0;
2973 static __isl_give isl_schedule_node *compute_schedule(isl_schedule_node *node,
2974 struct isl_sched_graph *graph);
2975 static __isl_give isl_schedule_node *compute_schedule_wcc(
2976 isl_schedule_node *node, struct isl_sched_graph *graph);
2978 /* Compute a schedule for a subgraph of "graph". In particular, for
2979 * the graph composed of nodes that satisfy node_pred and edges that
2980 * that satisfy edge_pred. The caller should precompute the number
2981 * of nodes and edges that satisfy these predicates and pass them along
2982 * as "n" and "n_edge".
2983 * If the subgraph is known to consist of a single component, then wcc should
2984 * be set and then we call compute_schedule_wcc on the constructed subgraph.
2985 * Otherwise, we call compute_schedule, which will check whether the subgraph
2986 * is connected.
2988 * The schedule is inserted at "node" and the updated schedule node
2989 * is returned.
2991 static __isl_give isl_schedule_node *compute_sub_schedule(
2992 __isl_take isl_schedule_node *node, isl_ctx *ctx,
2993 struct isl_sched_graph *graph, int n, int n_edge,
2994 int (*node_pred)(struct isl_sched_node *node, int data),
2995 int (*edge_pred)(struct isl_sched_edge *edge, int data),
2996 int data, int wcc)
2998 struct isl_sched_graph split = { 0 };
2999 int t;
3001 if (graph_alloc(ctx, &split, n, n_edge) < 0)
3002 goto error;
3003 if (copy_nodes(&split, graph, node_pred, data) < 0)
3004 goto error;
3005 if (graph_init_table(ctx, &split) < 0)
3006 goto error;
3007 for (t = 0; t <= isl_edge_last; ++t)
3008 split.max_edge[t] = graph->max_edge[t];
3009 if (graph_init_edge_tables(ctx, &split) < 0)
3010 goto error;
3011 if (copy_edges(ctx, &split, graph, edge_pred, data) < 0)
3012 goto error;
3013 split.n_row = graph->n_row;
3014 split.max_row = graph->max_row;
3015 split.n_total_row = graph->n_total_row;
3016 split.band_start = graph->band_start;
3018 if (wcc)
3019 node = compute_schedule_wcc(node, &split);
3020 else
3021 node = compute_schedule(node, &split);
3023 graph_free(ctx, &split);
3024 return node;
3025 error:
3026 graph_free(ctx, &split);
3027 return isl_schedule_node_free(node);
3030 static int edge_scc_exactly(struct isl_sched_edge *edge, int scc)
3032 return edge->src->scc == scc && edge->dst->scc == scc;
3035 static int edge_dst_scc_at_most(struct isl_sched_edge *edge, int scc)
3037 return edge->dst->scc <= scc;
3040 static int edge_src_scc_at_least(struct isl_sched_edge *edge, int scc)
3042 return edge->src->scc >= scc;
3045 /* Reset the current band by dropping all its schedule rows.
3047 static int reset_band(struct isl_sched_graph *graph)
3049 int i;
3050 int drop;
3052 drop = graph->n_total_row - graph->band_start;
3053 graph->n_total_row -= drop;
3054 graph->n_row -= drop;
3056 for (i = 0; i < graph->n; ++i) {
3057 struct isl_sched_node *node = &graph->node[i];
3059 isl_map_free(node->sched_map);
3060 node->sched_map = NULL;
3062 node->sched = isl_mat_drop_rows(node->sched,
3063 graph->band_start, drop);
3065 if (!node->sched)
3066 return -1;
3069 return 0;
3072 /* Split the current graph into two parts and compute a schedule for each
3073 * part individually. In particular, one part consists of all SCCs up
3074 * to and including graph->src_scc, while the other part contains the other
3075 * SCCS. The split is enforced by a sequence node inserted at position "node"
3076 * in the schedule tree. Return the updated schedule node.
3078 * The current band is reset. It would be possible to reuse
3079 * the previously computed rows as the first rows in the next
3080 * band, but recomputing them may result in better rows as we are looking
3081 * at a smaller part of the dependence graph.
3083 static __isl_give isl_schedule_node *compute_split_schedule(
3084 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph)
3086 int i, n, e1, e2;
3087 int orig_total_row;
3088 isl_ctx *ctx;
3089 isl_union_set_list *filters;
3091 if (!node)
3092 return NULL;
3094 if (reset_band(graph) < 0)
3095 return isl_schedule_node_free(node);
3097 n = 0;
3098 for (i = 0; i < graph->n; ++i) {
3099 struct isl_sched_node *node = &graph->node[i];
3100 int before = node->scc <= graph->src_scc;
3102 if (before)
3103 n++;
3106 e1 = e2 = 0;
3107 for (i = 0; i < graph->n_edge; ++i) {
3108 if (graph->edge[i].dst->scc <= graph->src_scc)
3109 e1++;
3110 if (graph->edge[i].src->scc > graph->src_scc)
3111 e2++;
3114 next_band(graph);
3116 ctx = isl_schedule_node_get_ctx(node);
3117 filters = extract_split(ctx, graph);
3118 node = isl_schedule_node_insert_sequence(node, filters);
3119 node = isl_schedule_node_child(node, 0);
3120 node = isl_schedule_node_child(node, 0);
3122 orig_total_row = graph->n_total_row;
3123 node = compute_sub_schedule(node, ctx, graph, n, e1,
3124 &node_scc_at_most, &edge_dst_scc_at_most,
3125 graph->src_scc, 0);
3126 node = isl_schedule_node_parent(node);
3127 node = isl_schedule_node_next_sibling(node);
3128 node = isl_schedule_node_child(node, 0);
3129 graph->n_total_row = orig_total_row;
3130 node = compute_sub_schedule(node, ctx, graph, graph->n - n, e2,
3131 &node_scc_at_least, &edge_src_scc_at_least,
3132 graph->src_scc + 1, 0);
3133 node = isl_schedule_node_parent(node);
3134 node = isl_schedule_node_parent(node);
3136 return node;
3139 /* Insert a band node at position "node" in the schedule tree corresponding
3140 * to the current band in "graph". Mark the band node permutable
3141 * if "permutable" is set.
3142 * The partial schedules and the coincidence property are extracted
3143 * from the graph nodes.
3144 * Return the updated schedule node.
3146 static __isl_give isl_schedule_node *insert_current_band(
3147 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph,
3148 int permutable)
3150 int i;
3151 int start, end, n;
3152 isl_multi_aff *ma;
3153 isl_multi_pw_aff *mpa;
3154 isl_multi_union_pw_aff *mupa;
3156 if (!node)
3157 return NULL;
3159 if (graph->n < 1)
3160 isl_die(isl_schedule_node_get_ctx(node), isl_error_internal,
3161 "graph should have at least one node",
3162 return isl_schedule_node_free(node));
3164 start = graph->band_start;
3165 end = graph->n_total_row;
3166 n = end - start;
3168 ma = node_extract_partial_schedule_multi_aff(&graph->node[0], start, n);
3169 mpa = isl_multi_pw_aff_from_multi_aff(ma);
3170 mupa = isl_multi_union_pw_aff_from_multi_pw_aff(mpa);
3172 for (i = 1; i < graph->n; ++i) {
3173 isl_multi_union_pw_aff *mupa_i;
3175 ma = node_extract_partial_schedule_multi_aff(&graph->node[i],
3176 start, n);
3177 mpa = isl_multi_pw_aff_from_multi_aff(ma);
3178 mupa_i = isl_multi_union_pw_aff_from_multi_pw_aff(mpa);
3179 mupa = isl_multi_union_pw_aff_union_add(mupa, mupa_i);
3181 node = isl_schedule_node_insert_partial_schedule(node, mupa);
3183 for (i = 0; i < n; ++i)
3184 node = isl_schedule_node_band_member_set_coincident(node, i,
3185 graph->node[0].coincident[start + i]);
3186 node = isl_schedule_node_band_set_permutable(node, permutable);
3188 return node;
3191 /* Update the dependence relations based on the current schedule,
3192 * add the current band to "node" and the continue with the computation
3193 * of the next band.
3194 * Return the updated schedule node.
3196 static __isl_give isl_schedule_node *compute_next_band(
3197 __isl_take isl_schedule_node *node,
3198 struct isl_sched_graph *graph, int permutable)
3200 isl_ctx *ctx;
3202 if (!node)
3203 return NULL;
3205 ctx = isl_schedule_node_get_ctx(node);
3206 if (update_edges(ctx, graph) < 0)
3207 return isl_schedule_node_free(node);
3208 node = insert_current_band(node, graph, permutable);
3209 next_band(graph);
3211 node = isl_schedule_node_child(node, 0);
3212 node = compute_schedule(node, graph);
3213 node = isl_schedule_node_parent(node);
3215 return node;
3218 /* Add constraints to graph->lp that force the dependence "map" (which
3219 * is part of the dependence relation of "edge")
3220 * to be respected and attempt to carry it, where the edge is one from
3221 * a node j to itself. "pos" is the sequence number of the given map.
3222 * That is, add constraints that enforce
3224 * (c_j_0 + c_j_n n + c_j_x y) - (c_j_0 + c_j_n n + c_j_x x)
3225 * = c_j_x (y - x) >= e_i
3227 * for each (x,y) in R.
3228 * We obtain general constraints on coefficients (c_0, c_n, c_x)
3229 * of valid constraints for (y - x) and then plug in (-e_i, 0, c_j_x),
3230 * with each coefficient in c_j_x represented as a pair of non-negative
3231 * coefficients.
3233 static int add_intra_constraints(struct isl_sched_graph *graph,
3234 struct isl_sched_edge *edge, __isl_take isl_map *map, int pos)
3236 unsigned total;
3237 isl_ctx *ctx = isl_map_get_ctx(map);
3238 isl_space *dim;
3239 isl_dim_map *dim_map;
3240 isl_basic_set *coef;
3241 struct isl_sched_node *node = edge->src;
3243 coef = intra_coefficients(graph, node, map);
3244 if (!coef)
3245 return -1;
3247 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
3249 total = isl_basic_set_total_dim(graph->lp);
3250 dim_map = isl_dim_map_alloc(ctx, total);
3251 isl_dim_map_range(dim_map, 3 + pos, 0, 0, 0, 1, -1);
3252 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 1, 2,
3253 isl_space_dim(dim, isl_dim_set), 1,
3254 node->nvar, -1);
3255 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 2, 2,
3256 isl_space_dim(dim, isl_dim_set), 1,
3257 node->nvar, 1);
3258 graph->lp = isl_basic_set_extend_constraints(graph->lp,
3259 coef->n_eq, coef->n_ineq);
3260 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
3261 coef, dim_map);
3262 isl_space_free(dim);
3264 return 0;
3267 /* Add constraints to graph->lp that force the dependence "map" (which
3268 * is part of the dependence relation of "edge")
3269 * to be respected and attempt to carry it, where the edge is one from
3270 * node j to node k. "pos" is the sequence number of the given map.
3271 * That is, add constraints that enforce
3273 * (c_k_0 + c_k_n n + c_k_x y) - (c_j_0 + c_j_n n + c_j_x x) >= e_i
3275 * for each (x,y) in R.
3276 * We obtain general constraints on coefficients (c_0, c_n, c_x)
3277 * of valid constraints for R and then plug in
3278 * (-e_i + c_k_0 - c_j_0, c_k_n - c_j_n, c_k_x - c_j_x)
3279 * with each coefficient (except e_i, c_k_0 and c_j_0)
3280 * represented as a pair of non-negative coefficients.
3282 static int add_inter_constraints(struct isl_sched_graph *graph,
3283 struct isl_sched_edge *edge, __isl_take isl_map *map, int pos)
3285 unsigned total;
3286 isl_ctx *ctx = isl_map_get_ctx(map);
3287 isl_space *dim;
3288 isl_dim_map *dim_map;
3289 isl_basic_set *coef;
3290 struct isl_sched_node *src = edge->src;
3291 struct isl_sched_node *dst = edge->dst;
3293 coef = inter_coefficients(graph, edge, map);
3294 if (!coef)
3295 return -1;
3297 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
3299 total = isl_basic_set_total_dim(graph->lp);
3300 dim_map = isl_dim_map_alloc(ctx, total);
3302 isl_dim_map_range(dim_map, 3 + pos, 0, 0, 0, 1, -1);
3304 isl_dim_map_range(dim_map, dst->start, 0, 0, 0, 1, 1);
3305 isl_dim_map_range(dim_map, dst->start + 1, 2, 1, 1, dst->nparam, -1);
3306 isl_dim_map_range(dim_map, dst->start + 2, 2, 1, 1, dst->nparam, 1);
3307 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 1, 2,
3308 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
3309 dst->nvar, -1);
3310 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 2, 2,
3311 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
3312 dst->nvar, 1);
3314 isl_dim_map_range(dim_map, src->start, 0, 0, 0, 1, -1);
3315 isl_dim_map_range(dim_map, src->start + 1, 2, 1, 1, src->nparam, 1);
3316 isl_dim_map_range(dim_map, src->start + 2, 2, 1, 1, src->nparam, -1);
3317 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 1, 2,
3318 isl_space_dim(dim, isl_dim_set), 1,
3319 src->nvar, 1);
3320 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 2, 2,
3321 isl_space_dim(dim, isl_dim_set), 1,
3322 src->nvar, -1);
3324 graph->lp = isl_basic_set_extend_constraints(graph->lp,
3325 coef->n_eq, coef->n_ineq);
3326 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
3327 coef, dim_map);
3328 isl_space_free(dim);
3330 return 0;
3333 /* Add constraints to graph->lp that force all (conditional) validity
3334 * dependences to be respected and attempt to carry them.
3336 static int add_all_constraints(struct isl_sched_graph *graph)
3338 int i, j;
3339 int pos;
3341 pos = 0;
3342 for (i = 0; i < graph->n_edge; ++i) {
3343 struct isl_sched_edge *edge= &graph->edge[i];
3345 if (!edge->validity && !edge->conditional_validity)
3346 continue;
3348 for (j = 0; j < edge->map->n; ++j) {
3349 isl_basic_map *bmap;
3350 isl_map *map;
3352 bmap = isl_basic_map_copy(edge->map->p[j]);
3353 map = isl_map_from_basic_map(bmap);
3355 if (edge->src == edge->dst &&
3356 add_intra_constraints(graph, edge, map, pos) < 0)
3357 return -1;
3358 if (edge->src != edge->dst &&
3359 add_inter_constraints(graph, edge, map, pos) < 0)
3360 return -1;
3361 ++pos;
3365 return 0;
3368 /* Count the number of equality and inequality constraints
3369 * that will be added to the carry_lp problem.
3370 * We count each edge exactly once.
3372 static int count_all_constraints(struct isl_sched_graph *graph,
3373 int *n_eq, int *n_ineq)
3375 int i, j;
3377 *n_eq = *n_ineq = 0;
3378 for (i = 0; i < graph->n_edge; ++i) {
3379 struct isl_sched_edge *edge= &graph->edge[i];
3380 for (j = 0; j < edge->map->n; ++j) {
3381 isl_basic_map *bmap;
3382 isl_map *map;
3384 bmap = isl_basic_map_copy(edge->map->p[j]);
3385 map = isl_map_from_basic_map(bmap);
3387 if (count_map_constraints(graph, edge, map,
3388 n_eq, n_ineq, 1, 0) < 0)
3389 return -1;
3393 return 0;
3396 /* Construct an LP problem for finding schedule coefficients
3397 * such that the schedule carries as many dependences as possible.
3398 * In particular, for each dependence i, we bound the dependence distance
3399 * from below by e_i, with 0 <= e_i <= 1 and then maximize the sum
3400 * of all e_i's. Dependence with e_i = 0 in the solution are simply
3401 * respected, while those with e_i > 0 (in practice e_i = 1) are carried.
3402 * Note that if the dependence relation is a union of basic maps,
3403 * then we have to consider each basic map individually as it may only
3404 * be possible to carry the dependences expressed by some of those
3405 * basic maps and not all off them.
3406 * Below, we consider each of those basic maps as a separate "edge".
3408 * All variables of the LP are non-negative. The actual coefficients
3409 * may be negative, so each coefficient is represented as the difference
3410 * of two non-negative variables. The negative part always appears
3411 * immediately before the positive part.
3412 * Other than that, the variables have the following order
3414 * - sum of (1 - e_i) over all edges
3415 * - sum of positive and negative parts of all c_n coefficients
3416 * (unconstrained when computing non-parametric schedules)
3417 * - sum of positive and negative parts of all c_x coefficients
3418 * - for each edge
3419 * - e_i
3420 * - for each node
3421 * - c_i_0
3422 * - positive and negative parts of c_i_n (if parametric)
3423 * - positive and negative parts of c_i_x
3425 * The constraints are those from the (validity) edges plus three equalities
3426 * to express the sums and n_edge inequalities to express e_i <= 1.
3428 static int setup_carry_lp(isl_ctx *ctx, struct isl_sched_graph *graph)
3430 int i, j;
3431 int k;
3432 isl_space *dim;
3433 unsigned total;
3434 int n_eq, n_ineq;
3435 int n_edge;
3437 n_edge = 0;
3438 for (i = 0; i < graph->n_edge; ++i)
3439 n_edge += graph->edge[i].map->n;
3441 total = 3 + n_edge;
3442 for (i = 0; i < graph->n; ++i) {
3443 struct isl_sched_node *node = &graph->node[graph->sorted[i]];
3444 node->start = total;
3445 total += 1 + 2 * (node->nparam + node->nvar);
3448 if (count_all_constraints(graph, &n_eq, &n_ineq) < 0)
3449 return -1;
3450 if (count_bound_coefficient_constraints(ctx, graph, &n_eq, &n_ineq) < 0)
3451 return -1;
3453 dim = isl_space_set_alloc(ctx, 0, total);
3454 isl_basic_set_free(graph->lp);
3455 n_eq += 3;
3456 n_ineq += n_edge;
3457 graph->lp = isl_basic_set_alloc_space(dim, 0, n_eq, n_ineq);
3458 graph->lp = isl_basic_set_set_rational(graph->lp);
3460 k = isl_basic_set_alloc_equality(graph->lp);
3461 if (k < 0)
3462 return -1;
3463 isl_seq_clr(graph->lp->eq[k], 1 + total);
3464 isl_int_set_si(graph->lp->eq[k][0], -n_edge);
3465 isl_int_set_si(graph->lp->eq[k][1], 1);
3466 for (i = 0; i < n_edge; ++i)
3467 isl_int_set_si(graph->lp->eq[k][4 + i], 1);
3469 k = isl_basic_set_alloc_equality(graph->lp);
3470 if (k < 0)
3471 return -1;
3472 isl_seq_clr(graph->lp->eq[k], 1 + total);
3473 isl_int_set_si(graph->lp->eq[k][2], -1);
3474 for (i = 0; i < graph->n; ++i) {
3475 int pos = 1 + graph->node[i].start + 1;
3477 for (j = 0; j < 2 * graph->node[i].nparam; ++j)
3478 isl_int_set_si(graph->lp->eq[k][pos + j], 1);
3481 k = isl_basic_set_alloc_equality(graph->lp);
3482 if (k < 0)
3483 return -1;
3484 isl_seq_clr(graph->lp->eq[k], 1 + total);
3485 isl_int_set_si(graph->lp->eq[k][3], -1);
3486 for (i = 0; i < graph->n; ++i) {
3487 struct isl_sched_node *node = &graph->node[i];
3488 int pos = 1 + node->start + 1 + 2 * node->nparam;
3490 for (j = 0; j < 2 * node->nvar; ++j)
3491 isl_int_set_si(graph->lp->eq[k][pos + j], 1);
3494 for (i = 0; i < n_edge; ++i) {
3495 k = isl_basic_set_alloc_inequality(graph->lp);
3496 if (k < 0)
3497 return -1;
3498 isl_seq_clr(graph->lp->ineq[k], 1 + total);
3499 isl_int_set_si(graph->lp->ineq[k][4 + i], -1);
3500 isl_int_set_si(graph->lp->ineq[k][0], 1);
3503 if (add_bound_coefficient_constraints(ctx, graph) < 0)
3504 return -1;
3505 if (add_all_constraints(graph) < 0)
3506 return -1;
3508 return 0;
3511 static __isl_give isl_schedule_node *compute_component_schedule(
3512 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph,
3513 int wcc);
3515 /* Comparison function for sorting the statements based on
3516 * the corresponding value in "r".
3518 static int smaller_value(const void *a, const void *b, void *data)
3520 isl_vec *r = data;
3521 const int *i1 = a;
3522 const int *i2 = b;
3524 return isl_int_cmp(r->el[*i1], r->el[*i2]);
3527 /* If the schedule_split_scaled option is set and if the linear
3528 * parts of the scheduling rows for all nodes in the graphs have
3529 * a non-trivial common divisor, then split off the remainder of the
3530 * constant term modulo this common divisor from the linear part.
3531 * Otherwise, insert a band node directly and continue with
3532 * the construction of the schedule.
3534 * If a non-trivial common divisor is found, then
3535 * the linear part is reduced and the remainder is enforced
3536 * by a sequence node with the children placed in the order
3537 * of this remainder.
3538 * In particular, we assign an scc index based on the remainder and
3539 * then rely on compute_component_schedule to insert the sequence and
3540 * to continue the schedule construction on each part.
3542 static __isl_give isl_schedule_node *split_scaled(
3543 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph)
3545 int i;
3546 int row;
3547 int scc;
3548 isl_ctx *ctx;
3549 isl_int gcd, gcd_i;
3550 isl_vec *r;
3551 int *order;
3553 if (!node)
3554 return NULL;
3556 ctx = isl_schedule_node_get_ctx(node);
3557 if (!ctx->opt->schedule_split_scaled)
3558 return compute_next_band(node, graph, 0);
3559 if (graph->n <= 1)
3560 return compute_next_band(node, graph, 0);
3562 isl_int_init(gcd);
3563 isl_int_init(gcd_i);
3565 isl_int_set_si(gcd, 0);
3567 row = isl_mat_rows(graph->node[0].sched) - 1;
3569 for (i = 0; i < graph->n; ++i) {
3570 struct isl_sched_node *node = &graph->node[i];
3571 int cols = isl_mat_cols(node->sched);
3573 isl_seq_gcd(node->sched->row[row] + 1, cols - 1, &gcd_i);
3574 isl_int_gcd(gcd, gcd, gcd_i);
3577 isl_int_clear(gcd_i);
3579 if (isl_int_cmp_si(gcd, 1) <= 0) {
3580 isl_int_clear(gcd);
3581 return compute_next_band(node, graph, 0);
3584 r = isl_vec_alloc(ctx, graph->n);
3585 order = isl_calloc_array(ctx, int, graph->n);
3586 if (!r || !order)
3587 goto error;
3589 for (i = 0; i < graph->n; ++i) {
3590 struct isl_sched_node *node = &graph->node[i];
3592 order[i] = i;
3593 isl_int_fdiv_r(r->el[i], node->sched->row[row][0], gcd);
3594 isl_int_fdiv_q(node->sched->row[row][0],
3595 node->sched->row[row][0], gcd);
3596 isl_int_mul(node->sched->row[row][0],
3597 node->sched->row[row][0], gcd);
3598 node->sched = isl_mat_scale_down_row(node->sched, row, gcd);
3599 if (!node->sched)
3600 goto error;
3603 if (isl_sort(order, graph->n, sizeof(order[0]), &smaller_value, r) < 0)
3604 goto error;
3606 scc = 0;
3607 for (i = 0; i < graph->n; ++i) {
3608 if (i > 0 && isl_int_ne(r->el[order[i - 1]], r->el[order[i]]))
3609 ++scc;
3610 graph->node[order[i]].scc = scc;
3612 graph->scc = ++scc;
3613 graph->weak = 0;
3615 isl_int_clear(gcd);
3616 isl_vec_free(r);
3617 free(order);
3619 if (update_edges(ctx, graph) < 0)
3620 return isl_schedule_node_free(node);
3621 node = insert_current_band(node, graph, 0);
3622 next_band(graph);
3624 node = isl_schedule_node_child(node, 0);
3625 node = compute_component_schedule(node, graph, 0);
3626 node = isl_schedule_node_parent(node);
3628 return node;
3629 error:
3630 isl_vec_free(r);
3631 free(order);
3632 isl_int_clear(gcd);
3633 return isl_schedule_node_free(node);
3636 /* Is the schedule row "sol" trivial on node "node"?
3637 * That is, is the solution zero on the dimensions orthogonal to
3638 * the previously found solutions?
3639 * Return 1 if the solution is trivial, 0 if it is not and -1 on error.
3641 * Each coefficient is represented as the difference between
3642 * two non-negative values in "sol". "sol" has been computed
3643 * in terms of the original iterators (i.e., without use of cmap).
3644 * We construct the schedule row s and write it as a linear
3645 * combination of (linear combinations of) previously computed schedule rows.
3646 * s = Q c or c = U s.
3647 * If the final entries of c are all zero, then the solution is trivial.
3649 static int is_trivial(struct isl_sched_node *node, __isl_keep isl_vec *sol)
3651 int i;
3652 int pos;
3653 int trivial;
3654 isl_ctx *ctx;
3655 isl_vec *node_sol;
3657 if (!sol)
3658 return -1;
3659 if (node->nvar == node->rank)
3660 return 0;
3662 ctx = isl_vec_get_ctx(sol);
3663 node_sol = isl_vec_alloc(ctx, node->nvar);
3664 if (!node_sol)
3665 return -1;
3667 pos = 1 + node->start + 1 + 2 * node->nparam;
3669 for (i = 0; i < node->nvar; ++i)
3670 isl_int_sub(node_sol->el[i],
3671 sol->el[pos + 2 * i + 1], sol->el[pos + 2 * i]);
3673 node_sol = isl_mat_vec_product(isl_mat_copy(node->cinv), node_sol);
3675 if (!node_sol)
3676 return -1;
3678 trivial = isl_seq_first_non_zero(node_sol->el + node->rank,
3679 node->nvar - node->rank) == -1;
3681 isl_vec_free(node_sol);
3683 return trivial;
3686 /* Is the schedule row "sol" trivial on any node where it should
3687 * not be trivial?
3688 * "sol" has been computed in terms of the original iterators
3689 * (i.e., without use of cmap).
3690 * Return 1 if any solution is trivial, 0 if they are not and -1 on error.
3692 static int is_any_trivial(struct isl_sched_graph *graph,
3693 __isl_keep isl_vec *sol)
3695 int i;
3697 for (i = 0; i < graph->n; ++i) {
3698 struct isl_sched_node *node = &graph->node[i];
3699 int trivial;
3701 if (!needs_row(graph, node))
3702 continue;
3703 trivial = is_trivial(node, sol);
3704 if (trivial < 0 || trivial)
3705 return trivial;
3708 return 0;
3711 /* Construct a schedule row for each node such that as many dependences
3712 * as possible are carried and then continue with the next band.
3714 * If the computed schedule row turns out to be trivial on one or
3715 * more nodes where it should not be trivial, then we throw it away
3716 * and try again on each component separately.
3718 * If there is only one component, then we accept the schedule row anyway,
3719 * but we do not consider it as a complete row and therefore do not
3720 * increment graph->n_row. Note that the ranks of the nodes that
3721 * do get a non-trivial schedule part will get updated regardless and
3722 * graph->maxvar is computed based on these ranks. The test for
3723 * whether more schedule rows are required in compute_schedule_wcc
3724 * is therefore not affected.
3726 * Insert a band corresponding to the schedule row at position "node"
3727 * of the schedule tree and continue with the construction of the schedule.
3728 * This insertion and the continued construction is performed by split_scaled
3729 * after optionally checking for non-trivial common divisors.
3731 static __isl_give isl_schedule_node *carry_dependences(
3732 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph)
3734 int i;
3735 int n_edge;
3736 int trivial;
3737 isl_ctx *ctx;
3738 isl_vec *sol;
3739 isl_basic_set *lp;
3741 if (!node)
3742 return NULL;
3744 n_edge = 0;
3745 for (i = 0; i < graph->n_edge; ++i)
3746 n_edge += graph->edge[i].map->n;
3748 ctx = isl_schedule_node_get_ctx(node);
3749 if (setup_carry_lp(ctx, graph) < 0)
3750 return isl_schedule_node_free(node);
3752 lp = isl_basic_set_copy(graph->lp);
3753 sol = isl_tab_basic_set_non_neg_lexmin(lp);
3754 if (!sol)
3755 return isl_schedule_node_free(node);
3757 if (sol->size == 0) {
3758 isl_vec_free(sol);
3759 isl_die(ctx, isl_error_internal,
3760 "error in schedule construction",
3761 return isl_schedule_node_free(node));
3764 isl_int_divexact(sol->el[1], sol->el[1], sol->el[0]);
3765 if (isl_int_cmp_si(sol->el[1], n_edge) >= 0) {
3766 isl_vec_free(sol);
3767 isl_die(ctx, isl_error_unknown,
3768 "unable to carry dependences",
3769 return isl_schedule_node_free(node));
3772 trivial = is_any_trivial(graph, sol);
3773 if (trivial < 0) {
3774 sol = isl_vec_free(sol);
3775 } else if (trivial && graph->scc > 1) {
3776 isl_vec_free(sol);
3777 return compute_component_schedule(node, graph, 1);
3780 if (update_schedule(graph, sol, 0, 0) < 0)
3781 return isl_schedule_node_free(node);
3782 if (trivial)
3783 graph->n_row--;
3785 return split_scaled(node, graph);
3788 /* Are there any (non-empty) (conditional) validity edges in the graph?
3790 static int has_validity_edges(struct isl_sched_graph *graph)
3792 int i;
3794 for (i = 0; i < graph->n_edge; ++i) {
3795 int empty;
3797 empty = isl_map_plain_is_empty(graph->edge[i].map);
3798 if (empty < 0)
3799 return -1;
3800 if (empty)
3801 continue;
3802 if (graph->edge[i].validity ||
3803 graph->edge[i].conditional_validity)
3804 return 1;
3807 return 0;
3810 /* Should we apply a Feautrier step?
3811 * That is, did the user request the Feautrier algorithm and are
3812 * there any validity dependences (left)?
3814 static int need_feautrier_step(isl_ctx *ctx, struct isl_sched_graph *graph)
3816 if (ctx->opt->schedule_algorithm != ISL_SCHEDULE_ALGORITHM_FEAUTRIER)
3817 return 0;
3819 return has_validity_edges(graph);
3822 /* Compute a schedule for a connected dependence graph using Feautrier's
3823 * multi-dimensional scheduling algorithm and return the updated schedule node.
3825 * The original algorithm is described in [1].
3826 * The main idea is to minimize the number of scheduling dimensions, by
3827 * trying to satisfy as many dependences as possible per scheduling dimension.
3829 * [1] P. Feautrier, Some Efficient Solutions to the Affine Scheduling
3830 * Problem, Part II: Multi-Dimensional Time.
3831 * In Intl. Journal of Parallel Programming, 1992.
3833 static __isl_give isl_schedule_node *compute_schedule_wcc_feautrier(
3834 isl_schedule_node *node, struct isl_sched_graph *graph)
3836 return carry_dependences(node, graph);
3839 /* Turn off the "local" bit on all (condition) edges.
3841 static void clear_local_edges(struct isl_sched_graph *graph)
3843 int i;
3845 for (i = 0; i < graph->n_edge; ++i)
3846 if (graph->edge[i].condition)
3847 graph->edge[i].local = 0;
3850 /* Does "graph" have both condition and conditional validity edges?
3852 static int need_condition_check(struct isl_sched_graph *graph)
3854 int i;
3855 int any_condition = 0;
3856 int any_conditional_validity = 0;
3858 for (i = 0; i < graph->n_edge; ++i) {
3859 if (graph->edge[i].condition)
3860 any_condition = 1;
3861 if (graph->edge[i].conditional_validity)
3862 any_conditional_validity = 1;
3865 return any_condition && any_conditional_validity;
3868 /* Does "graph" contain any coincidence edge?
3870 static int has_any_coincidence(struct isl_sched_graph *graph)
3872 int i;
3874 for (i = 0; i < graph->n_edge; ++i)
3875 if (graph->edge[i].coincidence)
3876 return 1;
3878 return 0;
3881 /* Extract the final schedule row as a map with the iteration domain
3882 * of "node" as domain.
3884 static __isl_give isl_map *final_row(struct isl_sched_node *node)
3886 isl_local_space *ls;
3887 isl_aff *aff;
3888 int row;
3890 row = isl_mat_rows(node->sched) - 1;
3891 ls = isl_local_space_from_space(isl_space_copy(node->space));
3892 aff = extract_schedule_row(ls, node, row);
3893 return isl_map_from_aff(aff);
3896 /* Is the conditional validity dependence in the edge with index "edge_index"
3897 * violated by the latest (i.e., final) row of the schedule?
3898 * That is, is i scheduled after j
3899 * for any conditional validity dependence i -> j?
3901 static int is_violated(struct isl_sched_graph *graph, int edge_index)
3903 isl_map *src_sched, *dst_sched, *map;
3904 struct isl_sched_edge *edge = &graph->edge[edge_index];
3905 int empty;
3907 src_sched = final_row(edge->src);
3908 dst_sched = final_row(edge->dst);
3909 map = isl_map_copy(edge->map);
3910 map = isl_map_apply_domain(map, src_sched);
3911 map = isl_map_apply_range(map, dst_sched);
3912 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_out, 0);
3913 empty = isl_map_is_empty(map);
3914 isl_map_free(map);
3916 if (empty < 0)
3917 return -1;
3919 return !empty;
3922 /* Does "graph" have any satisfied condition edges that
3923 * are adjacent to the conditional validity constraint with
3924 * domain "conditional_source" and range "conditional_sink"?
3926 * A satisfied condition is one that is not local.
3927 * If a condition was forced to be local already (i.e., marked as local)
3928 * then there is no need to check if it is in fact local.
3930 * Additionally, mark all adjacent condition edges found as local.
3932 static int has_adjacent_true_conditions(struct isl_sched_graph *graph,
3933 __isl_keep isl_union_set *conditional_source,
3934 __isl_keep isl_union_set *conditional_sink)
3936 int i;
3937 int any = 0;
3939 for (i = 0; i < graph->n_edge; ++i) {
3940 int adjacent, local;
3941 isl_union_map *condition;
3943 if (!graph->edge[i].condition)
3944 continue;
3945 if (graph->edge[i].local)
3946 continue;
3948 condition = graph->edge[i].tagged_condition;
3949 adjacent = domain_intersects(condition, conditional_sink);
3950 if (adjacent >= 0 && !adjacent)
3951 adjacent = range_intersects(condition,
3952 conditional_source);
3953 if (adjacent < 0)
3954 return -1;
3955 if (!adjacent)
3956 continue;
3958 graph->edge[i].local = 1;
3960 local = is_condition_false(&graph->edge[i]);
3961 if (local < 0)
3962 return -1;
3963 if (!local)
3964 any = 1;
3967 return any;
3970 /* Are there any violated conditional validity dependences with
3971 * adjacent condition dependences that are not local with respect
3972 * to the current schedule?
3973 * That is, is the conditional validity constraint violated?
3975 * Additionally, mark all those adjacent condition dependences as local.
3976 * We also mark those adjacent condition dependences that were not marked
3977 * as local before, but just happened to be local already. This ensures
3978 * that they remain local if the schedule is recomputed.
3980 * We first collect domain and range of all violated conditional validity
3981 * dependences and then check if there are any adjacent non-local
3982 * condition dependences.
3984 static int has_violated_conditional_constraint(isl_ctx *ctx,
3985 struct isl_sched_graph *graph)
3987 int i;
3988 int any = 0;
3989 isl_union_set *source, *sink;
3991 source = isl_union_set_empty(isl_space_params_alloc(ctx, 0));
3992 sink = isl_union_set_empty(isl_space_params_alloc(ctx, 0));
3993 for (i = 0; i < graph->n_edge; ++i) {
3994 isl_union_set *uset;
3995 isl_union_map *umap;
3996 int violated;
3998 if (!graph->edge[i].conditional_validity)
3999 continue;
4001 violated = is_violated(graph, i);
4002 if (violated < 0)
4003 goto error;
4004 if (!violated)
4005 continue;
4007 any = 1;
4009 umap = isl_union_map_copy(graph->edge[i].tagged_validity);
4010 uset = isl_union_map_domain(umap);
4011 source = isl_union_set_union(source, uset);
4012 source = isl_union_set_coalesce(source);
4014 umap = isl_union_map_copy(graph->edge[i].tagged_validity);
4015 uset = isl_union_map_range(umap);
4016 sink = isl_union_set_union(sink, uset);
4017 sink = isl_union_set_coalesce(sink);
4020 if (any)
4021 any = has_adjacent_true_conditions(graph, source, sink);
4023 isl_union_set_free(source);
4024 isl_union_set_free(sink);
4025 return any;
4026 error:
4027 isl_union_set_free(source);
4028 isl_union_set_free(sink);
4029 return -1;
4032 /* Compute a schedule for a connected dependence graph and return
4033 * the updated schedule node.
4035 * We try to find a sequence of as many schedule rows as possible that result
4036 * in non-negative dependence distances (independent of the previous rows
4037 * in the sequence, i.e., such that the sequence is tilable), with as
4038 * many of the initial rows as possible satisfying the coincidence constraints.
4039 * If we can't find any more rows we either
4040 * - split between SCCs and start over (assuming we found an interesting
4041 * pair of SCCs between which to split)
4042 * - continue with the next band (assuming the current band has at least
4043 * one row)
4044 * - try to carry as many dependences as possible and continue with the next
4045 * band
4046 * In each case, we first insert a band node in the schedule tree
4047 * if any rows have been computed.
4049 * If Feautrier's algorithm is selected, we first recursively try to satisfy
4050 * as many validity dependences as possible. When all validity dependences
4051 * are satisfied we extend the schedule to a full-dimensional schedule.
4053 * If we manage to complete the schedule, we insert a band node
4054 * (if any schedule rows were computed) and we finish off by topologically
4055 * sorting the statements based on the remaining dependences.
4057 * If ctx->opt->schedule_outer_coincidence is set, then we force the
4058 * outermost dimension to satisfy the coincidence constraints. If this
4059 * turns out to be impossible, we fall back on the general scheme above
4060 * and try to carry as many dependences as possible.
4062 * If "graph" contains both condition and conditional validity dependences,
4063 * then we need to check that that the conditional schedule constraint
4064 * is satisfied, i.e., there are no violated conditional validity dependences
4065 * that are adjacent to any non-local condition dependences.
4066 * If there are, then we mark all those adjacent condition dependences
4067 * as local and recompute the current band. Those dependences that
4068 * are marked local will then be forced to be local.
4069 * The initial computation is performed with no dependences marked as local.
4070 * If we are lucky, then there will be no violated conditional validity
4071 * dependences adjacent to any non-local condition dependences.
4072 * Otherwise, we mark some additional condition dependences as local and
4073 * recompute. We continue this process until there are no violations left or
4074 * until we are no longer able to compute a schedule.
4075 * Since there are only a finite number of dependences,
4076 * there will only be a finite number of iterations.
4078 static __isl_give isl_schedule_node *compute_schedule_wcc(
4079 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph)
4081 int has_coincidence;
4082 int use_coincidence;
4083 int force_coincidence = 0;
4084 int check_conditional;
4085 isl_ctx *ctx;
4087 if (!node)
4088 return NULL;
4090 ctx = isl_schedule_node_get_ctx(node);
4091 if (detect_sccs(ctx, graph) < 0)
4092 return isl_schedule_node_free(node);
4093 if (sort_sccs(graph) < 0)
4094 return isl_schedule_node_free(node);
4096 if (compute_maxvar(graph) < 0)
4097 return isl_schedule_node_free(node);
4099 if (need_feautrier_step(ctx, graph))
4100 return compute_schedule_wcc_feautrier(node, graph);
4102 clear_local_edges(graph);
4103 check_conditional = need_condition_check(graph);
4104 has_coincidence = has_any_coincidence(graph);
4106 if (ctx->opt->schedule_outer_coincidence)
4107 force_coincidence = 1;
4109 use_coincidence = has_coincidence;
4110 while (graph->n_row < graph->maxvar) {
4111 isl_vec *sol;
4112 int violated;
4113 int coincident;
4115 graph->src_scc = -1;
4116 graph->dst_scc = -1;
4118 if (setup_lp(ctx, graph, use_coincidence) < 0)
4119 return isl_schedule_node_free(node);
4120 sol = solve_lp(graph);
4121 if (!sol)
4122 return isl_schedule_node_free(node);
4123 if (sol->size == 0) {
4124 int empty = graph->n_total_row == graph->band_start;
4126 isl_vec_free(sol);
4127 if (use_coincidence && (!force_coincidence || !empty)) {
4128 use_coincidence = 0;
4129 continue;
4131 if (!ctx->opt->schedule_maximize_band_depth && !empty)
4132 return compute_next_band(node, graph, 1);
4133 if (graph->src_scc >= 0)
4134 return compute_split_schedule(node, graph);
4135 if (!empty)
4136 return compute_next_band(node, graph, 1);
4137 return carry_dependences(node, graph);
4139 coincident = !has_coincidence || use_coincidence;
4140 if (update_schedule(graph, sol, 1, coincident) < 0)
4141 return isl_schedule_node_free(node);
4143 if (!check_conditional)
4144 continue;
4145 violated = has_violated_conditional_constraint(ctx, graph);
4146 if (violated < 0)
4147 return isl_schedule_node_free(node);
4148 if (!violated)
4149 continue;
4150 if (reset_band(graph) < 0)
4151 return isl_schedule_node_free(node);
4152 use_coincidence = has_coincidence;
4155 if (graph->n_total_row > graph->band_start) {
4156 node = insert_current_band(node, graph, 1);
4157 node = isl_schedule_node_child(node, 0);
4159 node = sort_statements(node, graph);
4160 if (graph->n_total_row > graph->band_start)
4161 node = isl_schedule_node_parent(node);
4163 return node;
4166 /* Compute a schedule for each group of nodes identified by node->scc
4167 * separately and then combine them in a sequence node (or as set node
4168 * if graph->weak is set) inserted at position "node" of the schedule tree.
4169 * Return the updated schedule node.
4171 * If "wcc" is set then each of the groups belongs to a single
4172 * weakly connected component in the dependence graph so that
4173 * there is no need for compute_sub_schedule to look for weakly
4174 * connected components.
4176 static __isl_give isl_schedule_node *compute_component_schedule(
4177 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph,
4178 int wcc)
4180 int component, i;
4181 int n, n_edge;
4182 int orig_total_row;
4183 isl_ctx *ctx;
4184 isl_union_set_list *filters;
4186 if (!node)
4187 return NULL;
4188 ctx = isl_schedule_node_get_ctx(node);
4190 filters = extract_sccs(ctx, graph);
4191 if (graph->weak)
4192 node = isl_schedule_node_insert_set(node, filters);
4193 else
4194 node = isl_schedule_node_insert_sequence(node, filters);
4196 orig_total_row = graph->n_total_row;
4197 for (component = 0; component < graph->scc; ++component) {
4198 n = 0;
4199 for (i = 0; i < graph->n; ++i)
4200 if (graph->node[i].scc == component)
4201 n++;
4202 n_edge = 0;
4203 for (i = 0; i < graph->n_edge; ++i)
4204 if (graph->edge[i].src->scc == component &&
4205 graph->edge[i].dst->scc == component)
4206 n_edge++;
4208 node = isl_schedule_node_child(node, component);
4209 node = isl_schedule_node_child(node, 0);
4210 node = compute_sub_schedule(node, ctx, graph, n, n_edge,
4211 &node_scc_exactly,
4212 &edge_scc_exactly, component, wcc);
4213 node = isl_schedule_node_parent(node);
4214 node = isl_schedule_node_parent(node);
4215 graph->n_total_row = orig_total_row;
4218 return node;
4221 /* Compute a schedule for the given dependence graph and insert it at "node".
4222 * Return the updated schedule node.
4224 * We first check if the graph is connected (through validity and conditional
4225 * validity dependences) and, if not, compute a schedule
4226 * for each component separately.
4227 * If schedule_fuse is set to minimal fusion, then we check for strongly
4228 * connected components instead and compute a separate schedule for
4229 * each such strongly connected component.
4231 static __isl_give isl_schedule_node *compute_schedule(isl_schedule_node *node,
4232 struct isl_sched_graph *graph)
4234 isl_ctx *ctx;
4236 if (!node)
4237 return NULL;
4239 ctx = isl_schedule_node_get_ctx(node);
4240 if (ctx->opt->schedule_fuse == ISL_SCHEDULE_FUSE_MIN) {
4241 if (detect_sccs(ctx, graph) < 0)
4242 return isl_schedule_node_free(node);
4243 } else {
4244 if (detect_wccs(ctx, graph) < 0)
4245 return isl_schedule_node_free(node);
4248 if (graph->scc > 1)
4249 return compute_component_schedule(node, graph, 1);
4251 return compute_schedule_wcc(node, graph);
4254 /* Compute a schedule on sc->domain that respects the given schedule
4255 * constraints.
4257 * In particular, the schedule respects all the validity dependences.
4258 * If the default isl scheduling algorithm is used, it tries to minimize
4259 * the dependence distances over the proximity dependences.
4260 * If Feautrier's scheduling algorithm is used, the proximity dependence
4261 * distances are only minimized during the extension to a full-dimensional
4262 * schedule.
4264 * If there are any condition and conditional validity dependences,
4265 * then the conditional validity dependences may be violated inside
4266 * a tilable band, provided they have no adjacent non-local
4267 * condition dependences.
4269 * The context is included in the domain before the nodes of
4270 * the graphs are extracted in order to be able to exploit
4271 * any possible additional equalities.
4272 * However, the returned schedule contains the original domain
4273 * (before this intersection).
4275 __isl_give isl_schedule *isl_schedule_constraints_compute_schedule(
4276 __isl_take isl_schedule_constraints *sc)
4278 isl_ctx *ctx = isl_schedule_constraints_get_ctx(sc);
4279 struct isl_sched_graph graph = { 0 };
4280 isl_schedule *sched;
4281 isl_schedule_node *node;
4282 isl_union_set *domain;
4283 struct isl_extract_edge_data data;
4284 enum isl_edge_type i;
4285 int r;
4287 sc = isl_schedule_constraints_align_params(sc);
4288 if (!sc)
4289 return NULL;
4291 graph.n = isl_union_set_n_set(sc->domain);
4292 if (graph.n == 0) {
4293 isl_union_set *domain = isl_union_set_copy(sc->domain);
4294 sched = isl_schedule_from_domain(domain);
4295 goto done;
4297 if (graph_alloc(ctx, &graph, graph.n,
4298 isl_schedule_constraints_n_map(sc)) < 0)
4299 goto error;
4300 if (compute_max_row(&graph, sc) < 0)
4301 goto error;
4302 graph.root = 1;
4303 graph.n = 0;
4304 domain = isl_union_set_copy(sc->domain);
4305 domain = isl_union_set_intersect_params(domain,
4306 isl_set_copy(sc->context));
4307 r = isl_union_set_foreach_set(domain, &extract_node, &graph);
4308 isl_union_set_free(domain);
4309 if (r < 0)
4310 goto error;
4311 if (graph_init_table(ctx, &graph) < 0)
4312 goto error;
4313 for (i = isl_edge_first; i <= isl_edge_last; ++i)
4314 graph.max_edge[i] = isl_union_map_n_map(sc->constraint[i]);
4315 if (graph_init_edge_tables(ctx, &graph) < 0)
4316 goto error;
4317 graph.n_edge = 0;
4318 data.graph = &graph;
4319 for (i = isl_edge_first; i <= isl_edge_last; ++i) {
4320 data.type = i;
4321 if (isl_union_map_foreach_map(sc->constraint[i],
4322 &extract_edge, &data) < 0)
4323 goto error;
4326 node = isl_schedule_node_from_domain(isl_union_set_copy(sc->domain));
4327 node = isl_schedule_node_child(node, 0);
4328 node = compute_schedule(node, &graph);
4329 sched = isl_schedule_node_get_schedule(node);
4330 isl_schedule_node_free(node);
4332 done:
4333 graph_free(ctx, &graph);
4334 isl_schedule_constraints_free(sc);
4336 return sched;
4337 error:
4338 graph_free(ctx, &graph);
4339 isl_schedule_constraints_free(sc);
4340 return NULL;
4343 /* Compute a schedule for the given union of domains that respects
4344 * all the validity dependences and minimizes
4345 * the dependence distances over the proximity dependences.
4347 * This function is kept for backward compatibility.
4349 __isl_give isl_schedule *isl_union_set_compute_schedule(
4350 __isl_take isl_union_set *domain,
4351 __isl_take isl_union_map *validity,
4352 __isl_take isl_union_map *proximity)
4354 isl_schedule_constraints *sc;
4356 sc = isl_schedule_constraints_on_domain(domain);
4357 sc = isl_schedule_constraints_set_validity(sc, validity);
4358 sc = isl_schedule_constraints_set_proximity(sc, proximity);
4360 return isl_schedule_constraints_compute_schedule(sc);