doc: document isl_basic_set_is_equal
[isl.git] / isl_scheduler.c
blob863e45022672c83b6c0bda136b8a62a8937945d9
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 /* Copy nodes that satisfy node_pred from the src dependence graph
2798 * to the dst dependence graph.
2800 static int copy_nodes(struct isl_sched_graph *dst, struct isl_sched_graph *src,
2801 int (*node_pred)(struct isl_sched_node *node, int data), int data)
2803 int i;
2805 dst->n = 0;
2806 for (i = 0; i < src->n; ++i) {
2807 int j;
2809 if (!node_pred(&src->node[i], data))
2810 continue;
2812 j = dst->n;
2813 dst->node[j].space = isl_space_copy(src->node[i].space);
2814 dst->node[j].compressed = src->node[i].compressed;
2815 dst->node[j].hull = isl_set_copy(src->node[i].hull);
2816 dst->node[j].compress =
2817 isl_multi_aff_copy(src->node[i].compress);
2818 dst->node[j].decompress =
2819 isl_multi_aff_copy(src->node[i].decompress);
2820 dst->node[j].nvar = src->node[i].nvar;
2821 dst->node[j].nparam = src->node[i].nparam;
2822 dst->node[j].sched = isl_mat_copy(src->node[i].sched);
2823 dst->node[j].sched_map = isl_map_copy(src->node[i].sched_map);
2824 dst->node[j].coincident = src->node[i].coincident;
2825 dst->n++;
2827 if (!dst->node[j].space || !dst->node[j].sched)
2828 return -1;
2829 if (dst->node[j].compressed &&
2830 (!dst->node[j].hull || !dst->node[j].compress ||
2831 !dst->node[j].decompress))
2832 return -1;
2835 return 0;
2838 /* Copy non-empty edges that satisfy edge_pred from the src dependence graph
2839 * to the dst dependence graph.
2840 * If the source or destination node of the edge is not in the destination
2841 * graph, then it must be a backward proximity edge and it should simply
2842 * be ignored.
2844 static int copy_edges(isl_ctx *ctx, struct isl_sched_graph *dst,
2845 struct isl_sched_graph *src,
2846 int (*edge_pred)(struct isl_sched_edge *edge, int data), int data)
2848 int i;
2849 enum isl_edge_type t;
2851 dst->n_edge = 0;
2852 for (i = 0; i < src->n_edge; ++i) {
2853 struct isl_sched_edge *edge = &src->edge[i];
2854 isl_map *map;
2855 isl_union_map *tagged_condition;
2856 isl_union_map *tagged_validity;
2857 struct isl_sched_node *dst_src, *dst_dst;
2859 if (!edge_pred(edge, data))
2860 continue;
2862 if (isl_map_plain_is_empty(edge->map))
2863 continue;
2865 dst_src = graph_find_node(ctx, dst, edge->src->space);
2866 dst_dst = graph_find_node(ctx, dst, edge->dst->space);
2867 if (!dst_src || !dst_dst) {
2868 if (edge->validity || edge->conditional_validity)
2869 isl_die(ctx, isl_error_internal,
2870 "backward (conditional) validity edge",
2871 return -1);
2872 continue;
2875 map = isl_map_copy(edge->map);
2876 tagged_condition = isl_union_map_copy(edge->tagged_condition);
2877 tagged_validity = isl_union_map_copy(edge->tagged_validity);
2879 dst->edge[dst->n_edge].src = dst_src;
2880 dst->edge[dst->n_edge].dst = dst_dst;
2881 dst->edge[dst->n_edge].map = map;
2882 dst->edge[dst->n_edge].tagged_condition = tagged_condition;
2883 dst->edge[dst->n_edge].tagged_validity = tagged_validity;
2884 dst->edge[dst->n_edge].validity = edge->validity;
2885 dst->edge[dst->n_edge].proximity = edge->proximity;
2886 dst->edge[dst->n_edge].coincidence = edge->coincidence;
2887 dst->edge[dst->n_edge].condition = edge->condition;
2888 dst->edge[dst->n_edge].conditional_validity =
2889 edge->conditional_validity;
2890 dst->n_edge++;
2892 if (edge->tagged_condition && !tagged_condition)
2893 return -1;
2894 if (edge->tagged_validity && !tagged_validity)
2895 return -1;
2897 for (t = isl_edge_first; t <= isl_edge_last; ++t) {
2898 if (edge !=
2899 graph_find_edge(src, t, edge->src, edge->dst))
2900 continue;
2901 if (graph_edge_table_add(ctx, dst, t,
2902 &dst->edge[dst->n_edge - 1]) < 0)
2903 return -1;
2907 return 0;
2910 /* Compute the maximal number of variables over all nodes.
2911 * This is the maximal number of linearly independent schedule
2912 * rows that we need to compute.
2913 * Just in case we end up in a part of the dependence graph
2914 * with only lower-dimensional domains, we make sure we will
2915 * compute the required amount of extra linearly independent rows.
2917 static int compute_maxvar(struct isl_sched_graph *graph)
2919 int i;
2921 graph->maxvar = 0;
2922 for (i = 0; i < graph->n; ++i) {
2923 struct isl_sched_node *node = &graph->node[i];
2924 int nvar;
2926 if (node_update_cmap(node) < 0)
2927 return -1;
2928 nvar = node->nvar + graph->n_row - node->rank;
2929 if (nvar > graph->maxvar)
2930 graph->maxvar = nvar;
2933 return 0;
2936 static __isl_give isl_schedule_node *compute_schedule(isl_schedule_node *node,
2937 struct isl_sched_graph *graph);
2938 static __isl_give isl_schedule_node *compute_schedule_wcc(
2939 isl_schedule_node *node, struct isl_sched_graph *graph);
2941 /* Compute a schedule for a subgraph of "graph". In particular, for
2942 * the graph composed of nodes that satisfy node_pred and edges that
2943 * that satisfy edge_pred. The caller should precompute the number
2944 * of nodes and edges that satisfy these predicates and pass them along
2945 * as "n" and "n_edge".
2946 * If the subgraph is known to consist of a single component, then wcc should
2947 * be set and then we call compute_schedule_wcc on the constructed subgraph.
2948 * Otherwise, we call compute_schedule, which will check whether the subgraph
2949 * is connected.
2951 * The schedule is inserted at "node" and the updated schedule node
2952 * is returned.
2954 static __isl_give isl_schedule_node *compute_sub_schedule(
2955 __isl_take isl_schedule_node *node, isl_ctx *ctx,
2956 struct isl_sched_graph *graph, int n, int n_edge,
2957 int (*node_pred)(struct isl_sched_node *node, int data),
2958 int (*edge_pred)(struct isl_sched_edge *edge, int data),
2959 int data, int wcc)
2961 struct isl_sched_graph split = { 0 };
2962 int t;
2964 if (graph_alloc(ctx, &split, n, n_edge) < 0)
2965 goto error;
2966 if (copy_nodes(&split, graph, node_pred, data) < 0)
2967 goto error;
2968 if (graph_init_table(ctx, &split) < 0)
2969 goto error;
2970 for (t = 0; t <= isl_edge_last; ++t)
2971 split.max_edge[t] = graph->max_edge[t];
2972 if (graph_init_edge_tables(ctx, &split) < 0)
2973 goto error;
2974 if (copy_edges(ctx, &split, graph, edge_pred, data) < 0)
2975 goto error;
2976 split.n_row = graph->n_row;
2977 split.max_row = graph->max_row;
2978 split.n_total_row = graph->n_total_row;
2979 split.band_start = graph->band_start;
2981 if (wcc)
2982 node = compute_schedule_wcc(node, &split);
2983 else
2984 node = compute_schedule(node, &split);
2986 graph_free(ctx, &split);
2987 return node;
2988 error:
2989 graph_free(ctx, &split);
2990 return isl_schedule_node_free(node);
2993 static int edge_scc_exactly(struct isl_sched_edge *edge, int scc)
2995 return edge->src->scc == scc && edge->dst->scc == scc;
2998 static int edge_dst_scc_at_most(struct isl_sched_edge *edge, int scc)
3000 return edge->dst->scc <= scc;
3003 static int edge_src_scc_at_least(struct isl_sched_edge *edge, int scc)
3005 return edge->src->scc >= scc;
3008 /* Reset the current band by dropping all its schedule rows.
3010 static int reset_band(struct isl_sched_graph *graph)
3012 int i;
3013 int drop;
3015 drop = graph->n_total_row - graph->band_start;
3016 graph->n_total_row -= drop;
3017 graph->n_row -= drop;
3019 for (i = 0; i < graph->n; ++i) {
3020 struct isl_sched_node *node = &graph->node[i];
3022 isl_map_free(node->sched_map);
3023 node->sched_map = NULL;
3025 node->sched = isl_mat_drop_rows(node->sched,
3026 graph->band_start, drop);
3028 if (!node->sched)
3029 return -1;
3032 return 0;
3035 /* Split the current graph into two parts and compute a schedule for each
3036 * part individually. In particular, one part consists of all SCCs up
3037 * to and including graph->src_scc, while the other part contains the other
3038 * SCCS. The split is enforced by a sequence node inserted at position "node"
3039 * in the schedule tree. Return the updated schedule node.
3041 * The current band is reset. It would be possible to reuse
3042 * the previously computed rows as the first rows in the next
3043 * band, but recomputing them may result in better rows as we are looking
3044 * at a smaller part of the dependence graph.
3046 static __isl_give isl_schedule_node *compute_split_schedule(
3047 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph)
3049 int i, n, e1, e2;
3050 int orig_total_row;
3051 isl_ctx *ctx;
3052 isl_union_set_list *filters;
3054 if (!node)
3055 return NULL;
3057 if (reset_band(graph) < 0)
3058 return isl_schedule_node_free(node);
3060 n = 0;
3061 for (i = 0; i < graph->n; ++i) {
3062 struct isl_sched_node *node = &graph->node[i];
3063 int before = node->scc <= graph->src_scc;
3065 if (before)
3066 n++;
3069 e1 = e2 = 0;
3070 for (i = 0; i < graph->n_edge; ++i) {
3071 if (graph->edge[i].dst->scc <= graph->src_scc)
3072 e1++;
3073 if (graph->edge[i].src->scc > graph->src_scc)
3074 e2++;
3077 next_band(graph);
3079 ctx = isl_schedule_node_get_ctx(node);
3080 filters = extract_split(ctx, graph);
3081 node = isl_schedule_node_insert_sequence(node, filters);
3082 node = isl_schedule_node_child(node, 0);
3083 node = isl_schedule_node_child(node, 0);
3085 orig_total_row = graph->n_total_row;
3086 node = compute_sub_schedule(node, ctx, graph, n, e1,
3087 &node_scc_at_most, &edge_dst_scc_at_most,
3088 graph->src_scc, 0);
3089 node = isl_schedule_node_parent(node);
3090 node = isl_schedule_node_next_sibling(node);
3091 node = isl_schedule_node_child(node, 0);
3092 graph->n_total_row = orig_total_row;
3093 node = compute_sub_schedule(node, ctx, graph, graph->n - n, e2,
3094 &node_scc_at_least, &edge_src_scc_at_least,
3095 graph->src_scc + 1, 0);
3096 node = isl_schedule_node_parent(node);
3097 node = isl_schedule_node_parent(node);
3099 return node;
3102 /* Insert a band node at position "node" in the schedule tree corresponding
3103 * to the current band in "graph". Mark the band node permutable
3104 * if "permutable" is set.
3105 * The partial schedules and the coincidence property are extracted
3106 * from the graph nodes.
3107 * Return the updated schedule node.
3109 static __isl_give isl_schedule_node *insert_current_band(
3110 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph,
3111 int permutable)
3113 int i;
3114 int start, end, n;
3115 isl_multi_aff *ma;
3116 isl_multi_pw_aff *mpa;
3117 isl_multi_union_pw_aff *mupa;
3119 if (!node)
3120 return NULL;
3122 if (graph->n < 1)
3123 isl_die(isl_schedule_node_get_ctx(node), isl_error_internal,
3124 "graph should have at least one node",
3125 return isl_schedule_node_free(node));
3127 start = graph->band_start;
3128 end = graph->n_total_row;
3129 n = end - start;
3131 ma = node_extract_partial_schedule_multi_aff(&graph->node[0], start, n);
3132 mpa = isl_multi_pw_aff_from_multi_aff(ma);
3133 mupa = isl_multi_union_pw_aff_from_multi_pw_aff(mpa);
3135 for (i = 1; i < graph->n; ++i) {
3136 isl_multi_union_pw_aff *mupa_i;
3138 ma = node_extract_partial_schedule_multi_aff(&graph->node[i],
3139 start, n);
3140 mpa = isl_multi_pw_aff_from_multi_aff(ma);
3141 mupa_i = isl_multi_union_pw_aff_from_multi_pw_aff(mpa);
3142 mupa = isl_multi_union_pw_aff_union_add(mupa, mupa_i);
3144 node = isl_schedule_node_insert_partial_schedule(node, mupa);
3146 for (i = 0; i < n; ++i)
3147 node = isl_schedule_node_band_member_set_coincident(node, i,
3148 graph->node[0].coincident[start + i]);
3149 node = isl_schedule_node_band_set_permutable(node, permutable);
3151 return node;
3154 /* Update the dependence relations based on the current schedule,
3155 * add the current band to "node" and the continue with the computation
3156 * of the next band.
3157 * Return the updated schedule node.
3159 static __isl_give isl_schedule_node *compute_next_band(
3160 __isl_take isl_schedule_node *node,
3161 struct isl_sched_graph *graph, int permutable)
3163 isl_ctx *ctx;
3165 if (!node)
3166 return NULL;
3168 ctx = isl_schedule_node_get_ctx(node);
3169 if (update_edges(ctx, graph) < 0)
3170 return isl_schedule_node_free(node);
3171 node = insert_current_band(node, graph, permutable);
3172 next_band(graph);
3174 node = isl_schedule_node_child(node, 0);
3175 node = compute_schedule(node, graph);
3176 node = isl_schedule_node_parent(node);
3178 return node;
3181 /* Add constraints to graph->lp that force the dependence "map" (which
3182 * is part of the dependence relation of "edge")
3183 * to be respected and attempt to carry it, where the edge is one from
3184 * a node j to itself. "pos" is the sequence number of the given map.
3185 * That is, add constraints that enforce
3187 * (c_j_0 + c_j_n n + c_j_x y) - (c_j_0 + c_j_n n + c_j_x x)
3188 * = c_j_x (y - x) >= e_i
3190 * for each (x,y) in R.
3191 * We obtain general constraints on coefficients (c_0, c_n, c_x)
3192 * of valid constraints for (y - x) and then plug in (-e_i, 0, c_j_x),
3193 * with each coefficient in c_j_x represented as a pair of non-negative
3194 * coefficients.
3196 static int add_intra_constraints(struct isl_sched_graph *graph,
3197 struct isl_sched_edge *edge, __isl_take isl_map *map, int pos)
3199 unsigned total;
3200 isl_ctx *ctx = isl_map_get_ctx(map);
3201 isl_space *dim;
3202 isl_dim_map *dim_map;
3203 isl_basic_set *coef;
3204 struct isl_sched_node *node = edge->src;
3206 coef = intra_coefficients(graph, node, map);
3207 if (!coef)
3208 return -1;
3210 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
3212 total = isl_basic_set_total_dim(graph->lp);
3213 dim_map = isl_dim_map_alloc(ctx, total);
3214 isl_dim_map_range(dim_map, 3 + pos, 0, 0, 0, 1, -1);
3215 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 1, 2,
3216 isl_space_dim(dim, isl_dim_set), 1,
3217 node->nvar, -1);
3218 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 2, 2,
3219 isl_space_dim(dim, isl_dim_set), 1,
3220 node->nvar, 1);
3221 graph->lp = isl_basic_set_extend_constraints(graph->lp,
3222 coef->n_eq, coef->n_ineq);
3223 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
3224 coef, dim_map);
3225 isl_space_free(dim);
3227 return 0;
3230 /* Add constraints to graph->lp that force the dependence "map" (which
3231 * is part of the dependence relation of "edge")
3232 * to be respected and attempt to carry it, where the edge is one from
3233 * node j to node k. "pos" is the sequence number of the given map.
3234 * That is, add constraints that enforce
3236 * (c_k_0 + c_k_n n + c_k_x y) - (c_j_0 + c_j_n n + c_j_x x) >= e_i
3238 * for each (x,y) in R.
3239 * We obtain general constraints on coefficients (c_0, c_n, c_x)
3240 * of valid constraints for R and then plug in
3241 * (-e_i + c_k_0 - c_j_0, c_k_n - c_j_n, c_k_x - c_j_x)
3242 * with each coefficient (except e_i, c_k_0 and c_j_0)
3243 * represented as a pair of non-negative coefficients.
3245 static int add_inter_constraints(struct isl_sched_graph *graph,
3246 struct isl_sched_edge *edge, __isl_take isl_map *map, int pos)
3248 unsigned total;
3249 isl_ctx *ctx = isl_map_get_ctx(map);
3250 isl_space *dim;
3251 isl_dim_map *dim_map;
3252 isl_basic_set *coef;
3253 struct isl_sched_node *src = edge->src;
3254 struct isl_sched_node *dst = edge->dst;
3256 coef = inter_coefficients(graph, edge, map);
3257 if (!coef)
3258 return -1;
3260 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
3262 total = isl_basic_set_total_dim(graph->lp);
3263 dim_map = isl_dim_map_alloc(ctx, total);
3265 isl_dim_map_range(dim_map, 3 + pos, 0, 0, 0, 1, -1);
3267 isl_dim_map_range(dim_map, dst->start, 0, 0, 0, 1, 1);
3268 isl_dim_map_range(dim_map, dst->start + 1, 2, 1, 1, dst->nparam, -1);
3269 isl_dim_map_range(dim_map, dst->start + 2, 2, 1, 1, dst->nparam, 1);
3270 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 1, 2,
3271 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
3272 dst->nvar, -1);
3273 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 2, 2,
3274 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
3275 dst->nvar, 1);
3277 isl_dim_map_range(dim_map, src->start, 0, 0, 0, 1, -1);
3278 isl_dim_map_range(dim_map, src->start + 1, 2, 1, 1, src->nparam, 1);
3279 isl_dim_map_range(dim_map, src->start + 2, 2, 1, 1, src->nparam, -1);
3280 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 1, 2,
3281 isl_space_dim(dim, isl_dim_set), 1,
3282 src->nvar, 1);
3283 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 2, 2,
3284 isl_space_dim(dim, isl_dim_set), 1,
3285 src->nvar, -1);
3287 graph->lp = isl_basic_set_extend_constraints(graph->lp,
3288 coef->n_eq, coef->n_ineq);
3289 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
3290 coef, dim_map);
3291 isl_space_free(dim);
3293 return 0;
3296 /* Add constraints to graph->lp that force all (conditional) validity
3297 * dependences to be respected and attempt to carry them.
3299 static int add_all_constraints(struct isl_sched_graph *graph)
3301 int i, j;
3302 int pos;
3304 pos = 0;
3305 for (i = 0; i < graph->n_edge; ++i) {
3306 struct isl_sched_edge *edge= &graph->edge[i];
3308 if (!edge->validity && !edge->conditional_validity)
3309 continue;
3311 for (j = 0; j < edge->map->n; ++j) {
3312 isl_basic_map *bmap;
3313 isl_map *map;
3315 bmap = isl_basic_map_copy(edge->map->p[j]);
3316 map = isl_map_from_basic_map(bmap);
3318 if (edge->src == edge->dst &&
3319 add_intra_constraints(graph, edge, map, pos) < 0)
3320 return -1;
3321 if (edge->src != edge->dst &&
3322 add_inter_constraints(graph, edge, map, pos) < 0)
3323 return -1;
3324 ++pos;
3328 return 0;
3331 /* Count the number of equality and inequality constraints
3332 * that will be added to the carry_lp problem.
3333 * We count each edge exactly once.
3335 static int count_all_constraints(struct isl_sched_graph *graph,
3336 int *n_eq, int *n_ineq)
3338 int i, j;
3340 *n_eq = *n_ineq = 0;
3341 for (i = 0; i < graph->n_edge; ++i) {
3342 struct isl_sched_edge *edge= &graph->edge[i];
3343 for (j = 0; j < edge->map->n; ++j) {
3344 isl_basic_map *bmap;
3345 isl_map *map;
3347 bmap = isl_basic_map_copy(edge->map->p[j]);
3348 map = isl_map_from_basic_map(bmap);
3350 if (count_map_constraints(graph, edge, map,
3351 n_eq, n_ineq, 1, 0) < 0)
3352 return -1;
3356 return 0;
3359 /* Construct an LP problem for finding schedule coefficients
3360 * such that the schedule carries as many dependences as possible.
3361 * In particular, for each dependence i, we bound the dependence distance
3362 * from below by e_i, with 0 <= e_i <= 1 and then maximize the sum
3363 * of all e_i's. Dependence with e_i = 0 in the solution are simply
3364 * respected, while those with e_i > 0 (in practice e_i = 1) are carried.
3365 * Note that if the dependence relation is a union of basic maps,
3366 * then we have to consider each basic map individually as it may only
3367 * be possible to carry the dependences expressed by some of those
3368 * basic maps and not all off them.
3369 * Below, we consider each of those basic maps as a separate "edge".
3371 * All variables of the LP are non-negative. The actual coefficients
3372 * may be negative, so each coefficient is represented as the difference
3373 * of two non-negative variables. The negative part always appears
3374 * immediately before the positive part.
3375 * Other than that, the variables have the following order
3377 * - sum of (1 - e_i) over all edges
3378 * - sum of positive and negative parts of all c_n coefficients
3379 * (unconstrained when computing non-parametric schedules)
3380 * - sum of positive and negative parts of all c_x coefficients
3381 * - for each edge
3382 * - e_i
3383 * - for each node
3384 * - c_i_0
3385 * - positive and negative parts of c_i_n (if parametric)
3386 * - positive and negative parts of c_i_x
3388 * The constraints are those from the (validity) edges plus three equalities
3389 * to express the sums and n_edge inequalities to express e_i <= 1.
3391 static int setup_carry_lp(isl_ctx *ctx, struct isl_sched_graph *graph)
3393 int i, j;
3394 int k;
3395 isl_space *dim;
3396 unsigned total;
3397 int n_eq, n_ineq;
3398 int n_edge;
3400 n_edge = 0;
3401 for (i = 0; i < graph->n_edge; ++i)
3402 n_edge += graph->edge[i].map->n;
3404 total = 3 + n_edge;
3405 for (i = 0; i < graph->n; ++i) {
3406 struct isl_sched_node *node = &graph->node[graph->sorted[i]];
3407 node->start = total;
3408 total += 1 + 2 * (node->nparam + node->nvar);
3411 if (count_all_constraints(graph, &n_eq, &n_ineq) < 0)
3412 return -1;
3413 if (count_bound_coefficient_constraints(ctx, graph, &n_eq, &n_ineq) < 0)
3414 return -1;
3416 dim = isl_space_set_alloc(ctx, 0, total);
3417 isl_basic_set_free(graph->lp);
3418 n_eq += 3;
3419 n_ineq += n_edge;
3420 graph->lp = isl_basic_set_alloc_space(dim, 0, n_eq, n_ineq);
3421 graph->lp = isl_basic_set_set_rational(graph->lp);
3423 k = isl_basic_set_alloc_equality(graph->lp);
3424 if (k < 0)
3425 return -1;
3426 isl_seq_clr(graph->lp->eq[k], 1 + total);
3427 isl_int_set_si(graph->lp->eq[k][0], -n_edge);
3428 isl_int_set_si(graph->lp->eq[k][1], 1);
3429 for (i = 0; i < n_edge; ++i)
3430 isl_int_set_si(graph->lp->eq[k][4 + i], 1);
3432 k = isl_basic_set_alloc_equality(graph->lp);
3433 if (k < 0)
3434 return -1;
3435 isl_seq_clr(graph->lp->eq[k], 1 + total);
3436 isl_int_set_si(graph->lp->eq[k][2], -1);
3437 for (i = 0; i < graph->n; ++i) {
3438 int pos = 1 + graph->node[i].start + 1;
3440 for (j = 0; j < 2 * graph->node[i].nparam; ++j)
3441 isl_int_set_si(graph->lp->eq[k][pos + j], 1);
3444 k = isl_basic_set_alloc_equality(graph->lp);
3445 if (k < 0)
3446 return -1;
3447 isl_seq_clr(graph->lp->eq[k], 1 + total);
3448 isl_int_set_si(graph->lp->eq[k][3], -1);
3449 for (i = 0; i < graph->n; ++i) {
3450 struct isl_sched_node *node = &graph->node[i];
3451 int pos = 1 + node->start + 1 + 2 * node->nparam;
3453 for (j = 0; j < 2 * node->nvar; ++j)
3454 isl_int_set_si(graph->lp->eq[k][pos + j], 1);
3457 for (i = 0; i < n_edge; ++i) {
3458 k = isl_basic_set_alloc_inequality(graph->lp);
3459 if (k < 0)
3460 return -1;
3461 isl_seq_clr(graph->lp->ineq[k], 1 + total);
3462 isl_int_set_si(graph->lp->ineq[k][4 + i], -1);
3463 isl_int_set_si(graph->lp->ineq[k][0], 1);
3466 if (add_bound_coefficient_constraints(ctx, graph) < 0)
3467 return -1;
3468 if (add_all_constraints(graph) < 0)
3469 return -1;
3471 return 0;
3474 static __isl_give isl_schedule_node *compute_component_schedule(
3475 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph,
3476 int wcc);
3478 /* Comparison function for sorting the statements based on
3479 * the corresponding value in "r".
3481 static int smaller_value(const void *a, const void *b, void *data)
3483 isl_vec *r = data;
3484 const int *i1 = a;
3485 const int *i2 = b;
3487 return isl_int_cmp(r->el[*i1], r->el[*i2]);
3490 /* If the schedule_split_scaled option is set and if the linear
3491 * parts of the scheduling rows for all nodes in the graphs have
3492 * a non-trivial common divisor, then split off the remainder of the
3493 * constant term modulo this common divisor from the linear part.
3494 * Otherwise, insert a band node directly and continue with
3495 * the construction of the schedule.
3497 * If a non-trivial common divisor is found, then
3498 * the linear part is reduced and the remainder is enforced
3499 * by a sequence node with the children placed in the order
3500 * of this remainder.
3501 * In particular, we assign an scc index based on the remainder and
3502 * then rely on compute_component_schedule to insert the sequence and
3503 * to continue the schedule construction on each part.
3505 static __isl_give isl_schedule_node *split_scaled(
3506 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph)
3508 int i;
3509 int row;
3510 int scc;
3511 isl_ctx *ctx;
3512 isl_int gcd, gcd_i;
3513 isl_vec *r;
3514 int *order;
3516 if (!node)
3517 return NULL;
3519 ctx = isl_schedule_node_get_ctx(node);
3520 if (!ctx->opt->schedule_split_scaled)
3521 return compute_next_band(node, graph, 0);
3522 if (graph->n <= 1)
3523 return compute_next_band(node, graph, 0);
3525 isl_int_init(gcd);
3526 isl_int_init(gcd_i);
3528 isl_int_set_si(gcd, 0);
3530 row = isl_mat_rows(graph->node[0].sched) - 1;
3532 for (i = 0; i < graph->n; ++i) {
3533 struct isl_sched_node *node = &graph->node[i];
3534 int cols = isl_mat_cols(node->sched);
3536 isl_seq_gcd(node->sched->row[row] + 1, cols - 1, &gcd_i);
3537 isl_int_gcd(gcd, gcd, gcd_i);
3540 isl_int_clear(gcd_i);
3542 if (isl_int_cmp_si(gcd, 1) <= 0) {
3543 isl_int_clear(gcd);
3544 return compute_next_band(node, graph, 0);
3547 r = isl_vec_alloc(ctx, graph->n);
3548 order = isl_calloc_array(ctx, int, graph->n);
3549 if (!r || !order)
3550 goto error;
3552 for (i = 0; i < graph->n; ++i) {
3553 struct isl_sched_node *node = &graph->node[i];
3555 order[i] = i;
3556 isl_int_fdiv_r(r->el[i], node->sched->row[row][0], gcd);
3557 isl_int_fdiv_q(node->sched->row[row][0],
3558 node->sched->row[row][0], gcd);
3559 isl_int_mul(node->sched->row[row][0],
3560 node->sched->row[row][0], gcd);
3561 node->sched = isl_mat_scale_down_row(node->sched, row, gcd);
3562 if (!node->sched)
3563 goto error;
3566 if (isl_sort(order, graph->n, sizeof(order[0]), &smaller_value, r) < 0)
3567 goto error;
3569 scc = 0;
3570 for (i = 0; i < graph->n; ++i) {
3571 if (i > 0 && isl_int_ne(r->el[order[i - 1]], r->el[order[i]]))
3572 ++scc;
3573 graph->node[order[i]].scc = scc;
3575 graph->scc = ++scc;
3576 graph->weak = 0;
3578 isl_int_clear(gcd);
3579 isl_vec_free(r);
3580 free(order);
3582 if (update_edges(ctx, graph) < 0)
3583 return isl_schedule_node_free(node);
3584 node = insert_current_band(node, graph, 0);
3585 next_band(graph);
3587 node = isl_schedule_node_child(node, 0);
3588 node = compute_component_schedule(node, graph, 0);
3589 node = isl_schedule_node_parent(node);
3591 return node;
3592 error:
3593 isl_vec_free(r);
3594 free(order);
3595 isl_int_clear(gcd);
3596 return isl_schedule_node_free(node);
3599 /* Is the schedule row "sol" trivial on node "node"?
3600 * That is, is the solution zero on the dimensions orthogonal to
3601 * the previously found solutions?
3602 * Return 1 if the solution is trivial, 0 if it is not and -1 on error.
3604 * Each coefficient is represented as the difference between
3605 * two non-negative values in "sol". "sol" has been computed
3606 * in terms of the original iterators (i.e., without use of cmap).
3607 * We construct the schedule row s and write it as a linear
3608 * combination of (linear combinations of) previously computed schedule rows.
3609 * s = Q c or c = U s.
3610 * If the final entries of c are all zero, then the solution is trivial.
3612 static int is_trivial(struct isl_sched_node *node, __isl_keep isl_vec *sol)
3614 int i;
3615 int pos;
3616 int trivial;
3617 isl_ctx *ctx;
3618 isl_vec *node_sol;
3620 if (!sol)
3621 return -1;
3622 if (node->nvar == node->rank)
3623 return 0;
3625 ctx = isl_vec_get_ctx(sol);
3626 node_sol = isl_vec_alloc(ctx, node->nvar);
3627 if (!node_sol)
3628 return -1;
3630 pos = 1 + node->start + 1 + 2 * node->nparam;
3632 for (i = 0; i < node->nvar; ++i)
3633 isl_int_sub(node_sol->el[i],
3634 sol->el[pos + 2 * i + 1], sol->el[pos + 2 * i]);
3636 node_sol = isl_mat_vec_product(isl_mat_copy(node->cinv), node_sol);
3638 if (!node_sol)
3639 return -1;
3641 trivial = isl_seq_first_non_zero(node_sol->el + node->rank,
3642 node->nvar - node->rank) == -1;
3644 isl_vec_free(node_sol);
3646 return trivial;
3649 /* Is the schedule row "sol" trivial on any node where it should
3650 * not be trivial?
3651 * "sol" has been computed in terms of the original iterators
3652 * (i.e., without use of cmap).
3653 * Return 1 if any solution is trivial, 0 if they are not and -1 on error.
3655 static int is_any_trivial(struct isl_sched_graph *graph,
3656 __isl_keep isl_vec *sol)
3658 int i;
3660 for (i = 0; i < graph->n; ++i) {
3661 struct isl_sched_node *node = &graph->node[i];
3662 int trivial;
3664 if (!needs_row(graph, node))
3665 continue;
3666 trivial = is_trivial(node, sol);
3667 if (trivial < 0 || trivial)
3668 return trivial;
3671 return 0;
3674 /* Construct a schedule row for each node such that as many dependences
3675 * as possible are carried and then continue with the next band.
3677 * If the computed schedule row turns out to be trivial on one or
3678 * more nodes where it should not be trivial, then we throw it away
3679 * and try again on each component separately.
3681 * If there is only one component, then we accept the schedule row anyway,
3682 * but we do not consider it as a complete row and therefore do not
3683 * increment graph->n_row. Note that the ranks of the nodes that
3684 * do get a non-trivial schedule part will get updated regardless and
3685 * graph->maxvar is computed based on these ranks. The test for
3686 * whether more schedule rows are required in compute_schedule_wcc
3687 * is therefore not affected.
3689 * Insert a band corresponding to the schedule row at position "node"
3690 * of the schedule tree and continue with the construction of the schedule.
3691 * This insertion and the continued construction is performed by split_scaled
3692 * after optionally checking for non-trivial common divisors.
3694 static __isl_give isl_schedule_node *carry_dependences(
3695 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph)
3697 int i;
3698 int n_edge;
3699 int trivial;
3700 isl_ctx *ctx;
3701 isl_vec *sol;
3702 isl_basic_set *lp;
3704 if (!node)
3705 return NULL;
3707 n_edge = 0;
3708 for (i = 0; i < graph->n_edge; ++i)
3709 n_edge += graph->edge[i].map->n;
3711 ctx = isl_schedule_node_get_ctx(node);
3712 if (setup_carry_lp(ctx, graph) < 0)
3713 return isl_schedule_node_free(node);
3715 lp = isl_basic_set_copy(graph->lp);
3716 sol = isl_tab_basic_set_non_neg_lexmin(lp);
3717 if (!sol)
3718 return isl_schedule_node_free(node);
3720 if (sol->size == 0) {
3721 isl_vec_free(sol);
3722 isl_die(ctx, isl_error_internal,
3723 "error in schedule construction",
3724 return isl_schedule_node_free(node));
3727 isl_int_divexact(sol->el[1], sol->el[1], sol->el[0]);
3728 if (isl_int_cmp_si(sol->el[1], n_edge) >= 0) {
3729 isl_vec_free(sol);
3730 isl_die(ctx, isl_error_unknown,
3731 "unable to carry dependences",
3732 return isl_schedule_node_free(node));
3735 trivial = is_any_trivial(graph, sol);
3736 if (trivial < 0) {
3737 sol = isl_vec_free(sol);
3738 } else if (trivial && graph->scc > 1) {
3739 isl_vec_free(sol);
3740 return compute_component_schedule(node, graph, 1);
3743 if (update_schedule(graph, sol, 0, 0) < 0)
3744 return isl_schedule_node_free(node);
3745 if (trivial)
3746 graph->n_row--;
3748 return split_scaled(node, graph);
3751 /* Topologically sort statements mapped to the same schedule iteration
3752 * and add insert a sequence node in front of "node"
3753 * corresponding to this order.
3755 * If it turns out to be impossible to sort the statements apart,
3756 * because different dependences impose different orderings
3757 * on the statements, then we extend the schedule such that
3758 * it carries at least one more dependence.
3760 static __isl_give isl_schedule_node *sort_statements(
3761 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph)
3763 isl_ctx *ctx;
3764 isl_union_set_list *filters;
3766 if (!node)
3767 return NULL;
3769 ctx = isl_schedule_node_get_ctx(node);
3770 if (graph->n < 1)
3771 isl_die(ctx, isl_error_internal,
3772 "graph should have at least one node",
3773 return isl_schedule_node_free(node));
3775 if (graph->n == 1)
3776 return node;
3778 if (update_edges(ctx, graph) < 0)
3779 return isl_schedule_node_free(node);
3781 if (graph->n_edge == 0)
3782 return node;
3784 if (detect_sccs(ctx, graph) < 0)
3785 return isl_schedule_node_free(node);
3787 next_band(graph);
3788 if (graph->scc < graph->n)
3789 return carry_dependences(node, graph);
3791 filters = extract_sccs(ctx, graph);
3792 node = isl_schedule_node_insert_sequence(node, filters);
3794 return node;
3797 /* Are there any (non-empty) (conditional) validity edges in the graph?
3799 static int has_validity_edges(struct isl_sched_graph *graph)
3801 int i;
3803 for (i = 0; i < graph->n_edge; ++i) {
3804 int empty;
3806 empty = isl_map_plain_is_empty(graph->edge[i].map);
3807 if (empty < 0)
3808 return -1;
3809 if (empty)
3810 continue;
3811 if (graph->edge[i].validity ||
3812 graph->edge[i].conditional_validity)
3813 return 1;
3816 return 0;
3819 /* Should we apply a Feautrier step?
3820 * That is, did the user request the Feautrier algorithm and are
3821 * there any validity dependences (left)?
3823 static int need_feautrier_step(isl_ctx *ctx, struct isl_sched_graph *graph)
3825 if (ctx->opt->schedule_algorithm != ISL_SCHEDULE_ALGORITHM_FEAUTRIER)
3826 return 0;
3828 return has_validity_edges(graph);
3831 /* Compute a schedule for a connected dependence graph using Feautrier's
3832 * multi-dimensional scheduling algorithm and return the updated schedule node.
3834 * The original algorithm is described in [1].
3835 * The main idea is to minimize the number of scheduling dimensions, by
3836 * trying to satisfy as many dependences as possible per scheduling dimension.
3838 * [1] P. Feautrier, Some Efficient Solutions to the Affine Scheduling
3839 * Problem, Part II: Multi-Dimensional Time.
3840 * In Intl. Journal of Parallel Programming, 1992.
3842 static __isl_give isl_schedule_node *compute_schedule_wcc_feautrier(
3843 isl_schedule_node *node, struct isl_sched_graph *graph)
3845 return carry_dependences(node, graph);
3848 /* Turn off the "local" bit on all (condition) edges.
3850 static void clear_local_edges(struct isl_sched_graph *graph)
3852 int i;
3854 for (i = 0; i < graph->n_edge; ++i)
3855 if (graph->edge[i].condition)
3856 graph->edge[i].local = 0;
3859 /* Does "graph" have both condition and conditional validity edges?
3861 static int need_condition_check(struct isl_sched_graph *graph)
3863 int i;
3864 int any_condition = 0;
3865 int any_conditional_validity = 0;
3867 for (i = 0; i < graph->n_edge; ++i) {
3868 if (graph->edge[i].condition)
3869 any_condition = 1;
3870 if (graph->edge[i].conditional_validity)
3871 any_conditional_validity = 1;
3874 return any_condition && any_conditional_validity;
3877 /* Does "graph" contain any coincidence edge?
3879 static int has_any_coincidence(struct isl_sched_graph *graph)
3881 int i;
3883 for (i = 0; i < graph->n_edge; ++i)
3884 if (graph->edge[i].coincidence)
3885 return 1;
3887 return 0;
3890 /* Extract the final schedule row as a map with the iteration domain
3891 * of "node" as domain.
3893 static __isl_give isl_map *final_row(struct isl_sched_node *node)
3895 isl_local_space *ls;
3896 isl_aff *aff;
3897 int row;
3899 row = isl_mat_rows(node->sched) - 1;
3900 ls = isl_local_space_from_space(isl_space_copy(node->space));
3901 aff = extract_schedule_row(ls, node, row);
3902 return isl_map_from_aff(aff);
3905 /* Is the conditional validity dependence in the edge with index "edge_index"
3906 * violated by the latest (i.e., final) row of the schedule?
3907 * That is, is i scheduled after j
3908 * for any conditional validity dependence i -> j?
3910 static int is_violated(struct isl_sched_graph *graph, int edge_index)
3912 isl_map *src_sched, *dst_sched, *map;
3913 struct isl_sched_edge *edge = &graph->edge[edge_index];
3914 int empty;
3916 src_sched = final_row(edge->src);
3917 dst_sched = final_row(edge->dst);
3918 map = isl_map_copy(edge->map);
3919 map = isl_map_apply_domain(map, src_sched);
3920 map = isl_map_apply_range(map, dst_sched);
3921 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_out, 0);
3922 empty = isl_map_is_empty(map);
3923 isl_map_free(map);
3925 if (empty < 0)
3926 return -1;
3928 return !empty;
3931 /* Does "graph" have any satisfied condition edges that
3932 * are adjacent to the conditional validity constraint with
3933 * domain "conditional_source" and range "conditional_sink"?
3935 * A satisfied condition is one that is not local.
3936 * If a condition was forced to be local already (i.e., marked as local)
3937 * then there is no need to check if it is in fact local.
3939 * Additionally, mark all adjacent condition edges found as local.
3941 static int has_adjacent_true_conditions(struct isl_sched_graph *graph,
3942 __isl_keep isl_union_set *conditional_source,
3943 __isl_keep isl_union_set *conditional_sink)
3945 int i;
3946 int any = 0;
3948 for (i = 0; i < graph->n_edge; ++i) {
3949 int adjacent, local;
3950 isl_union_map *condition;
3952 if (!graph->edge[i].condition)
3953 continue;
3954 if (graph->edge[i].local)
3955 continue;
3957 condition = graph->edge[i].tagged_condition;
3958 adjacent = domain_intersects(condition, conditional_sink);
3959 if (adjacent >= 0 && !adjacent)
3960 adjacent = range_intersects(condition,
3961 conditional_source);
3962 if (adjacent < 0)
3963 return -1;
3964 if (!adjacent)
3965 continue;
3967 graph->edge[i].local = 1;
3969 local = is_condition_false(&graph->edge[i]);
3970 if (local < 0)
3971 return -1;
3972 if (!local)
3973 any = 1;
3976 return any;
3979 /* Are there any violated conditional validity dependences with
3980 * adjacent condition dependences that are not local with respect
3981 * to the current schedule?
3982 * That is, is the conditional validity constraint violated?
3984 * Additionally, mark all those adjacent condition dependences as local.
3985 * We also mark those adjacent condition dependences that were not marked
3986 * as local before, but just happened to be local already. This ensures
3987 * that they remain local if the schedule is recomputed.
3989 * We first collect domain and range of all violated conditional validity
3990 * dependences and then check if there are any adjacent non-local
3991 * condition dependences.
3993 static int has_violated_conditional_constraint(isl_ctx *ctx,
3994 struct isl_sched_graph *graph)
3996 int i;
3997 int any = 0;
3998 isl_union_set *source, *sink;
4000 source = isl_union_set_empty(isl_space_params_alloc(ctx, 0));
4001 sink = isl_union_set_empty(isl_space_params_alloc(ctx, 0));
4002 for (i = 0; i < graph->n_edge; ++i) {
4003 isl_union_set *uset;
4004 isl_union_map *umap;
4005 int violated;
4007 if (!graph->edge[i].conditional_validity)
4008 continue;
4010 violated = is_violated(graph, i);
4011 if (violated < 0)
4012 goto error;
4013 if (!violated)
4014 continue;
4016 any = 1;
4018 umap = isl_union_map_copy(graph->edge[i].tagged_validity);
4019 uset = isl_union_map_domain(umap);
4020 source = isl_union_set_union(source, uset);
4021 source = isl_union_set_coalesce(source);
4023 umap = isl_union_map_copy(graph->edge[i].tagged_validity);
4024 uset = isl_union_map_range(umap);
4025 sink = isl_union_set_union(sink, uset);
4026 sink = isl_union_set_coalesce(sink);
4029 if (any)
4030 any = has_adjacent_true_conditions(graph, source, sink);
4032 isl_union_set_free(source);
4033 isl_union_set_free(sink);
4034 return any;
4035 error:
4036 isl_union_set_free(source);
4037 isl_union_set_free(sink);
4038 return -1;
4041 /* Compute a schedule for a connected dependence graph and return
4042 * the updated schedule node.
4044 * We try to find a sequence of as many schedule rows as possible that result
4045 * in non-negative dependence distances (independent of the previous rows
4046 * in the sequence, i.e., such that the sequence is tilable), with as
4047 * many of the initial rows as possible satisfying the coincidence constraints.
4048 * If we can't find any more rows we either
4049 * - split between SCCs and start over (assuming we found an interesting
4050 * pair of SCCs between which to split)
4051 * - continue with the next band (assuming the current band has at least
4052 * one row)
4053 * - try to carry as many dependences as possible and continue with the next
4054 * band
4055 * In each case, we first insert a band node in the schedule tree
4056 * if any rows have been computed.
4058 * If Feautrier's algorithm is selected, we first recursively try to satisfy
4059 * as many validity dependences as possible. When all validity dependences
4060 * are satisfied we extend the schedule to a full-dimensional schedule.
4062 * If we manage to complete the schedule, we insert a band node
4063 * (if any schedule rows were computed) and we finish off by topologically
4064 * sorting the statements based on the remaining dependences.
4066 * If ctx->opt->schedule_outer_coincidence is set, then we force the
4067 * outermost dimension to satisfy the coincidence constraints. If this
4068 * turns out to be impossible, we fall back on the general scheme above
4069 * and try to carry as many dependences as possible.
4071 * If "graph" contains both condition and conditional validity dependences,
4072 * then we need to check that that the conditional schedule constraint
4073 * is satisfied, i.e., there are no violated conditional validity dependences
4074 * that are adjacent to any non-local condition dependences.
4075 * If there are, then we mark all those adjacent condition dependences
4076 * as local and recompute the current band. Those dependences that
4077 * are marked local will then be forced to be local.
4078 * The initial computation is performed with no dependences marked as local.
4079 * If we are lucky, then there will be no violated conditional validity
4080 * dependences adjacent to any non-local condition dependences.
4081 * Otherwise, we mark some additional condition dependences as local and
4082 * recompute. We continue this process until there are no violations left or
4083 * until we are no longer able to compute a schedule.
4084 * Since there are only a finite number of dependences,
4085 * there will only be a finite number of iterations.
4087 static __isl_give isl_schedule_node *compute_schedule_wcc(
4088 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph)
4090 int has_coincidence;
4091 int use_coincidence;
4092 int force_coincidence = 0;
4093 int check_conditional;
4094 int insert;
4095 isl_ctx *ctx;
4097 if (!node)
4098 return NULL;
4100 ctx = isl_schedule_node_get_ctx(node);
4101 if (detect_sccs(ctx, graph) < 0)
4102 return isl_schedule_node_free(node);
4103 if (sort_sccs(graph) < 0)
4104 return isl_schedule_node_free(node);
4106 if (compute_maxvar(graph) < 0)
4107 return isl_schedule_node_free(node);
4109 if (need_feautrier_step(ctx, graph))
4110 return compute_schedule_wcc_feautrier(node, graph);
4112 clear_local_edges(graph);
4113 check_conditional = need_condition_check(graph);
4114 has_coincidence = has_any_coincidence(graph);
4116 if (ctx->opt->schedule_outer_coincidence)
4117 force_coincidence = 1;
4119 use_coincidence = has_coincidence;
4120 while (graph->n_row < graph->maxvar) {
4121 isl_vec *sol;
4122 int violated;
4123 int coincident;
4125 graph->src_scc = -1;
4126 graph->dst_scc = -1;
4128 if (setup_lp(ctx, graph, use_coincidence) < 0)
4129 return isl_schedule_node_free(node);
4130 sol = solve_lp(graph);
4131 if (!sol)
4132 return isl_schedule_node_free(node);
4133 if (sol->size == 0) {
4134 int empty = graph->n_total_row == graph->band_start;
4136 isl_vec_free(sol);
4137 if (use_coincidence && (!force_coincidence || !empty)) {
4138 use_coincidence = 0;
4139 continue;
4141 if (!ctx->opt->schedule_maximize_band_depth && !empty)
4142 return compute_next_band(node, graph, 1);
4143 if (graph->src_scc >= 0)
4144 return compute_split_schedule(node, graph);
4145 if (!empty)
4146 return compute_next_band(node, graph, 1);
4147 return carry_dependences(node, graph);
4149 coincident = !has_coincidence || use_coincidence;
4150 if (update_schedule(graph, sol, 1, coincident) < 0)
4151 return isl_schedule_node_free(node);
4153 if (!check_conditional)
4154 continue;
4155 violated = has_violated_conditional_constraint(ctx, graph);
4156 if (violated < 0)
4157 return isl_schedule_node_free(node);
4158 if (!violated)
4159 continue;
4160 if (reset_band(graph) < 0)
4161 return isl_schedule_node_free(node);
4162 use_coincidence = has_coincidence;
4165 insert = graph->n_total_row > graph->band_start;
4166 if (insert) {
4167 node = insert_current_band(node, graph, 1);
4168 node = isl_schedule_node_child(node, 0);
4170 node = sort_statements(node, graph);
4171 if (insert)
4172 node = isl_schedule_node_parent(node);
4174 return node;
4177 /* Compute a schedule for each group of nodes identified by node->scc
4178 * separately and then combine them in a sequence node (or as set node
4179 * if graph->weak is set) inserted at position "node" of the schedule tree.
4180 * Return the updated schedule node.
4182 * If "wcc" is set then each of the groups belongs to a single
4183 * weakly connected component in the dependence graph so that
4184 * there is no need for compute_sub_schedule to look for weakly
4185 * connected components.
4187 static __isl_give isl_schedule_node *compute_component_schedule(
4188 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph,
4189 int wcc)
4191 int component, i;
4192 int n, n_edge;
4193 int orig_total_row;
4194 isl_ctx *ctx;
4195 isl_union_set_list *filters;
4197 if (!node)
4198 return NULL;
4199 ctx = isl_schedule_node_get_ctx(node);
4201 filters = extract_sccs(ctx, graph);
4202 if (graph->weak)
4203 node = isl_schedule_node_insert_set(node, filters);
4204 else
4205 node = isl_schedule_node_insert_sequence(node, filters);
4207 orig_total_row = graph->n_total_row;
4208 for (component = 0; component < graph->scc; ++component) {
4209 n = 0;
4210 for (i = 0; i < graph->n; ++i)
4211 if (graph->node[i].scc == component)
4212 n++;
4213 n_edge = 0;
4214 for (i = 0; i < graph->n_edge; ++i)
4215 if (graph->edge[i].src->scc == component &&
4216 graph->edge[i].dst->scc == component)
4217 n_edge++;
4219 node = isl_schedule_node_child(node, component);
4220 node = isl_schedule_node_child(node, 0);
4221 node = compute_sub_schedule(node, ctx, graph, n, n_edge,
4222 &node_scc_exactly,
4223 &edge_scc_exactly, component, wcc);
4224 node = isl_schedule_node_parent(node);
4225 node = isl_schedule_node_parent(node);
4226 graph->n_total_row = orig_total_row;
4229 return node;
4232 /* Compute a schedule for the given dependence graph and insert it at "node".
4233 * Return the updated schedule node.
4235 * We first check if the graph is connected (through validity and conditional
4236 * validity dependences) and, if not, compute a schedule
4237 * for each component separately.
4238 * If schedule_fuse is set to minimal fusion, then we check for strongly
4239 * connected components instead and compute a separate schedule for
4240 * each such strongly connected component.
4242 static __isl_give isl_schedule_node *compute_schedule(isl_schedule_node *node,
4243 struct isl_sched_graph *graph)
4245 isl_ctx *ctx;
4247 if (!node)
4248 return NULL;
4250 ctx = isl_schedule_node_get_ctx(node);
4251 if (ctx->opt->schedule_fuse == ISL_SCHEDULE_FUSE_MIN) {
4252 if (detect_sccs(ctx, graph) < 0)
4253 return isl_schedule_node_free(node);
4254 } else {
4255 if (detect_wccs(ctx, graph) < 0)
4256 return isl_schedule_node_free(node);
4259 if (graph->scc > 1)
4260 return compute_component_schedule(node, graph, 1);
4262 return compute_schedule_wcc(node, graph);
4265 /* Compute a schedule on sc->domain that respects the given schedule
4266 * constraints.
4268 * In particular, the schedule respects all the validity dependences.
4269 * If the default isl scheduling algorithm is used, it tries to minimize
4270 * the dependence distances over the proximity dependences.
4271 * If Feautrier's scheduling algorithm is used, the proximity dependence
4272 * distances are only minimized during the extension to a full-dimensional
4273 * schedule.
4275 * If there are any condition and conditional validity dependences,
4276 * then the conditional validity dependences may be violated inside
4277 * a tilable band, provided they have no adjacent non-local
4278 * condition dependences.
4280 * The context is included in the domain before the nodes of
4281 * the graphs are extracted in order to be able to exploit
4282 * any possible additional equalities.
4283 * However, the returned schedule contains the original domain
4284 * (before this intersection).
4286 __isl_give isl_schedule *isl_schedule_constraints_compute_schedule(
4287 __isl_take isl_schedule_constraints *sc)
4289 isl_ctx *ctx = isl_schedule_constraints_get_ctx(sc);
4290 struct isl_sched_graph graph = { 0 };
4291 isl_schedule *sched;
4292 isl_schedule_node *node;
4293 isl_union_set *domain;
4294 struct isl_extract_edge_data data;
4295 enum isl_edge_type i;
4296 int r;
4298 sc = isl_schedule_constraints_align_params(sc);
4299 if (!sc)
4300 return NULL;
4302 graph.n = isl_union_set_n_set(sc->domain);
4303 if (graph.n == 0) {
4304 isl_union_set *domain = isl_union_set_copy(sc->domain);
4305 sched = isl_schedule_from_domain(domain);
4306 goto done;
4308 if (graph_alloc(ctx, &graph, graph.n,
4309 isl_schedule_constraints_n_map(sc)) < 0)
4310 goto error;
4311 if (compute_max_row(&graph, sc) < 0)
4312 goto error;
4313 graph.root = 1;
4314 graph.n = 0;
4315 domain = isl_union_set_copy(sc->domain);
4316 domain = isl_union_set_intersect_params(domain,
4317 isl_set_copy(sc->context));
4318 r = isl_union_set_foreach_set(domain, &extract_node, &graph);
4319 isl_union_set_free(domain);
4320 if (r < 0)
4321 goto error;
4322 if (graph_init_table(ctx, &graph) < 0)
4323 goto error;
4324 for (i = isl_edge_first; i <= isl_edge_last; ++i)
4325 graph.max_edge[i] = isl_union_map_n_map(sc->constraint[i]);
4326 if (graph_init_edge_tables(ctx, &graph) < 0)
4327 goto error;
4328 graph.n_edge = 0;
4329 data.graph = &graph;
4330 for (i = isl_edge_first; i <= isl_edge_last; ++i) {
4331 data.type = i;
4332 if (isl_union_map_foreach_map(sc->constraint[i],
4333 &extract_edge, &data) < 0)
4334 goto error;
4337 node = isl_schedule_node_from_domain(isl_union_set_copy(sc->domain));
4338 node = isl_schedule_node_child(node, 0);
4339 node = compute_schedule(node, &graph);
4340 sched = isl_schedule_node_get_schedule(node);
4341 isl_schedule_node_free(node);
4343 done:
4344 graph_free(ctx, &graph);
4345 isl_schedule_constraints_free(sc);
4347 return sched;
4348 error:
4349 graph_free(ctx, &graph);
4350 isl_schedule_constraints_free(sc);
4351 return NULL;
4354 /* Compute a schedule for the given union of domains that respects
4355 * all the validity dependences and minimizes
4356 * the dependence distances over the proximity dependences.
4358 * This function is kept for backward compatibility.
4360 __isl_give isl_schedule *isl_union_set_compute_schedule(
4361 __isl_take isl_union_set *domain,
4362 __isl_take isl_union_map *validity,
4363 __isl_take isl_union_map *proximity)
4365 isl_schedule_constraints *sc;
4367 sc = isl_schedule_constraints_on_domain(domain);
4368 sc = isl_schedule_constraints_set_validity(sc, validity);
4369 sc = isl_schedule_constraints_set_proximity(sc, proximity);
4371 return isl_schedule_constraints_compute_schedule(sc);