isl_scheduler.c: is_condition_false: allow edge->tagged_condition to be empty
[isl.git] / isl_scheduler.c
blob22f44f4f2a9db507a99fb9bd6a5985d6cc36051e
1 /*
2 * Copyright 2011 INRIA Saclay
3 * Copyright 2012-2014 Ecole Normale Superieure
5 * Use of this software is governed by the MIT license
7 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
8 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
9 * 91893 Orsay, France
10 * and Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
13 #include <isl_ctx_private.h>
14 #include <isl_map_private.h>
15 #include <isl_space_private.h>
16 #include <isl_aff_private.h>
17 #include <isl/hash.h>
18 #include <isl/constraint.h>
19 #include <isl/schedule.h>
20 #include <isl_mat_private.h>
21 #include <isl_vec_private.h>
22 #include <isl/set.h>
23 #include <isl_seq.h>
24 #include <isl_tab.h>
25 #include <isl_dim_map.h>
26 #include <isl/map_to_basic_set.h>
27 #include <isl_sort.h>
28 #include <isl_schedule_private.h>
29 #include <isl_options_private.h>
30 #include <isl_tarjan.h>
31 #include <isl_morph.h>
34 * The scheduling algorithm implemented in this file was inspired by
35 * Bondhugula et al., "Automatic Transformations for Communication-Minimized
36 * Parallelization and Locality Optimization in the Polyhedral Model".
39 enum isl_edge_type {
40 isl_edge_validity = 0,
41 isl_edge_first = isl_edge_validity,
42 isl_edge_coincidence,
43 isl_edge_condition,
44 isl_edge_conditional_validity,
45 isl_edge_proximity,
46 isl_edge_last = isl_edge_proximity
49 /* The constraints that need to be satisfied by a schedule on "domain".
51 * "validity" constraints map domain elements i to domain elements
52 * that should be scheduled after i. (Hard constraint)
53 * "proximity" constraints map domain elements i to domains elements
54 * that should be scheduled as early as possible after i (or before i).
55 * (Soft constraint)
57 * "condition" and "conditional_validity" constraints map possibly "tagged"
58 * domain elements i -> s to "tagged" domain elements j -> t.
59 * The elements of the "conditional_validity" constraints, but without the
60 * tags (i.e., the elements i -> j) are treated as validity constraints,
61 * except that during the construction of a tilable band,
62 * the elements of the "conditional_validity" constraints may be violated
63 * provided that all adjacent elements of the "condition" constraints
64 * are local within the band.
65 * A dependence is local within a band if domain and range are mapped
66 * to the same schedule point by the band.
68 struct isl_schedule_constraints {
69 isl_union_set *domain;
71 isl_union_map *constraint[isl_edge_last + 1];
74 __isl_give isl_schedule_constraints *isl_schedule_constraints_copy(
75 __isl_keep isl_schedule_constraints *sc)
77 isl_ctx *ctx;
78 isl_schedule_constraints *sc_copy;
79 enum isl_edge_type i;
81 ctx = isl_union_set_get_ctx(sc->domain);
82 sc_copy = isl_calloc_type(ctx, struct isl_schedule_constraints);
83 if (!sc_copy)
84 return NULL;
86 sc_copy->domain = isl_union_set_copy(sc->domain);
87 if (!sc_copy->domain)
88 return isl_schedule_constraints_free(sc_copy);
90 for (i = isl_edge_first; i <= isl_edge_last; ++i) {
91 sc_copy->constraint[i] = isl_union_map_copy(sc->constraint[i]);
92 if (!sc_copy->constraint[i])
93 return isl_schedule_constraints_free(sc_copy);
96 return sc_copy;
100 /* Construct an isl_schedule_constraints object for computing a schedule
101 * on "domain". The initial object does not impose any constraints.
103 __isl_give isl_schedule_constraints *isl_schedule_constraints_on_domain(
104 __isl_take isl_union_set *domain)
106 isl_ctx *ctx;
107 isl_space *space;
108 isl_schedule_constraints *sc;
109 isl_union_map *empty;
110 enum isl_edge_type i;
112 if (!domain)
113 return NULL;
115 ctx = isl_union_set_get_ctx(domain);
116 sc = isl_calloc_type(ctx, struct isl_schedule_constraints);
117 if (!sc)
118 goto error;
120 space = isl_union_set_get_space(domain);
121 sc->domain = domain;
122 empty = isl_union_map_empty(space);
123 for (i = isl_edge_first; i <= isl_edge_last; ++i) {
124 sc->constraint[i] = isl_union_map_copy(empty);
125 if (!sc->constraint[i])
126 sc->domain = isl_union_set_free(sc->domain);
128 isl_union_map_free(empty);
130 if (!sc->domain)
131 return isl_schedule_constraints_free(sc);
133 return sc;
134 error:
135 isl_union_set_free(domain);
136 return NULL;
139 /* Replace the validity constraints of "sc" by "validity".
141 __isl_give isl_schedule_constraints *isl_schedule_constraints_set_validity(
142 __isl_take isl_schedule_constraints *sc,
143 __isl_take isl_union_map *validity)
145 if (!sc || !validity)
146 goto error;
148 isl_union_map_free(sc->constraint[isl_edge_validity]);
149 sc->constraint[isl_edge_validity] = validity;
151 return sc;
152 error:
153 isl_schedule_constraints_free(sc);
154 isl_union_map_free(validity);
155 return NULL;
158 /* Replace the coincidence constraints of "sc" by "coincidence".
160 __isl_give isl_schedule_constraints *isl_schedule_constraints_set_coincidence(
161 __isl_take isl_schedule_constraints *sc,
162 __isl_take isl_union_map *coincidence)
164 if (!sc || !coincidence)
165 goto error;
167 isl_union_map_free(sc->constraint[isl_edge_coincidence]);
168 sc->constraint[isl_edge_coincidence] = coincidence;
170 return sc;
171 error:
172 isl_schedule_constraints_free(sc);
173 isl_union_map_free(coincidence);
174 return NULL;
177 /* Replace the proximity constraints of "sc" by "proximity".
179 __isl_give isl_schedule_constraints *isl_schedule_constraints_set_proximity(
180 __isl_take isl_schedule_constraints *sc,
181 __isl_take isl_union_map *proximity)
183 if (!sc || !proximity)
184 goto error;
186 isl_union_map_free(sc->constraint[isl_edge_proximity]);
187 sc->constraint[isl_edge_proximity] = proximity;
189 return sc;
190 error:
191 isl_schedule_constraints_free(sc);
192 isl_union_map_free(proximity);
193 return NULL;
196 /* Replace the conditional validity constraints of "sc" by "condition"
197 * and "validity".
199 __isl_give isl_schedule_constraints *
200 isl_schedule_constraints_set_conditional_validity(
201 __isl_take isl_schedule_constraints *sc,
202 __isl_take isl_union_map *condition,
203 __isl_take isl_union_map *validity)
205 if (!sc || !condition || !validity)
206 goto error;
208 isl_union_map_free(sc->constraint[isl_edge_condition]);
209 sc->constraint[isl_edge_condition] = condition;
210 isl_union_map_free(sc->constraint[isl_edge_conditional_validity]);
211 sc->constraint[isl_edge_conditional_validity] = validity;
213 return sc;
214 error:
215 isl_schedule_constraints_free(sc);
216 isl_union_map_free(condition);
217 isl_union_map_free(validity);
218 return NULL;
221 __isl_null isl_schedule_constraints *isl_schedule_constraints_free(
222 __isl_take isl_schedule_constraints *sc)
224 enum isl_edge_type i;
226 if (!sc)
227 return NULL;
229 isl_union_set_free(sc->domain);
230 for (i = isl_edge_first; i <= isl_edge_last; ++i)
231 isl_union_map_free(sc->constraint[i]);
233 free(sc);
235 return NULL;
238 isl_ctx *isl_schedule_constraints_get_ctx(
239 __isl_keep isl_schedule_constraints *sc)
241 return sc ? isl_union_set_get_ctx(sc->domain) : NULL;
244 void isl_schedule_constraints_dump(__isl_keep isl_schedule_constraints *sc)
246 if (!sc)
247 return;
249 fprintf(stderr, "domain: ");
250 isl_union_set_dump(sc->domain);
251 fprintf(stderr, "validity: ");
252 isl_union_map_dump(sc->constraint[isl_edge_validity]);
253 fprintf(stderr, "proximity: ");
254 isl_union_map_dump(sc->constraint[isl_edge_proximity]);
255 fprintf(stderr, "coincidence: ");
256 isl_union_map_dump(sc->constraint[isl_edge_coincidence]);
257 fprintf(stderr, "condition: ");
258 isl_union_map_dump(sc->constraint[isl_edge_condition]);
259 fprintf(stderr, "conditional_validity: ");
260 isl_union_map_dump(sc->constraint[isl_edge_conditional_validity]);
263 /* Align the parameters of the fields of "sc".
265 static __isl_give isl_schedule_constraints *
266 isl_schedule_constraints_align_params(__isl_take isl_schedule_constraints *sc)
268 isl_space *space;
269 enum isl_edge_type i;
271 if (!sc)
272 return NULL;
274 space = isl_union_set_get_space(sc->domain);
275 for (i = isl_edge_first; i <= isl_edge_last; ++i)
276 space = isl_space_align_params(space,
277 isl_union_map_get_space(sc->constraint[i]));
279 for (i = isl_edge_first; i <= isl_edge_last; ++i) {
280 sc->constraint[i] = isl_union_map_align_params(
281 sc->constraint[i], isl_space_copy(space));
282 if (!sc->constraint[i])
283 space = isl_space_free(space);
285 sc->domain = isl_union_set_align_params(sc->domain, space);
286 if (!sc->domain)
287 return isl_schedule_constraints_free(sc);
289 return sc;
292 /* Return the total number of isl_maps in the constraints of "sc".
294 static __isl_give int isl_schedule_constraints_n_map(
295 __isl_keep isl_schedule_constraints *sc)
297 enum isl_edge_type i;
298 int n = 0;
300 for (i = isl_edge_first; i <= isl_edge_last; ++i)
301 n += isl_union_map_n_map(sc->constraint[i]);
303 return n;
306 /* Internal information about a node that is used during the construction
307 * of a schedule.
308 * space represents the space in which the domain lives
309 * sched is a matrix representation of the schedule being constructed
310 * for this node; if compressed is set, then this schedule is
311 * defined over the compressed domain space
312 * sched_map is an isl_map representation of the same (partial) schedule
313 * sched_map may be NULL; if compressed is set, then this map
314 * is defined over the uncompressed domain space
315 * rank is the number of linearly independent rows in the linear part
316 * of sched
317 * the columns of cmap represent a change of basis for the schedule
318 * coefficients; the first rank columns span the linear part of
319 * the schedule rows
320 * cinv is the inverse of cmap.
321 * start is the first variable in the LP problem in the sequences that
322 * represents the schedule coefficients of this node
323 * nvar is the dimension of the domain
324 * nparam is the number of parameters or 0 if we are not constructing
325 * a parametric schedule
327 * If compressed is set, then hull represents the constraints
328 * that were used to derive the compression, while compress and
329 * decompress map the original space to the compressed space and
330 * vice versa.
332 * scc is the index of SCC (or WCC) this node belongs to
334 * band contains the band index for each of the rows of the schedule.
335 * band_id is used to differentiate between separate bands at the same
336 * level within the same parent band, i.e., bands that are separated
337 * by the parent band or bands that are independent of each other.
338 * coincident contains a boolean for each of the rows of the schedule,
339 * indicating whether the corresponding scheduling dimension satisfies
340 * the coincidence constraints in the sense that the corresponding
341 * dependence distances are zero.
343 struct isl_sched_node {
344 isl_space *space;
345 int compressed;
346 isl_set *hull;
347 isl_multi_aff *compress;
348 isl_multi_aff *decompress;
349 isl_mat *sched;
350 isl_map *sched_map;
351 int rank;
352 isl_mat *cmap;
353 isl_mat *cinv;
354 int start;
355 int nvar;
356 int nparam;
358 int scc;
360 int *band;
361 int *band_id;
362 int *coincident;
365 static int node_has_space(const void *entry, const void *val)
367 struct isl_sched_node *node = (struct isl_sched_node *)entry;
368 isl_space *dim = (isl_space *)val;
370 return isl_space_is_equal(node->space, dim);
373 /* An edge in the dependence graph. An edge may be used to
374 * ensure validity of the generated schedule, to minimize the dependence
375 * distance or both
377 * map is the dependence relation, with i -> j in the map if j depends on i
378 * tagged_condition and tagged_validity contain the union of all tagged
379 * condition or conditional validity dependence relations that
380 * specialize the dependence relation "map"; that is,
381 * if (i -> a) -> (j -> b) is an element of "tagged_condition"
382 * or "tagged_validity", then i -> j is an element of "map".
383 * If these fields are NULL, then they represent the empty relation.
384 * src is the source node
385 * dst is the sink node
386 * validity is set if the edge is used to ensure correctness
387 * coincidence is used to enforce zero dependence distances
388 * proximity is set if the edge is used to minimize dependence distances
389 * condition is set if the edge represents a condition
390 * for a conditional validity schedule constraint
391 * local can only be set for condition edges and indicates that
392 * the dependence distance over the edge should be zero
393 * conditional_validity is set if the edge is used to conditionally
394 * ensure correctness
396 * For validity edges, start and end mark the sequence of inequality
397 * constraints in the LP problem that encode the validity constraint
398 * corresponding to this edge.
400 struct isl_sched_edge {
401 isl_map *map;
402 isl_union_map *tagged_condition;
403 isl_union_map *tagged_validity;
405 struct isl_sched_node *src;
406 struct isl_sched_node *dst;
408 unsigned validity : 1;
409 unsigned coincidence : 1;
410 unsigned proximity : 1;
411 unsigned local : 1;
412 unsigned condition : 1;
413 unsigned conditional_validity : 1;
415 int start;
416 int end;
419 /* Internal information about the dependence graph used during
420 * the construction of the schedule.
422 * intra_hmap is a cache, mapping dependence relations to their dual,
423 * for dependences from a node to itself
424 * inter_hmap is a cache, mapping dependence relations to their dual,
425 * for dependences between distinct nodes
426 * if compression is involved then the key for these maps
427 * it the original, uncompressed dependence relation, while
428 * the value is the dual of the compressed dependence relation.
430 * n is the number of nodes
431 * node is the list of nodes
432 * maxvar is the maximal number of variables over all nodes
433 * max_row is the allocated number of rows in the schedule
434 * n_row is the current (maximal) number of linearly independent
435 * rows in the node schedules
436 * n_total_row is the current number of rows in the node schedules
437 * n_band is the current number of completed bands
438 * band_start is the starting row in the node schedules of the current band
439 * root is set if this graph is the original dependence graph,
440 * without any splitting
442 * sorted contains a list of node indices sorted according to the
443 * SCC to which a node belongs
445 * n_edge is the number of edges
446 * edge is the list of edges
447 * max_edge contains the maximal number of edges of each type;
448 * in particular, it contains the number of edges in the inital graph.
449 * edge_table contains pointers into the edge array, hashed on the source
450 * and sink spaces; there is one such table for each type;
451 * a given edge may be referenced from more than one table
452 * if the corresponding relation appears in more than of the
453 * sets of dependences
455 * node_table contains pointers into the node array, hashed on the space
457 * region contains a list of variable sequences that should be non-trivial
459 * lp contains the (I)LP problem used to obtain new schedule rows
461 * src_scc and dst_scc are the source and sink SCCs of an edge with
462 * conflicting constraints
464 * scc represents the number of components
466 struct isl_sched_graph {
467 isl_map_to_basic_set *intra_hmap;
468 isl_map_to_basic_set *inter_hmap;
470 struct isl_sched_node *node;
471 int n;
472 int maxvar;
473 int max_row;
474 int n_row;
476 int *sorted;
478 int n_band;
479 int n_total_row;
480 int band_start;
482 int root;
484 struct isl_sched_edge *edge;
485 int n_edge;
486 int max_edge[isl_edge_last + 1];
487 struct isl_hash_table *edge_table[isl_edge_last + 1];
489 struct isl_hash_table *node_table;
490 struct isl_region *region;
492 isl_basic_set *lp;
494 int src_scc;
495 int dst_scc;
497 int scc;
500 /* Initialize node_table based on the list of nodes.
502 static int graph_init_table(isl_ctx *ctx, struct isl_sched_graph *graph)
504 int i;
506 graph->node_table = isl_hash_table_alloc(ctx, graph->n);
507 if (!graph->node_table)
508 return -1;
510 for (i = 0; i < graph->n; ++i) {
511 struct isl_hash_table_entry *entry;
512 uint32_t hash;
514 hash = isl_space_get_hash(graph->node[i].space);
515 entry = isl_hash_table_find(ctx, graph->node_table, hash,
516 &node_has_space,
517 graph->node[i].space, 1);
518 if (!entry)
519 return -1;
520 entry->data = &graph->node[i];
523 return 0;
526 /* Return a pointer to the node that lives within the given space,
527 * or NULL if there is no such node.
529 static struct isl_sched_node *graph_find_node(isl_ctx *ctx,
530 struct isl_sched_graph *graph, __isl_keep isl_space *dim)
532 struct isl_hash_table_entry *entry;
533 uint32_t hash;
535 hash = isl_space_get_hash(dim);
536 entry = isl_hash_table_find(ctx, graph->node_table, hash,
537 &node_has_space, dim, 0);
539 return entry ? entry->data : NULL;
542 static int edge_has_src_and_dst(const void *entry, const void *val)
544 const struct isl_sched_edge *edge = entry;
545 const struct isl_sched_edge *temp = val;
547 return edge->src == temp->src && edge->dst == temp->dst;
550 /* Add the given edge to graph->edge_table[type].
552 static int graph_edge_table_add(isl_ctx *ctx, struct isl_sched_graph *graph,
553 enum isl_edge_type type, struct isl_sched_edge *edge)
555 struct isl_hash_table_entry *entry;
556 uint32_t hash;
558 hash = isl_hash_init();
559 hash = isl_hash_builtin(hash, edge->src);
560 hash = isl_hash_builtin(hash, edge->dst);
561 entry = isl_hash_table_find(ctx, graph->edge_table[type], hash,
562 &edge_has_src_and_dst, edge, 1);
563 if (!entry)
564 return -1;
565 entry->data = edge;
567 return 0;
570 /* Allocate the edge_tables based on the maximal number of edges of
571 * each type.
573 static int graph_init_edge_tables(isl_ctx *ctx, struct isl_sched_graph *graph)
575 int i;
577 for (i = 0; i <= isl_edge_last; ++i) {
578 graph->edge_table[i] = isl_hash_table_alloc(ctx,
579 graph->max_edge[i]);
580 if (!graph->edge_table[i])
581 return -1;
584 return 0;
587 /* If graph->edge_table[type] contains an edge from the given source
588 * to the given destination, then return the hash table entry of this edge.
589 * Otherwise, return NULL.
591 static struct isl_hash_table_entry *graph_find_edge_entry(
592 struct isl_sched_graph *graph,
593 enum isl_edge_type type,
594 struct isl_sched_node *src, struct isl_sched_node *dst)
596 isl_ctx *ctx = isl_space_get_ctx(src->space);
597 uint32_t hash;
598 struct isl_sched_edge temp = { .src = src, .dst = dst };
600 hash = isl_hash_init();
601 hash = isl_hash_builtin(hash, temp.src);
602 hash = isl_hash_builtin(hash, temp.dst);
603 return isl_hash_table_find(ctx, graph->edge_table[type], hash,
604 &edge_has_src_and_dst, &temp, 0);
608 /* If graph->edge_table[type] contains an edge from the given source
609 * to the given destination, then return this edge.
610 * Otherwise, return NULL.
612 static struct isl_sched_edge *graph_find_edge(struct isl_sched_graph *graph,
613 enum isl_edge_type type,
614 struct isl_sched_node *src, struct isl_sched_node *dst)
616 struct isl_hash_table_entry *entry;
618 entry = graph_find_edge_entry(graph, type, src, dst);
619 if (!entry)
620 return NULL;
622 return entry->data;
625 /* Check whether the dependence graph has an edge of the given type
626 * between the given two nodes.
628 static int graph_has_edge(struct isl_sched_graph *graph,
629 enum isl_edge_type type,
630 struct isl_sched_node *src, struct isl_sched_node *dst)
632 struct isl_sched_edge *edge;
633 int empty;
635 edge = graph_find_edge(graph, type, src, dst);
636 if (!edge)
637 return 0;
639 empty = isl_map_plain_is_empty(edge->map);
640 if (empty < 0)
641 return -1;
643 return !empty;
646 /* Look for any edge with the same src, dst and map fields as "model".
648 * Return the matching edge if one can be found.
649 * Return "model" if no matching edge is found.
650 * Return NULL on error.
652 static struct isl_sched_edge *graph_find_matching_edge(
653 struct isl_sched_graph *graph, struct isl_sched_edge *model)
655 enum isl_edge_type i;
656 struct isl_sched_edge *edge;
658 for (i = isl_edge_first; i <= isl_edge_last; ++i) {
659 int is_equal;
661 edge = graph_find_edge(graph, i, model->src, model->dst);
662 if (!edge)
663 continue;
664 is_equal = isl_map_plain_is_equal(model->map, edge->map);
665 if (is_equal < 0)
666 return NULL;
667 if (is_equal)
668 return edge;
671 return model;
674 /* Remove the given edge from all the edge_tables that refer to it.
676 static void graph_remove_edge(struct isl_sched_graph *graph,
677 struct isl_sched_edge *edge)
679 isl_ctx *ctx = isl_map_get_ctx(edge->map);
680 enum isl_edge_type i;
682 for (i = isl_edge_first; i <= isl_edge_last; ++i) {
683 struct isl_hash_table_entry *entry;
685 entry = graph_find_edge_entry(graph, i, edge->src, edge->dst);
686 if (!entry)
687 continue;
688 if (entry->data != edge)
689 continue;
690 isl_hash_table_remove(ctx, graph->edge_table[i], entry);
694 /* Check whether the dependence graph has any edge
695 * between the given two nodes.
697 static int graph_has_any_edge(struct isl_sched_graph *graph,
698 struct isl_sched_node *src, struct isl_sched_node *dst)
700 enum isl_edge_type i;
701 int r;
703 for (i = isl_edge_first; i <= isl_edge_last; ++i) {
704 r = graph_has_edge(graph, i, src, dst);
705 if (r < 0 || r)
706 return r;
709 return r;
712 /* Check whether the dependence graph has a validity edge
713 * between the given two nodes.
715 * Conditional validity edges are essentially validity edges that
716 * can be ignored if the corresponding condition edges are iteration private.
717 * Here, we are only checking for the presence of validity
718 * edges, so we need to consider the conditional validity edges too.
719 * In particular, this function is used during the detection
720 * of strongly connected components and we cannot ignore
721 * conditional validity edges during this detection.
723 static int graph_has_validity_edge(struct isl_sched_graph *graph,
724 struct isl_sched_node *src, struct isl_sched_node *dst)
726 int r;
728 r = graph_has_edge(graph, isl_edge_validity, src, dst);
729 if (r < 0 || r)
730 return r;
732 return graph_has_edge(graph, isl_edge_conditional_validity, src, dst);
735 static int graph_alloc(isl_ctx *ctx, struct isl_sched_graph *graph,
736 int n_node, int n_edge)
738 int i;
740 graph->n = n_node;
741 graph->n_edge = n_edge;
742 graph->node = isl_calloc_array(ctx, struct isl_sched_node, graph->n);
743 graph->sorted = isl_calloc_array(ctx, int, graph->n);
744 graph->region = isl_alloc_array(ctx, struct isl_region, graph->n);
745 graph->edge = isl_calloc_array(ctx,
746 struct isl_sched_edge, graph->n_edge);
748 graph->intra_hmap = isl_map_to_basic_set_alloc(ctx, 2 * n_edge);
749 graph->inter_hmap = isl_map_to_basic_set_alloc(ctx, 2 * n_edge);
751 if (!graph->node || !graph->region || (graph->n_edge && !graph->edge) ||
752 !graph->sorted)
753 return -1;
755 for(i = 0; i < graph->n; ++i)
756 graph->sorted[i] = i;
758 return 0;
761 static void graph_free(isl_ctx *ctx, struct isl_sched_graph *graph)
763 int i;
765 isl_map_to_basic_set_free(graph->intra_hmap);
766 isl_map_to_basic_set_free(graph->inter_hmap);
768 if (graph->node)
769 for (i = 0; i < graph->n; ++i) {
770 isl_space_free(graph->node[i].space);
771 isl_set_free(graph->node[i].hull);
772 isl_multi_aff_free(graph->node[i].compress);
773 isl_multi_aff_free(graph->node[i].decompress);
774 isl_mat_free(graph->node[i].sched);
775 isl_map_free(graph->node[i].sched_map);
776 isl_mat_free(graph->node[i].cmap);
777 isl_mat_free(graph->node[i].cinv);
778 if (graph->root) {
779 free(graph->node[i].band);
780 free(graph->node[i].band_id);
781 free(graph->node[i].coincident);
784 free(graph->node);
785 free(graph->sorted);
786 if (graph->edge)
787 for (i = 0; i < graph->n_edge; ++i) {
788 isl_map_free(graph->edge[i].map);
789 isl_union_map_free(graph->edge[i].tagged_condition);
790 isl_union_map_free(graph->edge[i].tagged_validity);
792 free(graph->edge);
793 free(graph->region);
794 for (i = 0; i <= isl_edge_last; ++i)
795 isl_hash_table_free(ctx, graph->edge_table[i]);
796 isl_hash_table_free(ctx, graph->node_table);
797 isl_basic_set_free(graph->lp);
800 /* For each "set" on which this function is called, increment
801 * graph->n by one and update graph->maxvar.
803 static int init_n_maxvar(__isl_take isl_set *set, void *user)
805 struct isl_sched_graph *graph = user;
806 int nvar = isl_set_dim(set, isl_dim_set);
808 graph->n++;
809 if (nvar > graph->maxvar)
810 graph->maxvar = nvar;
812 isl_set_free(set);
814 return 0;
817 /* Add the number of basic maps in "map" to *n.
819 static int add_n_basic_map(__isl_take isl_map *map, void *user)
821 int *n = user;
823 *n += isl_map_n_basic_map(map);
824 isl_map_free(map);
826 return 0;
829 /* Compute the number of rows that should be allocated for the schedule.
830 * The graph can be split at most "n - 1" times, there can be at most
831 * one row for each dimension in the iteration domains plus two rows
832 * for each basic map in the dependences (in particular,
833 * we usually have one row, but it may be split by split_scaled),
834 * and there can be one extra row for ordering the statements.
835 * Note that if we have actually split "n - 1" times, then no ordering
836 * is needed, so in principle we could use "graph->n + 2 * graph->maxvar - 1".
837 * It is also practically impossible to exhaust both the number of dependences
838 * and the number of variables.
840 static int compute_max_row(struct isl_sched_graph *graph,
841 __isl_keep isl_schedule_constraints *sc)
843 enum isl_edge_type i;
844 int n_edge;
846 graph->n = 0;
847 graph->maxvar = 0;
848 if (isl_union_set_foreach_set(sc->domain, &init_n_maxvar, graph) < 0)
849 return -1;
850 n_edge = 0;
851 for (i = isl_edge_first; i <= isl_edge_last; ++i)
852 if (isl_union_map_foreach_map(sc->constraint[i],
853 &add_n_basic_map, &n_edge) < 0)
854 return -1;
855 graph->max_row = graph->n + 2 * n_edge + graph->maxvar;
857 return 0;
860 /* Does "bset" have any defining equalities for its set variables?
862 static int has_any_defining_equality(__isl_keep isl_basic_set *bset)
864 int i, n;
866 if (!bset)
867 return -1;
869 n = isl_basic_set_dim(bset, isl_dim_set);
870 for (i = 0; i < n; ++i) {
871 int has;
873 has = isl_basic_set_has_defining_equality(bset, isl_dim_set, i,
874 NULL);
875 if (has < 0 || has)
876 return has;
879 return 0;
882 /* Add a new node to the graph representing the given space.
883 * "nvar" is the (possibly compressed) number of variables and
884 * may be smaller than then number of set variables in "space"
885 * if "compressed" is set.
886 * If "compressed" is set, then "hull" represents the constraints
887 * that were used to derive the compression, while "compress" and
888 * "decompress" map the original space to the compressed space and
889 * vice versa.
890 * If "compressed" is not set, then "hull", "compress" and "decompress"
891 * should be NULL.
893 static int add_node(struct isl_sched_graph *graph, __isl_take isl_space *space,
894 int nvar, int compressed, __isl_take isl_set *hull,
895 __isl_take isl_multi_aff *compress,
896 __isl_take isl_multi_aff *decompress)
898 int nparam;
899 isl_ctx *ctx;
900 isl_mat *sched;
901 int *band, *band_id, *coincident;
903 if (!space)
904 return -1;
906 ctx = isl_space_get_ctx(space);
907 nparam = isl_space_dim(space, isl_dim_param);
908 if (!ctx->opt->schedule_parametric)
909 nparam = 0;
910 sched = isl_mat_alloc(ctx, 0, 1 + nparam + nvar);
911 graph->node[graph->n].space = space;
912 graph->node[graph->n].nvar = nvar;
913 graph->node[graph->n].nparam = nparam;
914 graph->node[graph->n].sched = sched;
915 graph->node[graph->n].sched_map = NULL;
916 band = isl_alloc_array(ctx, int, graph->max_row);
917 graph->node[graph->n].band = band;
918 band_id = isl_calloc_array(ctx, int, graph->max_row);
919 graph->node[graph->n].band_id = band_id;
920 coincident = isl_calloc_array(ctx, int, graph->max_row);
921 graph->node[graph->n].coincident = coincident;
922 graph->node[graph->n].compressed = compressed;
923 graph->node[graph->n].hull = hull;
924 graph->node[graph->n].compress = compress;
925 graph->node[graph->n].decompress = decompress;
926 graph->n++;
928 if (!space || !sched ||
929 (graph->max_row && (!band || !band_id || !coincident)))
930 return -1;
931 if (compressed && (!hull || !compress || !decompress))
932 return -1;
934 return 0;
937 /* Add a new node to the graph representing the given set.
939 * If any of the set variables is defined by an equality, then
940 * we perform variable compression such that we can perform
941 * the scheduling on the compressed domain.
943 static int extract_node(__isl_take isl_set *set, void *user)
945 int nvar;
946 int has_equality;
947 isl_space *space;
948 isl_basic_set *hull;
949 isl_set *hull_set;
950 isl_morph *morph;
951 isl_multi_aff *compress, *decompress;
952 struct isl_sched_graph *graph = user;
954 space = isl_set_get_space(set);
955 hull = isl_set_affine_hull(set);
956 hull = isl_basic_set_remove_divs(hull);
957 nvar = isl_space_dim(space, isl_dim_set);
958 has_equality = has_any_defining_equality(hull);
960 if (has_equality < 0)
961 goto error;
962 if (!has_equality) {
963 isl_basic_set_free(hull);
964 return add_node(graph, space, nvar, 0, NULL, NULL, NULL);
967 morph = isl_basic_set_variable_compression(hull, isl_dim_set);
968 nvar = isl_morph_ran_dim(morph, isl_dim_set);
969 compress = isl_morph_get_var_multi_aff(morph);
970 morph = isl_morph_inverse(morph);
971 decompress = isl_morph_get_var_multi_aff(morph);
972 isl_morph_free(morph);
974 hull_set = isl_set_from_basic_set(hull);
975 return add_node(graph, space, nvar, 1, hull_set, compress, decompress);
976 error:
977 isl_basic_set_free(hull);
978 isl_space_free(space);
979 return -1;
982 struct isl_extract_edge_data {
983 enum isl_edge_type type;
984 struct isl_sched_graph *graph;
987 /* Merge edge2 into edge1, freeing the contents of edge2.
988 * "type" is the type of the schedule constraint from which edge2 was
989 * extracted.
990 * Return 0 on success and -1 on failure.
992 * edge1 and edge2 are assumed to have the same value for the map field.
994 static int merge_edge(enum isl_edge_type type, struct isl_sched_edge *edge1,
995 struct isl_sched_edge *edge2)
997 edge1->validity |= edge2->validity;
998 edge1->coincidence |= edge2->coincidence;
999 edge1->proximity |= edge2->proximity;
1000 edge1->condition |= edge2->condition;
1001 edge1->conditional_validity |= edge2->conditional_validity;
1002 isl_map_free(edge2->map);
1004 if (type == isl_edge_condition) {
1005 if (!edge1->tagged_condition)
1006 edge1->tagged_condition = edge2->tagged_condition;
1007 else
1008 edge1->tagged_condition =
1009 isl_union_map_union(edge1->tagged_condition,
1010 edge2->tagged_condition);
1013 if (type == isl_edge_conditional_validity) {
1014 if (!edge1->tagged_validity)
1015 edge1->tagged_validity = edge2->tagged_validity;
1016 else
1017 edge1->tagged_validity =
1018 isl_union_map_union(edge1->tagged_validity,
1019 edge2->tagged_validity);
1022 if (type == isl_edge_condition && !edge1->tagged_condition)
1023 return -1;
1024 if (type == isl_edge_conditional_validity && !edge1->tagged_validity)
1025 return -1;
1027 return 0;
1030 /* Insert dummy tags in domain and range of "map".
1032 * In particular, if "map" is of the form
1034 * A -> B
1036 * then return
1038 * [A -> dummy_tag] -> [B -> dummy_tag]
1040 * where the dummy_tags are identical and equal to any dummy tags
1041 * introduced by any other call to this function.
1043 static __isl_give isl_map *insert_dummy_tags(__isl_take isl_map *map)
1045 static char dummy;
1046 isl_ctx *ctx;
1047 isl_id *id;
1048 isl_space *space;
1049 isl_set *domain, *range;
1051 ctx = isl_map_get_ctx(map);
1053 id = isl_id_alloc(ctx, NULL, &dummy);
1054 space = isl_space_params(isl_map_get_space(map));
1055 space = isl_space_set_from_params(space);
1056 space = isl_space_set_tuple_id(space, isl_dim_set, id);
1057 space = isl_space_map_from_set(space);
1059 domain = isl_map_wrap(map);
1060 range = isl_map_wrap(isl_map_universe(space));
1061 map = isl_map_from_domain_and_range(domain, range);
1062 map = isl_map_zip(map);
1064 return map;
1067 /* Given that at least one of "src" or "dst" is compressed, return
1068 * a map between the spaces of these nodes restricted to the affine
1069 * hull that was used in the compression.
1071 static __isl_give isl_map *extract_hull(struct isl_sched_node *src,
1072 struct isl_sched_node *dst)
1074 isl_set *dom, *ran;
1076 if (src->compressed)
1077 dom = isl_set_copy(src->hull);
1078 else
1079 dom = isl_set_universe(isl_space_copy(src->space));
1080 if (dst->compressed)
1081 ran = isl_set_copy(dst->hull);
1082 else
1083 ran = isl_set_universe(isl_space_copy(dst->space));
1085 return isl_map_from_domain_and_range(dom, ran);
1088 /* Intersect the domains of the nested relations in domain and range
1089 * of "tagged" with "map".
1091 static __isl_give isl_map *map_intersect_domains(__isl_take isl_map *tagged,
1092 __isl_keep isl_map *map)
1094 isl_set *set;
1096 tagged = isl_map_zip(tagged);
1097 set = isl_map_wrap(isl_map_copy(map));
1098 tagged = isl_map_intersect_domain(tagged, set);
1099 tagged = isl_map_zip(tagged);
1100 return tagged;
1103 /* Add a new edge to the graph based on the given map
1104 * and add it to data->graph->edge_table[data->type].
1105 * If a dependence relation of a given type happens to be identical
1106 * to one of the dependence relations of a type that was added before,
1107 * then we don't create a new edge, but instead mark the original edge
1108 * as also representing a dependence of the current type.
1110 * Edges of type isl_edge_condition or isl_edge_conditional_validity
1111 * may be specified as "tagged" dependence relations. That is, "map"
1112 * may contain elements (i -> a) -> (j -> b), where i -> j denotes
1113 * the dependence on iterations and a and b are tags.
1114 * edge->map is set to the relation containing the elements i -> j,
1115 * while edge->tagged_condition and edge->tagged_validity contain
1116 * the union of all the "map" relations
1117 * for which extract_edge is called that result in the same edge->map.
1119 * If the source or the destination node is compressed, then
1120 * intersect both "map" and "tagged" with the constraints that
1121 * were used to construct the compression.
1122 * This ensures that there are no schedule constraints defined
1123 * outside of these domains, while the scheduler no longer has
1124 * any control over those outside parts.
1126 static int extract_edge(__isl_take isl_map *map, void *user)
1128 isl_ctx *ctx = isl_map_get_ctx(map);
1129 struct isl_extract_edge_data *data = user;
1130 struct isl_sched_graph *graph = data->graph;
1131 struct isl_sched_node *src, *dst;
1132 isl_space *dim;
1133 struct isl_sched_edge *edge;
1134 isl_map *tagged = NULL;
1136 if (data->type == isl_edge_condition ||
1137 data->type == isl_edge_conditional_validity) {
1138 if (isl_map_can_zip(map)) {
1139 tagged = isl_map_copy(map);
1140 map = isl_set_unwrap(isl_map_domain(isl_map_zip(map)));
1141 } else {
1142 tagged = insert_dummy_tags(isl_map_copy(map));
1146 dim = isl_space_domain(isl_map_get_space(map));
1147 src = graph_find_node(ctx, graph, dim);
1148 isl_space_free(dim);
1149 dim = isl_space_range(isl_map_get_space(map));
1150 dst = graph_find_node(ctx, graph, dim);
1151 isl_space_free(dim);
1153 if (!src || !dst) {
1154 isl_map_free(map);
1155 isl_map_free(tagged);
1156 return 0;
1159 if (src->compressed || dst->compressed) {
1160 isl_map *hull;
1161 hull = extract_hull(src, dst);
1162 if (tagged)
1163 tagged = map_intersect_domains(tagged, hull);
1164 map = isl_map_intersect(map, hull);
1167 graph->edge[graph->n_edge].src = src;
1168 graph->edge[graph->n_edge].dst = dst;
1169 graph->edge[graph->n_edge].map = map;
1170 graph->edge[graph->n_edge].validity = 0;
1171 graph->edge[graph->n_edge].coincidence = 0;
1172 graph->edge[graph->n_edge].proximity = 0;
1173 graph->edge[graph->n_edge].condition = 0;
1174 graph->edge[graph->n_edge].local = 0;
1175 graph->edge[graph->n_edge].conditional_validity = 0;
1176 graph->edge[graph->n_edge].tagged_condition = NULL;
1177 graph->edge[graph->n_edge].tagged_validity = NULL;
1178 if (data->type == isl_edge_validity)
1179 graph->edge[graph->n_edge].validity = 1;
1180 if (data->type == isl_edge_coincidence)
1181 graph->edge[graph->n_edge].coincidence = 1;
1182 if (data->type == isl_edge_proximity)
1183 graph->edge[graph->n_edge].proximity = 1;
1184 if (data->type == isl_edge_condition) {
1185 graph->edge[graph->n_edge].condition = 1;
1186 graph->edge[graph->n_edge].tagged_condition =
1187 isl_union_map_from_map(tagged);
1189 if (data->type == isl_edge_conditional_validity) {
1190 graph->edge[graph->n_edge].conditional_validity = 1;
1191 graph->edge[graph->n_edge].tagged_validity =
1192 isl_union_map_from_map(tagged);
1195 edge = graph_find_matching_edge(graph, &graph->edge[graph->n_edge]);
1196 if (!edge) {
1197 graph->n_edge++;
1198 return -1;
1200 if (edge == &graph->edge[graph->n_edge])
1201 return graph_edge_table_add(ctx, graph, data->type,
1202 &graph->edge[graph->n_edge++]);
1204 if (merge_edge(data->type, edge, &graph->edge[graph->n_edge]) < 0)
1205 return -1;
1207 return graph_edge_table_add(ctx, graph, data->type, edge);
1210 /* Check whether there is any dependence from node[j] to node[i]
1211 * or from node[i] to node[j].
1213 static int node_follows_weak(int i, int j, void *user)
1215 int f;
1216 struct isl_sched_graph *graph = user;
1218 f = graph_has_any_edge(graph, &graph->node[j], &graph->node[i]);
1219 if (f < 0 || f)
1220 return f;
1221 return graph_has_any_edge(graph, &graph->node[i], &graph->node[j]);
1224 /* Check whether there is a (conditional) validity dependence from node[j]
1225 * to node[i], forcing node[i] to follow node[j].
1227 static int node_follows_strong(int i, int j, void *user)
1229 struct isl_sched_graph *graph = user;
1231 return graph_has_validity_edge(graph, &graph->node[j], &graph->node[i]);
1234 /* Use Tarjan's algorithm for computing the strongly connected components
1235 * in the dependence graph (only validity edges).
1236 * If weak is set, we consider the graph to be undirected and
1237 * we effectively compute the (weakly) connected components.
1238 * Additionally, we also consider other edges when weak is set.
1240 static int detect_ccs(isl_ctx *ctx, struct isl_sched_graph *graph, int weak)
1242 int i, n;
1243 struct isl_tarjan_graph *g = NULL;
1245 g = isl_tarjan_graph_init(ctx, graph->n,
1246 weak ? &node_follows_weak : &node_follows_strong, graph);
1247 if (!g)
1248 return -1;
1250 graph->scc = 0;
1251 i = 0;
1252 n = graph->n;
1253 while (n) {
1254 while (g->order[i] != -1) {
1255 graph->node[g->order[i]].scc = graph->scc;
1256 --n;
1257 ++i;
1259 ++i;
1260 graph->scc++;
1263 isl_tarjan_graph_free(g);
1265 return 0;
1268 /* Apply Tarjan's algorithm to detect the strongly connected components
1269 * in the dependence graph.
1271 static int detect_sccs(isl_ctx *ctx, struct isl_sched_graph *graph)
1273 return detect_ccs(ctx, graph, 0);
1276 /* Apply Tarjan's algorithm to detect the (weakly) connected components
1277 * in the dependence graph.
1279 static int detect_wccs(isl_ctx *ctx, struct isl_sched_graph *graph)
1281 return detect_ccs(ctx, graph, 1);
1284 static int cmp_scc(const void *a, const void *b, void *data)
1286 struct isl_sched_graph *graph = data;
1287 const int *i1 = a;
1288 const int *i2 = b;
1290 return graph->node[*i1].scc - graph->node[*i2].scc;
1293 /* Sort the elements of graph->sorted according to the corresponding SCCs.
1295 static int sort_sccs(struct isl_sched_graph *graph)
1297 return isl_sort(graph->sorted, graph->n, sizeof(int), &cmp_scc, graph);
1300 /* Given a dependence relation R from "node" to itself,
1301 * construct the set of coefficients of valid constraints for elements
1302 * in that dependence relation.
1303 * In particular, the result contains tuples of coefficients
1304 * c_0, c_n, c_x such that
1306 * c_0 + c_n n + c_x y - c_x x >= 0 for each (x,y) in R
1308 * or, equivalently,
1310 * c_0 + c_n n + c_x d >= 0 for each d in delta R = { y - x | (x,y) in R }
1312 * We choose here to compute the dual of delta R.
1313 * Alternatively, we could have computed the dual of R, resulting
1314 * in a set of tuples c_0, c_n, c_x, c_y, and then
1315 * plugged in (c_0, c_n, c_x, -c_x).
1317 * If "node" has been compressed, then the dependence relation
1318 * is also compressed before the set of coefficients is computed.
1320 static __isl_give isl_basic_set *intra_coefficients(
1321 struct isl_sched_graph *graph, struct isl_sched_node *node,
1322 __isl_take isl_map *map)
1324 isl_set *delta;
1325 isl_map *key;
1326 isl_basic_set *coef;
1328 if (isl_map_to_basic_set_has(graph->intra_hmap, map))
1329 return isl_map_to_basic_set_get(graph->intra_hmap, map);
1331 key = isl_map_copy(map);
1332 if (node->compressed) {
1333 map = isl_map_preimage_domain_multi_aff(map,
1334 isl_multi_aff_copy(node->decompress));
1335 map = isl_map_preimage_range_multi_aff(map,
1336 isl_multi_aff_copy(node->decompress));
1338 delta = isl_set_remove_divs(isl_map_deltas(map));
1339 coef = isl_set_coefficients(delta);
1340 graph->intra_hmap = isl_map_to_basic_set_set(graph->intra_hmap, key,
1341 isl_basic_set_copy(coef));
1343 return coef;
1346 /* Given a dependence relation R, construct the set of coefficients
1347 * of valid constraints for elements in that dependence relation.
1348 * In particular, the result contains tuples of coefficients
1349 * c_0, c_n, c_x, c_y such that
1351 * c_0 + c_n n + c_x x + c_y y >= 0 for each (x,y) in R
1353 * If the source or destination nodes of "edge" have been compressed,
1354 * then the dependence relation is also compressed before
1355 * the set of coefficients is computed.
1357 static __isl_give isl_basic_set *inter_coefficients(
1358 struct isl_sched_graph *graph, struct isl_sched_edge *edge,
1359 __isl_take isl_map *map)
1361 isl_set *set;
1362 isl_map *key;
1363 isl_basic_set *coef;
1365 if (isl_map_to_basic_set_has(graph->inter_hmap, map))
1366 return isl_map_to_basic_set_get(graph->inter_hmap, map);
1368 key = isl_map_copy(map);
1369 if (edge->src->compressed)
1370 map = isl_map_preimage_domain_multi_aff(map,
1371 isl_multi_aff_copy(edge->src->decompress));
1372 if (edge->dst->compressed)
1373 map = isl_map_preimage_range_multi_aff(map,
1374 isl_multi_aff_copy(edge->dst->decompress));
1375 set = isl_map_wrap(isl_map_remove_divs(map));
1376 coef = isl_set_coefficients(set);
1377 graph->inter_hmap = isl_map_to_basic_set_set(graph->inter_hmap, key,
1378 isl_basic_set_copy(coef));
1380 return coef;
1383 /* Add constraints to graph->lp that force validity for the given
1384 * dependence from a node i to itself.
1385 * That is, add constraints that enforce
1387 * (c_i_0 + c_i_n n + c_i_x y) - (c_i_0 + c_i_n n + c_i_x x)
1388 * = c_i_x (y - x) >= 0
1390 * for each (x,y) in R.
1391 * We obtain general constraints on coefficients (c_0, c_n, c_x)
1392 * of valid constraints for (y - x) and then plug in (0, 0, c_i_x^+ - c_i_x^-),
1393 * where c_i_x = c_i_x^+ - c_i_x^-, with c_i_x^+ and c_i_x^- non-negative.
1394 * In graph->lp, the c_i_x^- appear before their c_i_x^+ counterpart.
1396 * Actually, we do not construct constraints for the c_i_x themselves,
1397 * but for the coefficients of c_i_x written as a linear combination
1398 * of the columns in node->cmap.
1400 static int add_intra_validity_constraints(struct isl_sched_graph *graph,
1401 struct isl_sched_edge *edge)
1403 unsigned total;
1404 isl_map *map = isl_map_copy(edge->map);
1405 isl_ctx *ctx = isl_map_get_ctx(map);
1406 isl_space *dim;
1407 isl_dim_map *dim_map;
1408 isl_basic_set *coef;
1409 struct isl_sched_node *node = edge->src;
1411 coef = intra_coefficients(graph, node, map);
1413 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
1415 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
1416 isl_space_dim(dim, isl_dim_set), isl_mat_copy(node->cmap));
1417 if (!coef)
1418 goto error;
1420 total = isl_basic_set_total_dim(graph->lp);
1421 dim_map = isl_dim_map_alloc(ctx, total);
1422 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 1, 2,
1423 isl_space_dim(dim, isl_dim_set), 1,
1424 node->nvar, -1);
1425 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 2, 2,
1426 isl_space_dim(dim, isl_dim_set), 1,
1427 node->nvar, 1);
1428 graph->lp = isl_basic_set_extend_constraints(graph->lp,
1429 coef->n_eq, coef->n_ineq);
1430 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
1431 coef, dim_map);
1432 isl_space_free(dim);
1434 return 0;
1435 error:
1436 isl_space_free(dim);
1437 return -1;
1440 /* Add constraints to graph->lp that force validity for the given
1441 * dependence from node i to node j.
1442 * That is, add constraints that enforce
1444 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x) >= 0
1446 * for each (x,y) in R.
1447 * We obtain general constraints on coefficients (c_0, c_n, c_x, c_y)
1448 * of valid constraints for R and then plug in
1449 * (c_j_0 - c_i_0, c_j_n^+ - c_j_n^- - (c_i_n^+ - c_i_n^-),
1450 * c_j_x^+ - c_j_x^- - (c_i_x^+ - c_i_x^-)),
1451 * where c_* = c_*^+ - c_*^-, with c_*^+ and c_*^- non-negative.
1452 * In graph->lp, the c_*^- appear before their c_*^+ counterpart.
1454 * Actually, we do not construct constraints for the c_*_x themselves,
1455 * but for the coefficients of c_*_x written as a linear combination
1456 * of the columns in node->cmap.
1458 static int add_inter_validity_constraints(struct isl_sched_graph *graph,
1459 struct isl_sched_edge *edge)
1461 unsigned total;
1462 isl_map *map = isl_map_copy(edge->map);
1463 isl_ctx *ctx = isl_map_get_ctx(map);
1464 isl_space *dim;
1465 isl_dim_map *dim_map;
1466 isl_basic_set *coef;
1467 struct isl_sched_node *src = edge->src;
1468 struct isl_sched_node *dst = edge->dst;
1470 coef = inter_coefficients(graph, edge, map);
1472 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
1474 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
1475 isl_space_dim(dim, isl_dim_set), isl_mat_copy(src->cmap));
1476 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
1477 isl_space_dim(dim, isl_dim_set) + src->nvar,
1478 isl_mat_copy(dst->cmap));
1479 if (!coef)
1480 goto error;
1482 total = isl_basic_set_total_dim(graph->lp);
1483 dim_map = isl_dim_map_alloc(ctx, total);
1485 isl_dim_map_range(dim_map, dst->start, 0, 0, 0, 1, 1);
1486 isl_dim_map_range(dim_map, dst->start + 1, 2, 1, 1, dst->nparam, -1);
1487 isl_dim_map_range(dim_map, dst->start + 2, 2, 1, 1, dst->nparam, 1);
1488 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 1, 2,
1489 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
1490 dst->nvar, -1);
1491 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 2, 2,
1492 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
1493 dst->nvar, 1);
1495 isl_dim_map_range(dim_map, src->start, 0, 0, 0, 1, -1);
1496 isl_dim_map_range(dim_map, src->start + 1, 2, 1, 1, src->nparam, 1);
1497 isl_dim_map_range(dim_map, src->start + 2, 2, 1, 1, src->nparam, -1);
1498 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 1, 2,
1499 isl_space_dim(dim, isl_dim_set), 1,
1500 src->nvar, 1);
1501 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 2, 2,
1502 isl_space_dim(dim, isl_dim_set), 1,
1503 src->nvar, -1);
1505 edge->start = graph->lp->n_ineq;
1506 graph->lp = isl_basic_set_extend_constraints(graph->lp,
1507 coef->n_eq, coef->n_ineq);
1508 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
1509 coef, dim_map);
1510 if (!graph->lp)
1511 goto error;
1512 isl_space_free(dim);
1513 edge->end = graph->lp->n_ineq;
1515 return 0;
1516 error:
1517 isl_space_free(dim);
1518 return -1;
1521 /* Add constraints to graph->lp that bound the dependence distance for the given
1522 * dependence from a node i to itself.
1523 * If s = 1, we add the constraint
1525 * c_i_x (y - x) <= m_0 + m_n n
1527 * or
1529 * -c_i_x (y - x) + m_0 + m_n n >= 0
1531 * for each (x,y) in R.
1532 * If s = -1, we add the constraint
1534 * -c_i_x (y - x) <= m_0 + m_n n
1536 * or
1538 * c_i_x (y - x) + m_0 + m_n n >= 0
1540 * for each (x,y) in R.
1541 * We obtain general constraints on coefficients (c_0, c_n, c_x)
1542 * of valid constraints for (y - x) and then plug in (m_0, m_n, -s * c_i_x),
1543 * with each coefficient (except m_0) represented as a pair of non-negative
1544 * coefficients.
1546 * Actually, we do not construct constraints for the c_i_x themselves,
1547 * but for the coefficients of c_i_x written as a linear combination
1548 * of the columns in node->cmap.
1551 * If "local" is set, then we add constraints
1553 * c_i_x (y - x) <= 0
1555 * or
1557 * -c_i_x (y - x) <= 0
1559 * instead, forcing the dependence distance to be (less than or) equal to 0.
1560 * That is, we plug in (0, 0, -s * c_i_x),
1561 * Note that dependences marked local are treated as validity constraints
1562 * by add_all_validity_constraints and therefore also have
1563 * their distances bounded by 0 from below.
1565 static int add_intra_proximity_constraints(struct isl_sched_graph *graph,
1566 struct isl_sched_edge *edge, int s, int local)
1568 unsigned total;
1569 unsigned nparam;
1570 isl_map *map = isl_map_copy(edge->map);
1571 isl_ctx *ctx = isl_map_get_ctx(map);
1572 isl_space *dim;
1573 isl_dim_map *dim_map;
1574 isl_basic_set *coef;
1575 struct isl_sched_node *node = edge->src;
1577 coef = intra_coefficients(graph, node, map);
1579 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
1581 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
1582 isl_space_dim(dim, isl_dim_set), isl_mat_copy(node->cmap));
1583 if (!coef)
1584 goto error;
1586 nparam = isl_space_dim(node->space, isl_dim_param);
1587 total = isl_basic_set_total_dim(graph->lp);
1588 dim_map = isl_dim_map_alloc(ctx, total);
1590 if (!local) {
1591 isl_dim_map_range(dim_map, 1, 0, 0, 0, 1, 1);
1592 isl_dim_map_range(dim_map, 4, 2, 1, 1, nparam, -1);
1593 isl_dim_map_range(dim_map, 5, 2, 1, 1, nparam, 1);
1595 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 1, 2,
1596 isl_space_dim(dim, isl_dim_set), 1,
1597 node->nvar, s);
1598 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 2, 2,
1599 isl_space_dim(dim, isl_dim_set), 1,
1600 node->nvar, -s);
1601 graph->lp = isl_basic_set_extend_constraints(graph->lp,
1602 coef->n_eq, coef->n_ineq);
1603 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
1604 coef, dim_map);
1605 isl_space_free(dim);
1607 return 0;
1608 error:
1609 isl_space_free(dim);
1610 return -1;
1613 /* Add constraints to graph->lp that bound the dependence distance for the given
1614 * dependence from node i to node j.
1615 * If s = 1, we add the constraint
1617 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x)
1618 * <= m_0 + m_n n
1620 * or
1622 * -(c_j_0 + c_j_n n + c_j_x y) + (c_i_0 + c_i_n n + c_i_x x) +
1623 * m_0 + m_n n >= 0
1625 * for each (x,y) in R.
1626 * If s = -1, we add the constraint
1628 * -((c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x))
1629 * <= m_0 + m_n n
1631 * or
1633 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x) +
1634 * m_0 + m_n n >= 0
1636 * for each (x,y) in R.
1637 * We obtain general constraints on coefficients (c_0, c_n, c_x, c_y)
1638 * of valid constraints for R and then plug in
1639 * (m_0 - s*c_j_0 + s*c_i_0, m_n - s*c_j_n + s*c_i_n,
1640 * -s*c_j_x+s*c_i_x)
1641 * with each coefficient (except m_0, c_j_0 and c_i_0)
1642 * represented as a pair of non-negative coefficients.
1644 * Actually, we do not construct constraints for the c_*_x themselves,
1645 * but for the coefficients of c_*_x written as a linear combination
1646 * of the columns in node->cmap.
1649 * If "local" is set, then we add constraints
1651 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x) <= 0
1653 * or
1655 * -((c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x)) <= 0
1657 * instead, forcing the dependence distance to be (less than or) equal to 0.
1658 * That is, we plug in
1659 * (-s*c_j_0 + s*c_i_0, -s*c_j_n + s*c_i_n, -s*c_j_x+s*c_i_x).
1660 * Note that dependences marked local are treated as validity constraints
1661 * by add_all_validity_constraints and therefore also have
1662 * their distances bounded by 0 from below.
1664 static int add_inter_proximity_constraints(struct isl_sched_graph *graph,
1665 struct isl_sched_edge *edge, int s, int local)
1667 unsigned total;
1668 unsigned nparam;
1669 isl_map *map = isl_map_copy(edge->map);
1670 isl_ctx *ctx = isl_map_get_ctx(map);
1671 isl_space *dim;
1672 isl_dim_map *dim_map;
1673 isl_basic_set *coef;
1674 struct isl_sched_node *src = edge->src;
1675 struct isl_sched_node *dst = edge->dst;
1677 coef = inter_coefficients(graph, edge, map);
1679 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
1681 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
1682 isl_space_dim(dim, isl_dim_set), isl_mat_copy(src->cmap));
1683 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
1684 isl_space_dim(dim, isl_dim_set) + src->nvar,
1685 isl_mat_copy(dst->cmap));
1686 if (!coef)
1687 goto error;
1689 nparam = isl_space_dim(src->space, isl_dim_param);
1690 total = isl_basic_set_total_dim(graph->lp);
1691 dim_map = isl_dim_map_alloc(ctx, total);
1693 if (!local) {
1694 isl_dim_map_range(dim_map, 1, 0, 0, 0, 1, 1);
1695 isl_dim_map_range(dim_map, 4, 2, 1, 1, nparam, -1);
1696 isl_dim_map_range(dim_map, 5, 2, 1, 1, nparam, 1);
1699 isl_dim_map_range(dim_map, dst->start, 0, 0, 0, 1, -s);
1700 isl_dim_map_range(dim_map, dst->start + 1, 2, 1, 1, dst->nparam, s);
1701 isl_dim_map_range(dim_map, dst->start + 2, 2, 1, 1, dst->nparam, -s);
1702 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 1, 2,
1703 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
1704 dst->nvar, s);
1705 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 2, 2,
1706 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
1707 dst->nvar, -s);
1709 isl_dim_map_range(dim_map, src->start, 0, 0, 0, 1, s);
1710 isl_dim_map_range(dim_map, src->start + 1, 2, 1, 1, src->nparam, -s);
1711 isl_dim_map_range(dim_map, src->start + 2, 2, 1, 1, src->nparam, s);
1712 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 1, 2,
1713 isl_space_dim(dim, isl_dim_set), 1,
1714 src->nvar, -s);
1715 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 2, 2,
1716 isl_space_dim(dim, isl_dim_set), 1,
1717 src->nvar, s);
1719 graph->lp = isl_basic_set_extend_constraints(graph->lp,
1720 coef->n_eq, coef->n_ineq);
1721 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
1722 coef, dim_map);
1723 isl_space_free(dim);
1725 return 0;
1726 error:
1727 isl_space_free(dim);
1728 return -1;
1731 /* Add all validity constraints to graph->lp.
1733 * An edge that is forced to be local needs to have its dependence
1734 * distances equal to zero. We take care of bounding them by 0 from below
1735 * here. add_all_proximity_constraints takes care of bounding them by 0
1736 * from above.
1738 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
1739 * Otherwise, we ignore them.
1741 static int add_all_validity_constraints(struct isl_sched_graph *graph,
1742 int use_coincidence)
1744 int i;
1746 for (i = 0; i < graph->n_edge; ++i) {
1747 struct isl_sched_edge *edge= &graph->edge[i];
1748 int local;
1750 local = edge->local || (edge->coincidence && use_coincidence);
1751 if (!edge->validity && !local)
1752 continue;
1753 if (edge->src != edge->dst)
1754 continue;
1755 if (add_intra_validity_constraints(graph, edge) < 0)
1756 return -1;
1759 for (i = 0; i < graph->n_edge; ++i) {
1760 struct isl_sched_edge *edge = &graph->edge[i];
1761 int local;
1763 local = edge->local || (edge->coincidence && use_coincidence);
1764 if (!edge->validity && !local)
1765 continue;
1766 if (edge->src == edge->dst)
1767 continue;
1768 if (add_inter_validity_constraints(graph, edge) < 0)
1769 return -1;
1772 return 0;
1775 /* Add constraints to graph->lp that bound the dependence distance
1776 * for all dependence relations.
1777 * If a given proximity dependence is identical to a validity
1778 * dependence, then the dependence distance is already bounded
1779 * from below (by zero), so we only need to bound the distance
1780 * from above. (This includes the case of "local" dependences
1781 * which are treated as validity dependence by add_all_validity_constraints.)
1782 * Otherwise, we need to bound the distance both from above and from below.
1784 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
1785 * Otherwise, we ignore them.
1787 static int add_all_proximity_constraints(struct isl_sched_graph *graph,
1788 int use_coincidence)
1790 int i;
1792 for (i = 0; i < graph->n_edge; ++i) {
1793 struct isl_sched_edge *edge= &graph->edge[i];
1794 int local;
1796 local = edge->local || (edge->coincidence && use_coincidence);
1797 if (!edge->proximity && !local)
1798 continue;
1799 if (edge->src == edge->dst &&
1800 add_intra_proximity_constraints(graph, edge, 1, local) < 0)
1801 return -1;
1802 if (edge->src != edge->dst &&
1803 add_inter_proximity_constraints(graph, edge, 1, local) < 0)
1804 return -1;
1805 if (edge->validity || local)
1806 continue;
1807 if (edge->src == edge->dst &&
1808 add_intra_proximity_constraints(graph, edge, -1, 0) < 0)
1809 return -1;
1810 if (edge->src != edge->dst &&
1811 add_inter_proximity_constraints(graph, edge, -1, 0) < 0)
1812 return -1;
1815 return 0;
1818 /* Compute a basis for the rows in the linear part of the schedule
1819 * and extend this basis to a full basis. The remaining rows
1820 * can then be used to force linear independence from the rows
1821 * in the schedule.
1823 * In particular, given the schedule rows S, we compute
1825 * S = H Q
1826 * S U = H
1828 * with H the Hermite normal form of S. That is, all but the
1829 * first rank columns of H are zero and so each row in S is
1830 * a linear combination of the first rank rows of Q.
1831 * The matrix Q is then transposed because we will write the
1832 * coefficients of the next schedule row as a column vector s
1833 * and express this s as a linear combination s = Q c of the
1834 * computed basis.
1835 * Similarly, the matrix U is transposed such that we can
1836 * compute the coefficients c = U s from a schedule row s.
1838 static int node_update_cmap(struct isl_sched_node *node)
1840 isl_mat *H, *U, *Q;
1841 int n_row = isl_mat_rows(node->sched);
1843 H = isl_mat_sub_alloc(node->sched, 0, n_row,
1844 1 + node->nparam, node->nvar);
1846 H = isl_mat_left_hermite(H, 0, &U, &Q);
1847 isl_mat_free(node->cmap);
1848 isl_mat_free(node->cinv);
1849 node->cmap = isl_mat_transpose(Q);
1850 node->cinv = isl_mat_transpose(U);
1851 node->rank = isl_mat_initial_non_zero_cols(H);
1852 isl_mat_free(H);
1854 if (!node->cmap || !node->cinv || node->rank < 0)
1855 return -1;
1856 return 0;
1859 /* How many times should we count the constraints in "edge"?
1861 * If carry is set, then we are counting the number of
1862 * (validity or conditional validity) constraints that will be added
1863 * in setup_carry_lp and we count each edge exactly once.
1865 * Otherwise, we count as follows
1866 * validity -> 1 (>= 0)
1867 * validity+proximity -> 2 (>= 0 and upper bound)
1868 * proximity -> 2 (lower and upper bound)
1869 * local(+any) -> 2 (>= 0 and <= 0)
1871 * If an edge is only marked conditional_validity then it counts
1872 * as zero since it is only checked afterwards.
1874 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
1875 * Otherwise, we ignore them.
1877 static int edge_multiplicity(struct isl_sched_edge *edge, int carry,
1878 int use_coincidence)
1880 if (carry && !edge->validity && !edge->conditional_validity)
1881 return 0;
1882 if (carry)
1883 return 1;
1884 if (edge->proximity || edge->local)
1885 return 2;
1886 if (use_coincidence && edge->coincidence)
1887 return 2;
1888 if (edge->validity)
1889 return 1;
1890 return 0;
1893 /* Count the number of equality and inequality constraints
1894 * that will be added for the given map.
1896 * "use_coincidence" is set if we should take into account coincidence edges.
1898 static int count_map_constraints(struct isl_sched_graph *graph,
1899 struct isl_sched_edge *edge, __isl_take isl_map *map,
1900 int *n_eq, int *n_ineq, int carry, int use_coincidence)
1902 isl_basic_set *coef;
1903 int f = edge_multiplicity(edge, carry, use_coincidence);
1905 if (f == 0) {
1906 isl_map_free(map);
1907 return 0;
1910 if (edge->src == edge->dst)
1911 coef = intra_coefficients(graph, edge->src, map);
1912 else
1913 coef = inter_coefficients(graph, edge, map);
1914 if (!coef)
1915 return -1;
1916 *n_eq += f * coef->n_eq;
1917 *n_ineq += f * coef->n_ineq;
1918 isl_basic_set_free(coef);
1920 return 0;
1923 /* Count the number of equality and inequality constraints
1924 * that will be added to the main lp problem.
1925 * We count as follows
1926 * validity -> 1 (>= 0)
1927 * validity+proximity -> 2 (>= 0 and upper bound)
1928 * proximity -> 2 (lower and upper bound)
1929 * local(+any) -> 2 (>= 0 and <= 0)
1931 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
1932 * Otherwise, we ignore them.
1934 static int count_constraints(struct isl_sched_graph *graph,
1935 int *n_eq, int *n_ineq, int use_coincidence)
1937 int i;
1939 *n_eq = *n_ineq = 0;
1940 for (i = 0; i < graph->n_edge; ++i) {
1941 struct isl_sched_edge *edge= &graph->edge[i];
1942 isl_map *map = isl_map_copy(edge->map);
1944 if (count_map_constraints(graph, edge, map, n_eq, n_ineq,
1945 0, use_coincidence) < 0)
1946 return -1;
1949 return 0;
1952 /* Count the number of constraints that will be added by
1953 * add_bound_coefficient_constraints and increment *n_eq and *n_ineq
1954 * accordingly.
1956 * In practice, add_bound_coefficient_constraints only adds inequalities.
1958 static int count_bound_coefficient_constraints(isl_ctx *ctx,
1959 struct isl_sched_graph *graph, int *n_eq, int *n_ineq)
1961 int i;
1963 if (ctx->opt->schedule_max_coefficient == -1)
1964 return 0;
1966 for (i = 0; i < graph->n; ++i)
1967 *n_ineq += 2 * graph->node[i].nparam + 2 * graph->node[i].nvar;
1969 return 0;
1972 /* Add constraints that bound the values of the variable and parameter
1973 * coefficients of the schedule.
1975 * The maximal value of the coefficients is defined by the option
1976 * 'schedule_max_coefficient'.
1978 static int add_bound_coefficient_constraints(isl_ctx *ctx,
1979 struct isl_sched_graph *graph)
1981 int i, j, k;
1982 int max_coefficient;
1983 int total;
1985 max_coefficient = ctx->opt->schedule_max_coefficient;
1987 if (max_coefficient == -1)
1988 return 0;
1990 total = isl_basic_set_total_dim(graph->lp);
1992 for (i = 0; i < graph->n; ++i) {
1993 struct isl_sched_node *node = &graph->node[i];
1994 for (j = 0; j < 2 * node->nparam + 2 * node->nvar; ++j) {
1995 int dim;
1996 k = isl_basic_set_alloc_inequality(graph->lp);
1997 if (k < 0)
1998 return -1;
1999 dim = 1 + node->start + 1 + j;
2000 isl_seq_clr(graph->lp->ineq[k], 1 + total);
2001 isl_int_set_si(graph->lp->ineq[k][dim], -1);
2002 isl_int_set_si(graph->lp->ineq[k][0], max_coefficient);
2006 return 0;
2009 /* Construct an ILP problem for finding schedule coefficients
2010 * that result in non-negative, but small dependence distances
2011 * over all dependences.
2012 * In particular, the dependence distances over proximity edges
2013 * are bounded by m_0 + m_n n and we compute schedule coefficients
2014 * with small values (preferably zero) of m_n and m_0.
2016 * All variables of the ILP are non-negative. The actual coefficients
2017 * may be negative, so each coefficient is represented as the difference
2018 * of two non-negative variables. The negative part always appears
2019 * immediately before the positive part.
2020 * Other than that, the variables have the following order
2022 * - sum of positive and negative parts of m_n coefficients
2023 * - m_0
2024 * - sum of positive and negative parts of all c_n coefficients
2025 * (unconstrained when computing non-parametric schedules)
2026 * - sum of positive and negative parts of all c_x coefficients
2027 * - positive and negative parts of m_n coefficients
2028 * - for each node
2029 * - c_i_0
2030 * - positive and negative parts of c_i_n (if parametric)
2031 * - positive and negative parts of c_i_x
2033 * The c_i_x are not represented directly, but through the columns of
2034 * node->cmap. That is, the computed values are for variable t_i_x
2035 * such that c_i_x = Q t_i_x with Q equal to node->cmap.
2037 * The constraints are those from the edges plus two or three equalities
2038 * to express the sums.
2040 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
2041 * Otherwise, we ignore them.
2043 static int setup_lp(isl_ctx *ctx, struct isl_sched_graph *graph,
2044 int use_coincidence)
2046 int i, j;
2047 int k;
2048 unsigned nparam;
2049 unsigned total;
2050 isl_space *dim;
2051 int parametric;
2052 int param_pos;
2053 int n_eq, n_ineq;
2054 int max_constant_term;
2056 max_constant_term = ctx->opt->schedule_max_constant_term;
2058 parametric = ctx->opt->schedule_parametric;
2059 nparam = isl_space_dim(graph->node[0].space, isl_dim_param);
2060 param_pos = 4;
2061 total = param_pos + 2 * nparam;
2062 for (i = 0; i < graph->n; ++i) {
2063 struct isl_sched_node *node = &graph->node[graph->sorted[i]];
2064 if (node_update_cmap(node) < 0)
2065 return -1;
2066 node->start = total;
2067 total += 1 + 2 * (node->nparam + node->nvar);
2070 if (count_constraints(graph, &n_eq, &n_ineq, use_coincidence) < 0)
2071 return -1;
2072 if (count_bound_coefficient_constraints(ctx, graph, &n_eq, &n_ineq) < 0)
2073 return -1;
2075 dim = isl_space_set_alloc(ctx, 0, total);
2076 isl_basic_set_free(graph->lp);
2077 n_eq += 2 + parametric;
2078 if (max_constant_term != -1)
2079 n_ineq += graph->n;
2081 graph->lp = isl_basic_set_alloc_space(dim, 0, n_eq, n_ineq);
2083 k = isl_basic_set_alloc_equality(graph->lp);
2084 if (k < 0)
2085 return -1;
2086 isl_seq_clr(graph->lp->eq[k], 1 + total);
2087 isl_int_set_si(graph->lp->eq[k][1], -1);
2088 for (i = 0; i < 2 * nparam; ++i)
2089 isl_int_set_si(graph->lp->eq[k][1 + param_pos + i], 1);
2091 if (parametric) {
2092 k = isl_basic_set_alloc_equality(graph->lp);
2093 if (k < 0)
2094 return -1;
2095 isl_seq_clr(graph->lp->eq[k], 1 + total);
2096 isl_int_set_si(graph->lp->eq[k][3], -1);
2097 for (i = 0; i < graph->n; ++i) {
2098 int pos = 1 + graph->node[i].start + 1;
2100 for (j = 0; j < 2 * graph->node[i].nparam; ++j)
2101 isl_int_set_si(graph->lp->eq[k][pos + j], 1);
2105 k = isl_basic_set_alloc_equality(graph->lp);
2106 if (k < 0)
2107 return -1;
2108 isl_seq_clr(graph->lp->eq[k], 1 + total);
2109 isl_int_set_si(graph->lp->eq[k][4], -1);
2110 for (i = 0; i < graph->n; ++i) {
2111 struct isl_sched_node *node = &graph->node[i];
2112 int pos = 1 + node->start + 1 + 2 * node->nparam;
2114 for (j = 0; j < 2 * node->nvar; ++j)
2115 isl_int_set_si(graph->lp->eq[k][pos + j], 1);
2118 if (max_constant_term != -1)
2119 for (i = 0; i < graph->n; ++i) {
2120 struct isl_sched_node *node = &graph->node[i];
2121 k = isl_basic_set_alloc_inequality(graph->lp);
2122 if (k < 0)
2123 return -1;
2124 isl_seq_clr(graph->lp->ineq[k], 1 + total);
2125 isl_int_set_si(graph->lp->ineq[k][1 + node->start], -1);
2126 isl_int_set_si(graph->lp->ineq[k][0], max_constant_term);
2129 if (add_bound_coefficient_constraints(ctx, graph) < 0)
2130 return -1;
2131 if (add_all_validity_constraints(graph, use_coincidence) < 0)
2132 return -1;
2133 if (add_all_proximity_constraints(graph, use_coincidence) < 0)
2134 return -1;
2136 return 0;
2139 /* Analyze the conflicting constraint found by
2140 * isl_tab_basic_set_non_trivial_lexmin. If it corresponds to the validity
2141 * constraint of one of the edges between distinct nodes, living, moreover
2142 * in distinct SCCs, then record the source and sink SCC as this may
2143 * be a good place to cut between SCCs.
2145 static int check_conflict(int con, void *user)
2147 int i;
2148 struct isl_sched_graph *graph = user;
2150 if (graph->src_scc >= 0)
2151 return 0;
2153 con -= graph->lp->n_eq;
2155 if (con >= graph->lp->n_ineq)
2156 return 0;
2158 for (i = 0; i < graph->n_edge; ++i) {
2159 if (!graph->edge[i].validity)
2160 continue;
2161 if (graph->edge[i].src == graph->edge[i].dst)
2162 continue;
2163 if (graph->edge[i].src->scc == graph->edge[i].dst->scc)
2164 continue;
2165 if (graph->edge[i].start > con)
2166 continue;
2167 if (graph->edge[i].end <= con)
2168 continue;
2169 graph->src_scc = graph->edge[i].src->scc;
2170 graph->dst_scc = graph->edge[i].dst->scc;
2173 return 0;
2176 /* Check whether the next schedule row of the given node needs to be
2177 * non-trivial. Lower-dimensional domains may have some trivial rows,
2178 * but as soon as the number of remaining required non-trivial rows
2179 * is as large as the number or remaining rows to be computed,
2180 * all remaining rows need to be non-trivial.
2182 static int needs_row(struct isl_sched_graph *graph, struct isl_sched_node *node)
2184 return node->nvar - node->rank >= graph->maxvar - graph->n_row;
2187 /* Solve the ILP problem constructed in setup_lp.
2188 * For each node such that all the remaining rows of its schedule
2189 * need to be non-trivial, we construct a non-triviality region.
2190 * This region imposes that the next row is independent of previous rows.
2191 * In particular the coefficients c_i_x are represented by t_i_x
2192 * variables with c_i_x = Q t_i_x and Q a unimodular matrix such that
2193 * its first columns span the rows of the previously computed part
2194 * of the schedule. The non-triviality region enforces that at least
2195 * one of the remaining components of t_i_x is non-zero, i.e.,
2196 * that the new schedule row depends on at least one of the remaining
2197 * columns of Q.
2199 static __isl_give isl_vec *solve_lp(struct isl_sched_graph *graph)
2201 int i;
2202 isl_vec *sol;
2203 isl_basic_set *lp;
2205 for (i = 0; i < graph->n; ++i) {
2206 struct isl_sched_node *node = &graph->node[i];
2207 int skip = node->rank;
2208 graph->region[i].pos = node->start + 1 + 2*(node->nparam+skip);
2209 if (needs_row(graph, node))
2210 graph->region[i].len = 2 * (node->nvar - skip);
2211 else
2212 graph->region[i].len = 0;
2214 lp = isl_basic_set_copy(graph->lp);
2215 sol = isl_tab_basic_set_non_trivial_lexmin(lp, 2, graph->n,
2216 graph->region, &check_conflict, graph);
2217 return sol;
2220 /* Update the schedules of all nodes based on the given solution
2221 * of the LP problem.
2222 * The new row is added to the current band.
2223 * All possibly negative coefficients are encoded as a difference
2224 * of two non-negative variables, so we need to perform the subtraction
2225 * here. Moreover, if use_cmap is set, then the solution does
2226 * not refer to the actual coefficients c_i_x, but instead to variables
2227 * t_i_x such that c_i_x = Q t_i_x and Q is equal to node->cmap.
2228 * In this case, we then also need to perform this multiplication
2229 * to obtain the values of c_i_x.
2231 * If coincident is set, then the caller guarantees that the new
2232 * row satisfies the coincidence constraints.
2234 static int update_schedule(struct isl_sched_graph *graph,
2235 __isl_take isl_vec *sol, int use_cmap, int coincident)
2237 int i, j;
2238 isl_vec *csol = NULL;
2240 if (!sol)
2241 goto error;
2242 if (sol->size == 0)
2243 isl_die(sol->ctx, isl_error_internal,
2244 "no solution found", goto error);
2245 if (graph->n_total_row >= graph->max_row)
2246 isl_die(sol->ctx, isl_error_internal,
2247 "too many schedule rows", goto error);
2249 for (i = 0; i < graph->n; ++i) {
2250 struct isl_sched_node *node = &graph->node[i];
2251 int pos = node->start;
2252 int row = isl_mat_rows(node->sched);
2254 isl_vec_free(csol);
2255 csol = isl_vec_alloc(sol->ctx, node->nvar);
2256 if (!csol)
2257 goto error;
2259 isl_map_free(node->sched_map);
2260 node->sched_map = NULL;
2261 node->sched = isl_mat_add_rows(node->sched, 1);
2262 if (!node->sched)
2263 goto error;
2264 node->sched = isl_mat_set_element(node->sched, row, 0,
2265 sol->el[1 + pos]);
2266 for (j = 0; j < node->nparam + node->nvar; ++j)
2267 isl_int_sub(sol->el[1 + pos + 1 + 2 * j + 1],
2268 sol->el[1 + pos + 1 + 2 * j + 1],
2269 sol->el[1 + pos + 1 + 2 * j]);
2270 for (j = 0; j < node->nparam; ++j)
2271 node->sched = isl_mat_set_element(node->sched,
2272 row, 1 + j, sol->el[1+pos+1+2*j+1]);
2273 for (j = 0; j < node->nvar; ++j)
2274 isl_int_set(csol->el[j],
2275 sol->el[1+pos+1+2*(node->nparam+j)+1]);
2276 if (use_cmap)
2277 csol = isl_mat_vec_product(isl_mat_copy(node->cmap),
2278 csol);
2279 if (!csol)
2280 goto error;
2281 for (j = 0; j < node->nvar; ++j)
2282 node->sched = isl_mat_set_element(node->sched,
2283 row, 1 + node->nparam + j, csol->el[j]);
2284 node->band[graph->n_total_row] = graph->n_band;
2285 node->coincident[graph->n_total_row] = coincident;
2287 isl_vec_free(sol);
2288 isl_vec_free(csol);
2290 graph->n_row++;
2291 graph->n_total_row++;
2293 return 0;
2294 error:
2295 isl_vec_free(sol);
2296 isl_vec_free(csol);
2297 return -1;
2300 /* Convert row "row" of node->sched into an isl_aff living in "ls"
2301 * and return this isl_aff.
2303 static __isl_give isl_aff *extract_schedule_row(__isl_take isl_local_space *ls,
2304 struct isl_sched_node *node, int row)
2306 int j;
2307 isl_int v;
2308 isl_aff *aff;
2310 isl_int_init(v);
2312 aff = isl_aff_zero_on_domain(ls);
2313 isl_mat_get_element(node->sched, row, 0, &v);
2314 aff = isl_aff_set_constant(aff, v);
2315 for (j = 0; j < node->nparam; ++j) {
2316 isl_mat_get_element(node->sched, row, 1 + j, &v);
2317 aff = isl_aff_set_coefficient(aff, isl_dim_param, j, v);
2319 for (j = 0; j < node->nvar; ++j) {
2320 isl_mat_get_element(node->sched, row, 1 + node->nparam + j, &v);
2321 aff = isl_aff_set_coefficient(aff, isl_dim_in, j, v);
2324 isl_int_clear(v);
2326 return aff;
2329 /* Convert node->sched into a multi_aff and return this multi_aff.
2331 * The result is defined over the uncompressed node domain.
2333 static __isl_give isl_multi_aff *node_extract_schedule_multi_aff(
2334 struct isl_sched_node *node)
2336 int i;
2337 isl_space *space;
2338 isl_local_space *ls;
2339 isl_aff *aff;
2340 isl_multi_aff *ma;
2341 int nrow, ncol;
2343 nrow = isl_mat_rows(node->sched);
2344 ncol = isl_mat_cols(node->sched) - 1;
2345 if (node->compressed)
2346 space = isl_multi_aff_get_domain_space(node->decompress);
2347 else
2348 space = isl_space_copy(node->space);
2349 ls = isl_local_space_from_space(isl_space_copy(space));
2350 space = isl_space_from_domain(space);
2351 space = isl_space_add_dims(space, isl_dim_out, nrow);
2352 ma = isl_multi_aff_zero(space);
2354 for (i = 0; i < nrow; ++i) {
2355 aff = extract_schedule_row(isl_local_space_copy(ls), node, i);
2356 ma = isl_multi_aff_set_aff(ma, i, aff);
2359 isl_local_space_free(ls);
2361 if (node->compressed)
2362 ma = isl_multi_aff_pullback_multi_aff(ma,
2363 isl_multi_aff_copy(node->compress));
2365 return ma;
2368 /* Convert node->sched into a map and return this map.
2370 * The result is cached in node->sched_map, which needs to be released
2371 * whenever node->sched is updated.
2372 * It is defined over the uncompressed node domain.
2374 static __isl_give isl_map *node_extract_schedule(struct isl_sched_node *node)
2376 if (!node->sched_map) {
2377 isl_multi_aff *ma;
2379 ma = node_extract_schedule_multi_aff(node);
2380 node->sched_map = isl_map_from_multi_aff(ma);
2383 return isl_map_copy(node->sched_map);
2386 /* Construct a map that can be used to update a dependence relation
2387 * based on the current schedule.
2388 * That is, construct a map expressing that source and sink
2389 * are executed within the same iteration of the current schedule.
2390 * This map can then be intersected with the dependence relation.
2391 * This is not the most efficient way, but this shouldn't be a critical
2392 * operation.
2394 static __isl_give isl_map *specializer(struct isl_sched_node *src,
2395 struct isl_sched_node *dst)
2397 isl_map *src_sched, *dst_sched;
2399 src_sched = node_extract_schedule(src);
2400 dst_sched = node_extract_schedule(dst);
2401 return isl_map_apply_range(src_sched, isl_map_reverse(dst_sched));
2404 /* Intersect the domains of the nested relations in domain and range
2405 * of "umap" with "map".
2407 static __isl_give isl_union_map *intersect_domains(
2408 __isl_take isl_union_map *umap, __isl_keep isl_map *map)
2410 isl_union_set *uset;
2412 umap = isl_union_map_zip(umap);
2413 uset = isl_union_set_from_set(isl_map_wrap(isl_map_copy(map)));
2414 umap = isl_union_map_intersect_domain(umap, uset);
2415 umap = isl_union_map_zip(umap);
2416 return umap;
2419 /* Update the dependence relation of the given edge based
2420 * on the current schedule.
2421 * If the dependence is carried completely by the current schedule, then
2422 * it is removed from the edge_tables. It is kept in the list of edges
2423 * as otherwise all edge_tables would have to be recomputed.
2425 static int update_edge(struct isl_sched_graph *graph,
2426 struct isl_sched_edge *edge)
2428 int empty;
2429 isl_map *id;
2431 id = specializer(edge->src, edge->dst);
2432 edge->map = isl_map_intersect(edge->map, isl_map_copy(id));
2433 if (!edge->map)
2434 goto error;
2436 if (edge->tagged_condition) {
2437 edge->tagged_condition =
2438 intersect_domains(edge->tagged_condition, id);
2439 if (!edge->tagged_condition)
2440 goto error;
2442 if (edge->tagged_validity) {
2443 edge->tagged_validity =
2444 intersect_domains(edge->tagged_validity, id);
2445 if (!edge->tagged_validity)
2446 goto error;
2449 empty = isl_map_plain_is_empty(edge->map);
2450 if (empty < 0)
2451 goto error;
2452 if (empty)
2453 graph_remove_edge(graph, edge);
2455 isl_map_free(id);
2456 return 0;
2457 error:
2458 isl_map_free(id);
2459 return -1;
2462 /* Does the domain of "umap" intersect "uset"?
2464 static int domain_intersects(__isl_keep isl_union_map *umap,
2465 __isl_keep isl_union_set *uset)
2467 int empty;
2469 umap = isl_union_map_copy(umap);
2470 umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(uset));
2471 empty = isl_union_map_is_empty(umap);
2472 isl_union_map_free(umap);
2474 return empty < 0 ? -1 : !empty;
2477 /* Does the range of "umap" intersect "uset"?
2479 static int range_intersects(__isl_keep isl_union_map *umap,
2480 __isl_keep isl_union_set *uset)
2482 int empty;
2484 umap = isl_union_map_copy(umap);
2485 umap = isl_union_map_intersect_range(umap, isl_union_set_copy(uset));
2486 empty = isl_union_map_is_empty(umap);
2487 isl_union_map_free(umap);
2489 return empty < 0 ? -1 : !empty;
2492 /* Are the condition dependences of "edge" local with respect to
2493 * the current schedule?
2495 * That is, are domain and range of the condition dependences mapped
2496 * to the same point?
2498 * In other words, is the condition false?
2500 static int is_condition_false(struct isl_sched_edge *edge)
2502 isl_union_map *umap;
2503 isl_map *map, *sched, *test;
2504 int empty, local;
2506 empty = isl_union_map_is_empty(edge->tagged_condition);
2507 if (empty < 0 || empty)
2508 return empty;
2510 umap = isl_union_map_copy(edge->tagged_condition);
2511 umap = isl_union_map_zip(umap);
2512 umap = isl_union_set_unwrap(isl_union_map_domain(umap));
2513 map = isl_map_from_union_map(umap);
2515 sched = node_extract_schedule(edge->src);
2516 map = isl_map_apply_domain(map, sched);
2517 sched = node_extract_schedule(edge->dst);
2518 map = isl_map_apply_range(map, sched);
2520 test = isl_map_identity(isl_map_get_space(map));
2521 local = isl_map_is_subset(map, test);
2522 isl_map_free(map);
2523 isl_map_free(test);
2525 return local;
2528 /* Update the dependence relations of all edges based on the current schedule.
2530 static int update_edges(isl_ctx *ctx, struct isl_sched_graph *graph)
2532 int i;
2534 for (i = graph->n_edge - 1; i >= 0; --i) {
2535 if (update_edge(graph, &graph->edge[i]) < 0)
2536 return -1;
2539 return 0;
2542 static void next_band(struct isl_sched_graph *graph)
2544 graph->band_start = graph->n_total_row;
2545 graph->n_band++;
2548 /* Topologically sort statements mapped to the same schedule iteration
2549 * and add a row to the schedule corresponding to this order.
2551 static int sort_statements(isl_ctx *ctx, struct isl_sched_graph *graph)
2553 int i, j;
2555 if (graph->n <= 1)
2556 return 0;
2558 if (update_edges(ctx, graph) < 0)
2559 return -1;
2561 if (graph->n_edge == 0)
2562 return 0;
2564 if (detect_sccs(ctx, graph) < 0)
2565 return -1;
2567 if (graph->n_total_row >= graph->max_row)
2568 isl_die(ctx, isl_error_internal,
2569 "too many schedule rows", return -1);
2571 for (i = 0; i < graph->n; ++i) {
2572 struct isl_sched_node *node = &graph->node[i];
2573 int row = isl_mat_rows(node->sched);
2574 int cols = isl_mat_cols(node->sched);
2576 isl_map_free(node->sched_map);
2577 node->sched_map = NULL;
2578 node->sched = isl_mat_add_rows(node->sched, 1);
2579 if (!node->sched)
2580 return -1;
2581 node->sched = isl_mat_set_element_si(node->sched, row, 0,
2582 node->scc);
2583 for (j = 1; j < cols; ++j)
2584 node->sched = isl_mat_set_element_si(node->sched,
2585 row, j, 0);
2586 node->band[graph->n_total_row] = graph->n_band;
2589 graph->n_total_row++;
2590 next_band(graph);
2592 return 0;
2595 /* Construct an isl_schedule based on the computed schedule stored
2596 * in graph and with parameters specified by dim.
2598 static __isl_give isl_schedule *extract_schedule(struct isl_sched_graph *graph,
2599 __isl_take isl_space *dim)
2601 int i;
2602 isl_ctx *ctx;
2603 isl_schedule *sched = NULL;
2605 if (!dim)
2606 return NULL;
2608 ctx = isl_space_get_ctx(dim);
2609 sched = isl_calloc(ctx, struct isl_schedule,
2610 sizeof(struct isl_schedule) +
2611 (graph->n - 1) * sizeof(struct isl_schedule_node));
2612 if (!sched)
2613 goto error;
2615 sched->ref = 1;
2616 sched->n = graph->n;
2617 sched->n_band = graph->n_band;
2618 sched->n_total_row = graph->n_total_row;
2620 for (i = 0; i < sched->n; ++i) {
2621 int r, b;
2622 int *band_end, *band_id, *coincident;
2624 sched->node[i].sched =
2625 node_extract_schedule_multi_aff(&graph->node[i]);
2626 if (!sched->node[i].sched)
2627 goto error;
2629 sched->node[i].n_band = graph->n_band;
2630 if (graph->n_band == 0)
2631 continue;
2633 band_end = isl_alloc_array(ctx, int, graph->n_band);
2634 band_id = isl_alloc_array(ctx, int, graph->n_band);
2635 coincident = isl_alloc_array(ctx, int, graph->n_total_row);
2636 sched->node[i].band_end = band_end;
2637 sched->node[i].band_id = band_id;
2638 sched->node[i].coincident = coincident;
2639 if (!band_end || !band_id || !coincident)
2640 goto error;
2642 for (r = 0; r < graph->n_total_row; ++r)
2643 coincident[r] = graph->node[i].coincident[r];
2644 for (r = b = 0; r < graph->n_total_row; ++r) {
2645 if (graph->node[i].band[r] == b)
2646 continue;
2647 band_end[b++] = r;
2648 if (graph->node[i].band[r] == -1)
2649 break;
2651 if (r == graph->n_total_row)
2652 band_end[b++] = r;
2653 sched->node[i].n_band = b;
2654 for (--b; b >= 0; --b)
2655 band_id[b] = graph->node[i].band_id[b];
2658 sched->dim = dim;
2660 return sched;
2661 error:
2662 isl_space_free(dim);
2663 isl_schedule_free(sched);
2664 return NULL;
2667 /* Copy nodes that satisfy node_pred from the src dependence graph
2668 * to the dst dependence graph.
2670 static int copy_nodes(struct isl_sched_graph *dst, struct isl_sched_graph *src,
2671 int (*node_pred)(struct isl_sched_node *node, int data), int data)
2673 int i;
2675 dst->n = 0;
2676 for (i = 0; i < src->n; ++i) {
2677 int j;
2679 if (!node_pred(&src->node[i], data))
2680 continue;
2682 j = dst->n;
2683 dst->node[j].space = isl_space_copy(src->node[i].space);
2684 dst->node[j].compressed = src->node[i].compressed;
2685 dst->node[j].hull = isl_set_copy(src->node[i].hull);
2686 dst->node[j].compress =
2687 isl_multi_aff_copy(src->node[i].compress);
2688 dst->node[j].decompress =
2689 isl_multi_aff_copy(src->node[i].decompress);
2690 dst->node[j].nvar = src->node[i].nvar;
2691 dst->node[j].nparam = src->node[i].nparam;
2692 dst->node[j].sched = isl_mat_copy(src->node[i].sched);
2693 dst->node[j].sched_map = isl_map_copy(src->node[i].sched_map);
2694 dst->node[j].band = src->node[i].band;
2695 dst->node[j].band_id = src->node[i].band_id;
2696 dst->node[j].coincident = src->node[i].coincident;
2697 dst->n++;
2699 if (!dst->node[j].space || !dst->node[j].sched)
2700 return -1;
2701 if (dst->node[j].compressed &&
2702 (!dst->node[j].hull || !dst->node[j].compress ||
2703 !dst->node[j].decompress))
2704 return -1;
2707 return 0;
2710 /* Copy non-empty edges that satisfy edge_pred from the src dependence graph
2711 * to the dst dependence graph.
2712 * If the source or destination node of the edge is not in the destination
2713 * graph, then it must be a backward proximity edge and it should simply
2714 * be ignored.
2716 static int copy_edges(isl_ctx *ctx, struct isl_sched_graph *dst,
2717 struct isl_sched_graph *src,
2718 int (*edge_pred)(struct isl_sched_edge *edge, int data), int data)
2720 int i;
2721 enum isl_edge_type t;
2723 dst->n_edge = 0;
2724 for (i = 0; i < src->n_edge; ++i) {
2725 struct isl_sched_edge *edge = &src->edge[i];
2726 isl_map *map;
2727 isl_union_map *tagged_condition;
2728 isl_union_map *tagged_validity;
2729 struct isl_sched_node *dst_src, *dst_dst;
2731 if (!edge_pred(edge, data))
2732 continue;
2734 if (isl_map_plain_is_empty(edge->map))
2735 continue;
2737 dst_src = graph_find_node(ctx, dst, edge->src->space);
2738 dst_dst = graph_find_node(ctx, dst, edge->dst->space);
2739 if (!dst_src || !dst_dst) {
2740 if (edge->validity || edge->conditional_validity)
2741 isl_die(ctx, isl_error_internal,
2742 "backward (conditional) validity edge",
2743 return -1);
2744 continue;
2747 map = isl_map_copy(edge->map);
2748 tagged_condition = isl_union_map_copy(edge->tagged_condition);
2749 tagged_validity = isl_union_map_copy(edge->tagged_validity);
2751 dst->edge[dst->n_edge].src = dst_src;
2752 dst->edge[dst->n_edge].dst = dst_dst;
2753 dst->edge[dst->n_edge].map = map;
2754 dst->edge[dst->n_edge].tagged_condition = tagged_condition;
2755 dst->edge[dst->n_edge].tagged_validity = tagged_validity;
2756 dst->edge[dst->n_edge].validity = edge->validity;
2757 dst->edge[dst->n_edge].proximity = edge->proximity;
2758 dst->edge[dst->n_edge].coincidence = edge->coincidence;
2759 dst->edge[dst->n_edge].condition = edge->condition;
2760 dst->edge[dst->n_edge].conditional_validity =
2761 edge->conditional_validity;
2762 dst->n_edge++;
2764 if (edge->tagged_condition && !tagged_condition)
2765 return -1;
2766 if (edge->tagged_validity && !tagged_validity)
2767 return -1;
2769 for (t = isl_edge_first; t <= isl_edge_last; ++t) {
2770 if (edge !=
2771 graph_find_edge(src, t, edge->src, edge->dst))
2772 continue;
2773 if (graph_edge_table_add(ctx, dst, t,
2774 &dst->edge[dst->n_edge - 1]) < 0)
2775 return -1;
2779 return 0;
2782 /* Given a "src" dependence graph that contains the nodes from "dst"
2783 * that satisfy node_pred, copy the schedule computed in "src"
2784 * for those nodes back to "dst".
2786 static int copy_schedule(struct isl_sched_graph *dst,
2787 struct isl_sched_graph *src,
2788 int (*node_pred)(struct isl_sched_node *node, int data), int data)
2790 int i;
2792 src->n = 0;
2793 for (i = 0; i < dst->n; ++i) {
2794 if (!node_pred(&dst->node[i], data))
2795 continue;
2796 isl_mat_free(dst->node[i].sched);
2797 isl_map_free(dst->node[i].sched_map);
2798 dst->node[i].sched = isl_mat_copy(src->node[src->n].sched);
2799 dst->node[i].sched_map =
2800 isl_map_copy(src->node[src->n].sched_map);
2801 src->n++;
2804 dst->max_row = src->max_row;
2805 dst->n_total_row = src->n_total_row;
2806 dst->n_band = src->n_band;
2808 return 0;
2811 /* Compute the maximal number of variables over all nodes.
2812 * This is the maximal number of linearly independent schedule
2813 * rows that we need to compute.
2814 * Just in case we end up in a part of the dependence graph
2815 * with only lower-dimensional domains, we make sure we will
2816 * compute the required amount of extra linearly independent rows.
2818 static int compute_maxvar(struct isl_sched_graph *graph)
2820 int i;
2822 graph->maxvar = 0;
2823 for (i = 0; i < graph->n; ++i) {
2824 struct isl_sched_node *node = &graph->node[i];
2825 int nvar;
2827 if (node_update_cmap(node) < 0)
2828 return -1;
2829 nvar = node->nvar + graph->n_row - node->rank;
2830 if (nvar > graph->maxvar)
2831 graph->maxvar = nvar;
2834 return 0;
2837 static int compute_schedule(isl_ctx *ctx, struct isl_sched_graph *graph);
2838 static int compute_schedule_wcc(isl_ctx *ctx, struct isl_sched_graph *graph);
2840 /* Compute a schedule for a subgraph of "graph". In particular, for
2841 * the graph composed of nodes that satisfy node_pred and edges that
2842 * that satisfy edge_pred. The caller should precompute the number
2843 * of nodes and edges that satisfy these predicates and pass them along
2844 * as "n" and "n_edge".
2845 * If the subgraph is known to consist of a single component, then wcc should
2846 * be set and then we call compute_schedule_wcc on the constructed subgraph.
2847 * Otherwise, we call compute_schedule, which will check whether the subgraph
2848 * is connected.
2850 static int compute_sub_schedule(isl_ctx *ctx,
2851 struct isl_sched_graph *graph, int n, int n_edge,
2852 int (*node_pred)(struct isl_sched_node *node, int data),
2853 int (*edge_pred)(struct isl_sched_edge *edge, int data),
2854 int data, int wcc)
2856 struct isl_sched_graph split = { 0 };
2857 int t;
2859 if (graph_alloc(ctx, &split, n, n_edge) < 0)
2860 goto error;
2861 if (copy_nodes(&split, graph, node_pred, data) < 0)
2862 goto error;
2863 if (graph_init_table(ctx, &split) < 0)
2864 goto error;
2865 for (t = 0; t <= isl_edge_last; ++t)
2866 split.max_edge[t] = graph->max_edge[t];
2867 if (graph_init_edge_tables(ctx, &split) < 0)
2868 goto error;
2869 if (copy_edges(ctx, &split, graph, edge_pred, data) < 0)
2870 goto error;
2871 split.n_row = graph->n_row;
2872 split.max_row = graph->max_row;
2873 split.n_total_row = graph->n_total_row;
2874 split.n_band = graph->n_band;
2875 split.band_start = graph->band_start;
2877 if (wcc && compute_schedule_wcc(ctx, &split) < 0)
2878 goto error;
2879 if (!wcc && compute_schedule(ctx, &split) < 0)
2880 goto error;
2882 copy_schedule(graph, &split, node_pred, data);
2884 graph_free(ctx, &split);
2885 return 0;
2886 error:
2887 graph_free(ctx, &split);
2888 return -1;
2891 static int node_scc_exactly(struct isl_sched_node *node, int scc)
2893 return node->scc == scc;
2896 static int node_scc_at_most(struct isl_sched_node *node, int scc)
2898 return node->scc <= scc;
2901 static int node_scc_at_least(struct isl_sched_node *node, int scc)
2903 return node->scc >= scc;
2906 static int edge_scc_exactly(struct isl_sched_edge *edge, int scc)
2908 return edge->src->scc == scc && edge->dst->scc == scc;
2911 static int edge_dst_scc_at_most(struct isl_sched_edge *edge, int scc)
2913 return edge->dst->scc <= scc;
2916 static int edge_src_scc_at_least(struct isl_sched_edge *edge, int scc)
2918 return edge->src->scc >= scc;
2921 /* Pad the schedules of all nodes with zero rows such that in the end
2922 * they all have graph->n_total_row rows.
2923 * The extra rows don't belong to any band, so they get assigned band number -1.
2925 static int pad_schedule(struct isl_sched_graph *graph)
2927 int i, j;
2929 for (i = 0; i < graph->n; ++i) {
2930 struct isl_sched_node *node = &graph->node[i];
2931 int row = isl_mat_rows(node->sched);
2932 if (graph->n_total_row > row) {
2933 isl_map_free(node->sched_map);
2934 node->sched_map = NULL;
2936 node->sched = isl_mat_add_zero_rows(node->sched,
2937 graph->n_total_row - row);
2938 if (!node->sched)
2939 return -1;
2940 for (j = row; j < graph->n_total_row; ++j)
2941 node->band[j] = -1;
2944 return 0;
2947 /* Reset the current band by dropping all its schedule rows.
2949 static int reset_band(struct isl_sched_graph *graph)
2951 int i;
2952 int drop;
2954 drop = graph->n_total_row - graph->band_start;
2955 graph->n_total_row -= drop;
2956 graph->n_row -= drop;
2958 for (i = 0; i < graph->n; ++i) {
2959 struct isl_sched_node *node = &graph->node[i];
2961 isl_map_free(node->sched_map);
2962 node->sched_map = NULL;
2964 node->sched = isl_mat_drop_rows(node->sched,
2965 graph->band_start, drop);
2967 if (!node->sched)
2968 return -1;
2971 return 0;
2974 /* Split the current graph into two parts and compute a schedule for each
2975 * part individually. In particular, one part consists of all SCCs up
2976 * to and including graph->src_scc, while the other part contains the other
2977 * SCCS.
2979 * The split is enforced in the schedule by constant rows with two different
2980 * values (0 and 1). These constant rows replace the previously computed rows
2981 * in the current band.
2982 * It would be possible to reuse them as the first rows in the next
2983 * band, but recomputing them may result in better rows as we are looking
2984 * at a smaller part of the dependence graph.
2986 * Since we do not enforce coincidence, we conservatively mark the
2987 * splitting row as not coincident.
2989 * The band_id of the second group is set to n, where n is the number
2990 * of nodes in the first group. This ensures that the band_ids over
2991 * the two groups remain disjoint, even if either or both of the two
2992 * groups contain independent components.
2994 static int compute_split_schedule(isl_ctx *ctx, struct isl_sched_graph *graph)
2996 int i, j, n, e1, e2;
2997 int n_total_row, orig_total_row;
2998 int n_band, orig_band;
3000 if (graph->n_total_row >= graph->max_row)
3001 isl_die(ctx, isl_error_internal,
3002 "too many schedule rows", return -1);
3004 if (reset_band(graph) < 0)
3005 return -1;
3007 n = 0;
3008 for (i = 0; i < graph->n; ++i) {
3009 struct isl_sched_node *node = &graph->node[i];
3010 int row = isl_mat_rows(node->sched);
3011 int cols = isl_mat_cols(node->sched);
3012 int before = node->scc <= graph->src_scc;
3014 if (before)
3015 n++;
3017 isl_map_free(node->sched_map);
3018 node->sched_map = NULL;
3019 node->sched = isl_mat_add_rows(node->sched, 1);
3020 if (!node->sched)
3021 return -1;
3022 node->sched = isl_mat_set_element_si(node->sched, row, 0,
3023 !before);
3024 for (j = 1; j < cols; ++j)
3025 node->sched = isl_mat_set_element_si(node->sched,
3026 row, j, 0);
3027 node->band[graph->n_total_row] = graph->n_band;
3028 node->coincident[graph->n_total_row] = 0;
3031 e1 = e2 = 0;
3032 for (i = 0; i < graph->n_edge; ++i) {
3033 if (graph->edge[i].dst->scc <= graph->src_scc)
3034 e1++;
3035 if (graph->edge[i].src->scc > graph->src_scc)
3036 e2++;
3039 graph->n_total_row++;
3040 next_band(graph);
3042 for (i = 0; i < graph->n; ++i) {
3043 struct isl_sched_node *node = &graph->node[i];
3044 if (node->scc > graph->src_scc)
3045 node->band_id[graph->n_band] = n;
3048 orig_total_row = graph->n_total_row;
3049 orig_band = graph->n_band;
3050 if (compute_sub_schedule(ctx, graph, n, e1,
3051 &node_scc_at_most, &edge_dst_scc_at_most,
3052 graph->src_scc, 0) < 0)
3053 return -1;
3054 n_total_row = graph->n_total_row;
3055 graph->n_total_row = orig_total_row;
3056 n_band = graph->n_band;
3057 graph->n_band = orig_band;
3058 if (compute_sub_schedule(ctx, graph, graph->n - n, e2,
3059 &node_scc_at_least, &edge_src_scc_at_least,
3060 graph->src_scc + 1, 0) < 0)
3061 return -1;
3062 if (n_total_row > graph->n_total_row)
3063 graph->n_total_row = n_total_row;
3064 if (n_band > graph->n_band)
3065 graph->n_band = n_band;
3067 return pad_schedule(graph);
3070 /* Compute the next band of the schedule after updating the dependence
3071 * relations based on the the current schedule.
3073 static int compute_next_band(isl_ctx *ctx, struct isl_sched_graph *graph)
3075 if (update_edges(ctx, graph) < 0)
3076 return -1;
3077 next_band(graph);
3079 return compute_schedule(ctx, graph);
3082 /* Add constraints to graph->lp that force the dependence "map" (which
3083 * is part of the dependence relation of "edge")
3084 * to be respected and attempt to carry it, where the edge is one from
3085 * a node j to itself. "pos" is the sequence number of the given map.
3086 * That is, add constraints that enforce
3088 * (c_j_0 + c_j_n n + c_j_x y) - (c_j_0 + c_j_n n + c_j_x x)
3089 * = c_j_x (y - x) >= e_i
3091 * for each (x,y) in R.
3092 * We obtain general constraints on coefficients (c_0, c_n, c_x)
3093 * of valid constraints for (y - x) and then plug in (-e_i, 0, c_j_x),
3094 * with each coefficient in c_j_x represented as a pair of non-negative
3095 * coefficients.
3097 static int add_intra_constraints(struct isl_sched_graph *graph,
3098 struct isl_sched_edge *edge, __isl_take isl_map *map, int pos)
3100 unsigned total;
3101 isl_ctx *ctx = isl_map_get_ctx(map);
3102 isl_space *dim;
3103 isl_dim_map *dim_map;
3104 isl_basic_set *coef;
3105 struct isl_sched_node *node = edge->src;
3107 coef = intra_coefficients(graph, node, map);
3108 if (!coef)
3109 return -1;
3111 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
3113 total = isl_basic_set_total_dim(graph->lp);
3114 dim_map = isl_dim_map_alloc(ctx, total);
3115 isl_dim_map_range(dim_map, 3 + pos, 0, 0, 0, 1, -1);
3116 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 1, 2,
3117 isl_space_dim(dim, isl_dim_set), 1,
3118 node->nvar, -1);
3119 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 2, 2,
3120 isl_space_dim(dim, isl_dim_set), 1,
3121 node->nvar, 1);
3122 graph->lp = isl_basic_set_extend_constraints(graph->lp,
3123 coef->n_eq, coef->n_ineq);
3124 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
3125 coef, dim_map);
3126 isl_space_free(dim);
3128 return 0;
3131 /* Add constraints to graph->lp that force the dependence "map" (which
3132 * is part of the dependence relation of "edge")
3133 * to be respected and attempt to carry it, where the edge is one from
3134 * node j to node k. "pos" is the sequence number of the given map.
3135 * That is, add constraints that enforce
3137 * (c_k_0 + c_k_n n + c_k_x y) - (c_j_0 + c_j_n n + c_j_x x) >= e_i
3139 * for each (x,y) in R.
3140 * We obtain general constraints on coefficients (c_0, c_n, c_x)
3141 * of valid constraints for R and then plug in
3142 * (-e_i + c_k_0 - c_j_0, c_k_n - c_j_n, c_k_x - c_j_x)
3143 * with each coefficient (except e_i, c_k_0 and c_j_0)
3144 * represented as a pair of non-negative coefficients.
3146 static int add_inter_constraints(struct isl_sched_graph *graph,
3147 struct isl_sched_edge *edge, __isl_take isl_map *map, int pos)
3149 unsigned total;
3150 isl_ctx *ctx = isl_map_get_ctx(map);
3151 isl_space *dim;
3152 isl_dim_map *dim_map;
3153 isl_basic_set *coef;
3154 struct isl_sched_node *src = edge->src;
3155 struct isl_sched_node *dst = edge->dst;
3157 coef = inter_coefficients(graph, edge, map);
3158 if (!coef)
3159 return -1;
3161 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
3163 total = isl_basic_set_total_dim(graph->lp);
3164 dim_map = isl_dim_map_alloc(ctx, total);
3166 isl_dim_map_range(dim_map, 3 + pos, 0, 0, 0, 1, -1);
3168 isl_dim_map_range(dim_map, dst->start, 0, 0, 0, 1, 1);
3169 isl_dim_map_range(dim_map, dst->start + 1, 2, 1, 1, dst->nparam, -1);
3170 isl_dim_map_range(dim_map, dst->start + 2, 2, 1, 1, dst->nparam, 1);
3171 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 1, 2,
3172 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
3173 dst->nvar, -1);
3174 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 2, 2,
3175 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
3176 dst->nvar, 1);
3178 isl_dim_map_range(dim_map, src->start, 0, 0, 0, 1, -1);
3179 isl_dim_map_range(dim_map, src->start + 1, 2, 1, 1, src->nparam, 1);
3180 isl_dim_map_range(dim_map, src->start + 2, 2, 1, 1, src->nparam, -1);
3181 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 1, 2,
3182 isl_space_dim(dim, isl_dim_set), 1,
3183 src->nvar, 1);
3184 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 2, 2,
3185 isl_space_dim(dim, isl_dim_set), 1,
3186 src->nvar, -1);
3188 graph->lp = isl_basic_set_extend_constraints(graph->lp,
3189 coef->n_eq, coef->n_ineq);
3190 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
3191 coef, dim_map);
3192 isl_space_free(dim);
3194 return 0;
3197 /* Add constraints to graph->lp that force all (conditional) validity
3198 * dependences to be respected and attempt to carry them.
3200 static int add_all_constraints(struct isl_sched_graph *graph)
3202 int i, j;
3203 int pos;
3205 pos = 0;
3206 for (i = 0; i < graph->n_edge; ++i) {
3207 struct isl_sched_edge *edge= &graph->edge[i];
3209 if (!edge->validity && !edge->conditional_validity)
3210 continue;
3212 for (j = 0; j < edge->map->n; ++j) {
3213 isl_basic_map *bmap;
3214 isl_map *map;
3216 bmap = isl_basic_map_copy(edge->map->p[j]);
3217 map = isl_map_from_basic_map(bmap);
3219 if (edge->src == edge->dst &&
3220 add_intra_constraints(graph, edge, map, pos) < 0)
3221 return -1;
3222 if (edge->src != edge->dst &&
3223 add_inter_constraints(graph, edge, map, pos) < 0)
3224 return -1;
3225 ++pos;
3229 return 0;
3232 /* Count the number of equality and inequality constraints
3233 * that will be added to the carry_lp problem.
3234 * We count each edge exactly once.
3236 static int count_all_constraints(struct isl_sched_graph *graph,
3237 int *n_eq, int *n_ineq)
3239 int i, j;
3241 *n_eq = *n_ineq = 0;
3242 for (i = 0; i < graph->n_edge; ++i) {
3243 struct isl_sched_edge *edge= &graph->edge[i];
3244 for (j = 0; j < edge->map->n; ++j) {
3245 isl_basic_map *bmap;
3246 isl_map *map;
3248 bmap = isl_basic_map_copy(edge->map->p[j]);
3249 map = isl_map_from_basic_map(bmap);
3251 if (count_map_constraints(graph, edge, map,
3252 n_eq, n_ineq, 1, 0) < 0)
3253 return -1;
3257 return 0;
3260 /* Construct an LP problem for finding schedule coefficients
3261 * such that the schedule carries as many dependences as possible.
3262 * In particular, for each dependence i, we bound the dependence distance
3263 * from below by e_i, with 0 <= e_i <= 1 and then maximize the sum
3264 * of all e_i's. Dependence with e_i = 0 in the solution are simply
3265 * respected, while those with e_i > 0 (in practice e_i = 1) are carried.
3266 * Note that if the dependence relation is a union of basic maps,
3267 * then we have to consider each basic map individually as it may only
3268 * be possible to carry the dependences expressed by some of those
3269 * basic maps and not all off them.
3270 * Below, we consider each of those basic maps as a separate "edge".
3272 * All variables of the LP are non-negative. The actual coefficients
3273 * may be negative, so each coefficient is represented as the difference
3274 * of two non-negative variables. The negative part always appears
3275 * immediately before the positive part.
3276 * Other than that, the variables have the following order
3278 * - sum of (1 - e_i) over all edges
3279 * - sum of positive and negative parts of all c_n coefficients
3280 * (unconstrained when computing non-parametric schedules)
3281 * - sum of positive and negative parts of all c_x coefficients
3282 * - for each edge
3283 * - e_i
3284 * - for each node
3285 * - c_i_0
3286 * - positive and negative parts of c_i_n (if parametric)
3287 * - positive and negative parts of c_i_x
3289 * The constraints are those from the (validity) edges plus three equalities
3290 * to express the sums and n_edge inequalities to express e_i <= 1.
3292 static int setup_carry_lp(isl_ctx *ctx, struct isl_sched_graph *graph)
3294 int i, j;
3295 int k;
3296 isl_space *dim;
3297 unsigned total;
3298 int n_eq, n_ineq;
3299 int n_edge;
3301 n_edge = 0;
3302 for (i = 0; i < graph->n_edge; ++i)
3303 n_edge += graph->edge[i].map->n;
3305 total = 3 + n_edge;
3306 for (i = 0; i < graph->n; ++i) {
3307 struct isl_sched_node *node = &graph->node[graph->sorted[i]];
3308 node->start = total;
3309 total += 1 + 2 * (node->nparam + node->nvar);
3312 if (count_all_constraints(graph, &n_eq, &n_ineq) < 0)
3313 return -1;
3314 if (count_bound_coefficient_constraints(ctx, graph, &n_eq, &n_ineq) < 0)
3315 return -1;
3317 dim = isl_space_set_alloc(ctx, 0, total);
3318 isl_basic_set_free(graph->lp);
3319 n_eq += 3;
3320 n_ineq += n_edge;
3321 graph->lp = isl_basic_set_alloc_space(dim, 0, n_eq, n_ineq);
3322 graph->lp = isl_basic_set_set_rational(graph->lp);
3324 k = isl_basic_set_alloc_equality(graph->lp);
3325 if (k < 0)
3326 return -1;
3327 isl_seq_clr(graph->lp->eq[k], 1 + total);
3328 isl_int_set_si(graph->lp->eq[k][0], -n_edge);
3329 isl_int_set_si(graph->lp->eq[k][1], 1);
3330 for (i = 0; i < n_edge; ++i)
3331 isl_int_set_si(graph->lp->eq[k][4 + i], 1);
3333 k = isl_basic_set_alloc_equality(graph->lp);
3334 if (k < 0)
3335 return -1;
3336 isl_seq_clr(graph->lp->eq[k], 1 + total);
3337 isl_int_set_si(graph->lp->eq[k][2], -1);
3338 for (i = 0; i < graph->n; ++i) {
3339 int pos = 1 + graph->node[i].start + 1;
3341 for (j = 0; j < 2 * graph->node[i].nparam; ++j)
3342 isl_int_set_si(graph->lp->eq[k][pos + j], 1);
3345 k = isl_basic_set_alloc_equality(graph->lp);
3346 if (k < 0)
3347 return -1;
3348 isl_seq_clr(graph->lp->eq[k], 1 + total);
3349 isl_int_set_si(graph->lp->eq[k][3], -1);
3350 for (i = 0; i < graph->n; ++i) {
3351 struct isl_sched_node *node = &graph->node[i];
3352 int pos = 1 + node->start + 1 + 2 * node->nparam;
3354 for (j = 0; j < 2 * node->nvar; ++j)
3355 isl_int_set_si(graph->lp->eq[k][pos + j], 1);
3358 for (i = 0; i < n_edge; ++i) {
3359 k = isl_basic_set_alloc_inequality(graph->lp);
3360 if (k < 0)
3361 return -1;
3362 isl_seq_clr(graph->lp->ineq[k], 1 + total);
3363 isl_int_set_si(graph->lp->ineq[k][4 + i], -1);
3364 isl_int_set_si(graph->lp->ineq[k][0], 1);
3367 if (add_bound_coefficient_constraints(ctx, graph) < 0)
3368 return -1;
3369 if (add_all_constraints(graph) < 0)
3370 return -1;
3372 return 0;
3375 /* If the schedule_split_scaled option is set and if the linear
3376 * parts of the scheduling rows for all nodes in the graphs have
3377 * non-trivial common divisor, then split off the constant term
3378 * from the linear part.
3379 * The constant term is then placed in a separate band and
3380 * the linear part is reduced.
3382 static int split_scaled(isl_ctx *ctx, struct isl_sched_graph *graph)
3384 int i;
3385 int row;
3386 isl_int gcd, gcd_i;
3388 if (!ctx->opt->schedule_split_scaled)
3389 return 0;
3390 if (graph->n <= 1)
3391 return 0;
3393 if (graph->n_total_row >= graph->max_row)
3394 isl_die(ctx, isl_error_internal,
3395 "too many schedule rows", return -1);
3397 isl_int_init(gcd);
3398 isl_int_init(gcd_i);
3400 isl_int_set_si(gcd, 0);
3402 row = isl_mat_rows(graph->node[0].sched) - 1;
3404 for (i = 0; i < graph->n; ++i) {
3405 struct isl_sched_node *node = &graph->node[i];
3406 int cols = isl_mat_cols(node->sched);
3408 isl_seq_gcd(node->sched->row[row] + 1, cols - 1, &gcd_i);
3409 isl_int_gcd(gcd, gcd, gcd_i);
3412 isl_int_clear(gcd_i);
3414 if (isl_int_cmp_si(gcd, 1) <= 0) {
3415 isl_int_clear(gcd);
3416 return 0;
3419 next_band(graph);
3421 for (i = 0; i < graph->n; ++i) {
3422 struct isl_sched_node *node = &graph->node[i];
3424 isl_map_free(node->sched_map);
3425 node->sched_map = NULL;
3426 node->sched = isl_mat_add_zero_rows(node->sched, 1);
3427 if (!node->sched)
3428 goto error;
3429 isl_int_fdiv_r(node->sched->row[row + 1][0],
3430 node->sched->row[row][0], gcd);
3431 isl_int_fdiv_q(node->sched->row[row][0],
3432 node->sched->row[row][0], gcd);
3433 isl_int_mul(node->sched->row[row][0],
3434 node->sched->row[row][0], gcd);
3435 node->sched = isl_mat_scale_down_row(node->sched, row, gcd);
3436 if (!node->sched)
3437 goto error;
3438 node->band[graph->n_total_row] = graph->n_band;
3441 graph->n_total_row++;
3443 isl_int_clear(gcd);
3444 return 0;
3445 error:
3446 isl_int_clear(gcd);
3447 return -1;
3450 static int compute_component_schedule(isl_ctx *ctx,
3451 struct isl_sched_graph *graph);
3453 /* Is the schedule row "sol" trivial on node "node"?
3454 * That is, is the solution zero on the dimensions orthogonal to
3455 * the previously found solutions?
3456 * Return 1 if the solution is trivial, 0 if it is not and -1 on error.
3458 * Each coefficient is represented as the difference between
3459 * two non-negative values in "sol". "sol" has been computed
3460 * in terms of the original iterators (i.e., without use of cmap).
3461 * We construct the schedule row s and write it as a linear
3462 * combination of (linear combinations of) previously computed schedule rows.
3463 * s = Q c or c = U s.
3464 * If the final entries of c are all zero, then the solution is trivial.
3466 static int is_trivial(struct isl_sched_node *node, __isl_keep isl_vec *sol)
3468 int i;
3469 int pos;
3470 int trivial;
3471 isl_ctx *ctx;
3472 isl_vec *node_sol;
3474 if (!sol)
3475 return -1;
3476 if (node->nvar == node->rank)
3477 return 0;
3479 ctx = isl_vec_get_ctx(sol);
3480 node_sol = isl_vec_alloc(ctx, node->nvar);
3481 if (!node_sol)
3482 return -1;
3484 pos = 1 + node->start + 1 + 2 * node->nparam;
3486 for (i = 0; i < node->nvar; ++i)
3487 isl_int_sub(node_sol->el[i],
3488 sol->el[pos + 2 * i + 1], sol->el[pos + 2 * i]);
3490 node_sol = isl_mat_vec_product(isl_mat_copy(node->cinv), node_sol);
3492 if (!node_sol)
3493 return -1;
3495 trivial = isl_seq_first_non_zero(node_sol->el + node->rank,
3496 node->nvar - node->rank) == -1;
3498 isl_vec_free(node_sol);
3500 return trivial;
3503 /* Is the schedule row "sol" trivial on any node where it should
3504 * not be trivial?
3505 * "sol" has been computed in terms of the original iterators
3506 * (i.e., without use of cmap).
3507 * Return 1 if any solution is trivial, 0 if they are not and -1 on error.
3509 static int is_any_trivial(struct isl_sched_graph *graph,
3510 __isl_keep isl_vec *sol)
3512 int i;
3514 for (i = 0; i < graph->n; ++i) {
3515 struct isl_sched_node *node = &graph->node[i];
3516 int trivial;
3518 if (!needs_row(graph, node))
3519 continue;
3520 trivial = is_trivial(node, sol);
3521 if (trivial < 0 || trivial)
3522 return trivial;
3525 return 0;
3528 /* Construct a schedule row for each node such that as many dependences
3529 * as possible are carried and then continue with the next band.
3531 * If the computed schedule row turns out to be trivial on one or
3532 * more nodes where it should not be trivial, then we throw it away
3533 * and try again on each component separately.
3535 * If there is only one component, then we accept the schedule row anyway,
3536 * but we do not consider it as a complete row and therefore do not
3537 * increment graph->n_row. Note that the ranks of the nodes that
3538 * do get a non-trivial schedule part will get updated regardless and
3539 * graph->maxvar is computed based on these ranks. The test for
3540 * whether more schedule rows are required in compute_schedule_wcc
3541 * is therefore not affected.
3543 static int carry_dependences(isl_ctx *ctx, struct isl_sched_graph *graph)
3545 int i;
3546 int n_edge;
3547 int trivial;
3548 isl_vec *sol;
3549 isl_basic_set *lp;
3551 n_edge = 0;
3552 for (i = 0; i < graph->n_edge; ++i)
3553 n_edge += graph->edge[i].map->n;
3555 if (setup_carry_lp(ctx, graph) < 0)
3556 return -1;
3558 lp = isl_basic_set_copy(graph->lp);
3559 sol = isl_tab_basic_set_non_neg_lexmin(lp);
3560 if (!sol)
3561 return -1;
3563 if (sol->size == 0) {
3564 isl_vec_free(sol);
3565 isl_die(ctx, isl_error_internal,
3566 "error in schedule construction", return -1);
3569 isl_int_divexact(sol->el[1], sol->el[1], sol->el[0]);
3570 if (isl_int_cmp_si(sol->el[1], n_edge) >= 0) {
3571 isl_vec_free(sol);
3572 isl_die(ctx, isl_error_unknown,
3573 "unable to carry dependences", return -1);
3576 trivial = is_any_trivial(graph, sol);
3577 if (trivial < 0) {
3578 sol = isl_vec_free(sol);
3579 } else if (trivial && graph->scc > 1) {
3580 isl_vec_free(sol);
3581 return compute_component_schedule(ctx, graph);
3584 if (update_schedule(graph, sol, 0, 0) < 0)
3585 return -1;
3586 if (trivial)
3587 graph->n_row--;
3589 if (split_scaled(ctx, graph) < 0)
3590 return -1;
3592 return compute_next_band(ctx, graph);
3595 /* Are there any (non-empty) (conditional) validity edges in the graph?
3597 static int has_validity_edges(struct isl_sched_graph *graph)
3599 int i;
3601 for (i = 0; i < graph->n_edge; ++i) {
3602 int empty;
3604 empty = isl_map_plain_is_empty(graph->edge[i].map);
3605 if (empty < 0)
3606 return -1;
3607 if (empty)
3608 continue;
3609 if (graph->edge[i].validity ||
3610 graph->edge[i].conditional_validity)
3611 return 1;
3614 return 0;
3617 /* Should we apply a Feautrier step?
3618 * That is, did the user request the Feautrier algorithm and are
3619 * there any validity dependences (left)?
3621 static int need_feautrier_step(isl_ctx *ctx, struct isl_sched_graph *graph)
3623 if (ctx->opt->schedule_algorithm != ISL_SCHEDULE_ALGORITHM_FEAUTRIER)
3624 return 0;
3626 return has_validity_edges(graph);
3629 /* Compute a schedule for a connected dependence graph using Feautrier's
3630 * multi-dimensional scheduling algorithm.
3631 * The original algorithm is described in [1].
3632 * The main idea is to minimize the number of scheduling dimensions, by
3633 * trying to satisfy as many dependences as possible per scheduling dimension.
3635 * [1] P. Feautrier, Some Efficient Solutions to the Affine Scheduling
3636 * Problem, Part II: Multi-Dimensional Time.
3637 * In Intl. Journal of Parallel Programming, 1992.
3639 static int compute_schedule_wcc_feautrier(isl_ctx *ctx,
3640 struct isl_sched_graph *graph)
3642 return carry_dependences(ctx, graph);
3645 /* Turn off the "local" bit on all (condition) edges.
3647 static void clear_local_edges(struct isl_sched_graph *graph)
3649 int i;
3651 for (i = 0; i < graph->n_edge; ++i)
3652 if (graph->edge[i].condition)
3653 graph->edge[i].local = 0;
3656 /* Does "graph" have both condition and conditional validity edges?
3658 static int need_condition_check(struct isl_sched_graph *graph)
3660 int i;
3661 int any_condition = 0;
3662 int any_conditional_validity = 0;
3664 for (i = 0; i < graph->n_edge; ++i) {
3665 if (graph->edge[i].condition)
3666 any_condition = 1;
3667 if (graph->edge[i].conditional_validity)
3668 any_conditional_validity = 1;
3671 return any_condition && any_conditional_validity;
3674 /* Does "graph" contain any coincidence edge?
3676 static int has_any_coincidence(struct isl_sched_graph *graph)
3678 int i;
3680 for (i = 0; i < graph->n_edge; ++i)
3681 if (graph->edge[i].coincidence)
3682 return 1;
3684 return 0;
3687 /* Extract the final schedule row as a map with the iteration domain
3688 * of "node" as domain.
3690 static __isl_give isl_map *final_row(struct isl_sched_node *node)
3692 isl_local_space *ls;
3693 isl_aff *aff;
3694 int row;
3696 row = isl_mat_rows(node->sched) - 1;
3697 ls = isl_local_space_from_space(isl_space_copy(node->space));
3698 aff = extract_schedule_row(ls, node, row);
3699 return isl_map_from_aff(aff);
3702 /* Is the conditional validity dependence in the edge with index "edge_index"
3703 * violated by the latest (i.e., final) row of the schedule?
3704 * That is, is i scheduled after j
3705 * for any conditional validity dependence i -> j?
3707 static int is_violated(struct isl_sched_graph *graph, int edge_index)
3709 isl_map *src_sched, *dst_sched, *map;
3710 struct isl_sched_edge *edge = &graph->edge[edge_index];
3711 int empty;
3713 src_sched = final_row(edge->src);
3714 dst_sched = final_row(edge->dst);
3715 map = isl_map_copy(edge->map);
3716 map = isl_map_apply_domain(map, src_sched);
3717 map = isl_map_apply_range(map, dst_sched);
3718 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_out, 0);
3719 empty = isl_map_is_empty(map);
3720 isl_map_free(map);
3722 if (empty < 0)
3723 return -1;
3725 return !empty;
3728 /* Does "graph" have any satisfied condition edges that
3729 * are adjacent to the conditional validity constraint with
3730 * domain "conditional_source" and range "conditional_sink"?
3732 * A satisfied condition is one that is not local.
3733 * If a condition was forced to be local already (i.e., marked as local)
3734 * then there is no need to check if it is in fact local.
3736 * Additionally, mark all adjacent condition edges found as local.
3738 static int has_adjacent_true_conditions(struct isl_sched_graph *graph,
3739 __isl_keep isl_union_set *conditional_source,
3740 __isl_keep isl_union_set *conditional_sink)
3742 int i;
3743 int any = 0;
3745 for (i = 0; i < graph->n_edge; ++i) {
3746 int adjacent, local;
3747 isl_union_map *condition;
3749 if (!graph->edge[i].condition)
3750 continue;
3751 if (graph->edge[i].local)
3752 continue;
3754 condition = graph->edge[i].tagged_condition;
3755 adjacent = domain_intersects(condition, conditional_sink);
3756 if (adjacent >= 0 && !adjacent)
3757 adjacent = range_intersects(condition,
3758 conditional_source);
3759 if (adjacent < 0)
3760 return -1;
3761 if (!adjacent)
3762 continue;
3764 graph->edge[i].local = 1;
3766 local = is_condition_false(&graph->edge[i]);
3767 if (local < 0)
3768 return -1;
3769 if (!local)
3770 any = 1;
3773 return any;
3776 /* Are there any violated conditional validity dependences with
3777 * adjacent condition dependences that are not local with respect
3778 * to the current schedule?
3779 * That is, is the conditional validity constraint violated?
3781 * Additionally, mark all those adjacent condition dependences as local.
3782 * We also mark those adjacent condition dependences that were not marked
3783 * as local before, but just happened to be local already. This ensures
3784 * that they remain local if the schedule is recomputed.
3786 * We first collect domain and range of all violated conditional validity
3787 * dependences and then check if there are any adjacent non-local
3788 * condition dependences.
3790 static int has_violated_conditional_constraint(isl_ctx *ctx,
3791 struct isl_sched_graph *graph)
3793 int i;
3794 int any = 0;
3795 isl_union_set *source, *sink;
3797 source = isl_union_set_empty(isl_space_params_alloc(ctx, 0));
3798 sink = isl_union_set_empty(isl_space_params_alloc(ctx, 0));
3799 for (i = 0; i < graph->n_edge; ++i) {
3800 isl_union_set *uset;
3801 isl_union_map *umap;
3802 int violated;
3804 if (!graph->edge[i].conditional_validity)
3805 continue;
3807 violated = is_violated(graph, i);
3808 if (violated < 0)
3809 goto error;
3810 if (!violated)
3811 continue;
3813 any = 1;
3815 umap = isl_union_map_copy(graph->edge[i].tagged_validity);
3816 uset = isl_union_map_domain(umap);
3817 source = isl_union_set_union(source, uset);
3818 source = isl_union_set_coalesce(source);
3820 umap = isl_union_map_copy(graph->edge[i].tagged_validity);
3821 uset = isl_union_map_range(umap);
3822 sink = isl_union_set_union(sink, uset);
3823 sink = isl_union_set_coalesce(sink);
3826 if (any)
3827 any = has_adjacent_true_conditions(graph, source, sink);
3829 isl_union_set_free(source);
3830 isl_union_set_free(sink);
3831 return any;
3832 error:
3833 isl_union_set_free(source);
3834 isl_union_set_free(sink);
3835 return -1;
3838 /* Compute a schedule for a connected dependence graph.
3839 * We try to find a sequence of as many schedule rows as possible that result
3840 * in non-negative dependence distances (independent of the previous rows
3841 * in the sequence, i.e., such that the sequence is tilable), with as
3842 * many of the initial rows as possible satisfying the coincidence constraints.
3843 * If we can't find any more rows we either
3844 * - split between SCCs and start over (assuming we found an interesting
3845 * pair of SCCs between which to split)
3846 * - continue with the next band (assuming the current band has at least
3847 * one row)
3848 * - try to carry as many dependences as possible and continue with the next
3849 * band
3851 * If Feautrier's algorithm is selected, we first recursively try to satisfy
3852 * as many validity dependences as possible. When all validity dependences
3853 * are satisfied we extend the schedule to a full-dimensional schedule.
3855 * If we manage to complete the schedule, we finish off by topologically
3856 * sorting the statements based on the remaining dependences.
3858 * If ctx->opt->schedule_outer_coincidence is set, then we force the
3859 * outermost dimension to satisfy the coincidence constraints. If this
3860 * turns out to be impossible, we fall back on the general scheme above
3861 * and try to carry as many dependences as possible.
3863 * If "graph" contains both condition and conditional validity dependences,
3864 * then we need to check that that the conditional schedule constraint
3865 * is satisfied, i.e., there are no violated conditional validity dependences
3866 * that are adjacent to any non-local condition dependences.
3867 * If there are, then we mark all those adjacent condition dependences
3868 * as local and recompute the current band. Those dependences that
3869 * are marked local will then be forced to be local.
3870 * The initial computation is performed with no dependences marked as local.
3871 * If we are lucky, then there will be no violated conditional validity
3872 * dependences adjacent to any non-local condition dependences.
3873 * Otherwise, we mark some additional condition dependences as local and
3874 * recompute. We continue this process until there are no violations left or
3875 * until we are no longer able to compute a schedule.
3876 * Since there are only a finite number of dependences,
3877 * there will only be a finite number of iterations.
3879 static int compute_schedule_wcc(isl_ctx *ctx, struct isl_sched_graph *graph)
3881 int has_coincidence;
3882 int use_coincidence;
3883 int force_coincidence = 0;
3884 int check_conditional;
3886 if (detect_sccs(ctx, graph) < 0)
3887 return -1;
3888 if (sort_sccs(graph) < 0)
3889 return -1;
3891 if (compute_maxvar(graph) < 0)
3892 return -1;
3894 if (need_feautrier_step(ctx, graph))
3895 return compute_schedule_wcc_feautrier(ctx, graph);
3897 clear_local_edges(graph);
3898 check_conditional = need_condition_check(graph);
3899 has_coincidence = has_any_coincidence(graph);
3901 if (ctx->opt->schedule_outer_coincidence)
3902 force_coincidence = 1;
3904 use_coincidence = has_coincidence;
3905 while (graph->n_row < graph->maxvar) {
3906 isl_vec *sol;
3907 int violated;
3908 int coincident;
3910 graph->src_scc = -1;
3911 graph->dst_scc = -1;
3913 if (setup_lp(ctx, graph, use_coincidence) < 0)
3914 return -1;
3915 sol = solve_lp(graph);
3916 if (!sol)
3917 return -1;
3918 if (sol->size == 0) {
3919 int empty = graph->n_total_row == graph->band_start;
3921 isl_vec_free(sol);
3922 if (use_coincidence && (!force_coincidence || !empty)) {
3923 use_coincidence = 0;
3924 continue;
3926 if (!ctx->opt->schedule_maximize_band_depth && !empty)
3927 return compute_next_band(ctx, graph);
3928 if (graph->src_scc >= 0)
3929 return compute_split_schedule(ctx, graph);
3930 if (!empty)
3931 return compute_next_band(ctx, graph);
3932 return carry_dependences(ctx, graph);
3934 coincident = !has_coincidence || use_coincidence;
3935 if (update_schedule(graph, sol, 1, coincident) < 0)
3936 return -1;
3938 if (!check_conditional)
3939 continue;
3940 violated = has_violated_conditional_constraint(ctx, graph);
3941 if (violated < 0)
3942 return -1;
3943 if (!violated)
3944 continue;
3945 if (reset_band(graph) < 0)
3946 return -1;
3947 use_coincidence = has_coincidence;
3950 if (graph->n_total_row > graph->band_start)
3951 next_band(graph);
3952 return sort_statements(ctx, graph);
3955 /* Add a row to the schedules that separates the SCCs and move
3956 * to the next band.
3958 static int split_on_scc(isl_ctx *ctx, struct isl_sched_graph *graph)
3960 int i;
3962 if (graph->n_total_row >= graph->max_row)
3963 isl_die(ctx, isl_error_internal,
3964 "too many schedule rows", return -1);
3966 for (i = 0; i < graph->n; ++i) {
3967 struct isl_sched_node *node = &graph->node[i];
3968 int row = isl_mat_rows(node->sched);
3970 isl_map_free(node->sched_map);
3971 node->sched_map = NULL;
3972 node->sched = isl_mat_add_zero_rows(node->sched, 1);
3973 node->sched = isl_mat_set_element_si(node->sched, row, 0,
3974 node->scc);
3975 if (!node->sched)
3976 return -1;
3977 node->band[graph->n_total_row] = graph->n_band;
3980 graph->n_total_row++;
3981 next_band(graph);
3983 return 0;
3986 /* Compute a schedule for each component (identified by node->scc)
3987 * of the dependence graph separately and then combine the results.
3988 * Depending on the setting of schedule_fuse, a component may be
3989 * either weakly or strongly connected.
3991 * The band_id is adjusted such that each component has a separate id.
3992 * Note that the band_id may have already been set to a value different
3993 * from zero by compute_split_schedule.
3995 static int compute_component_schedule(isl_ctx *ctx,
3996 struct isl_sched_graph *graph)
3998 int wcc, i;
3999 int n, n_edge;
4000 int n_total_row, orig_total_row;
4001 int n_band, orig_band;
4003 if (ctx->opt->schedule_fuse == ISL_SCHEDULE_FUSE_MIN ||
4004 ctx->opt->schedule_separate_components)
4005 if (split_on_scc(ctx, graph) < 0)
4006 return -1;
4008 n_total_row = 0;
4009 orig_total_row = graph->n_total_row;
4010 n_band = 0;
4011 orig_band = graph->n_band;
4012 for (i = 0; i < graph->n; ++i)
4013 graph->node[i].band_id[graph->n_band] += graph->node[i].scc;
4014 for (wcc = 0; wcc < graph->scc; ++wcc) {
4015 n = 0;
4016 for (i = 0; i < graph->n; ++i)
4017 if (graph->node[i].scc == wcc)
4018 n++;
4019 n_edge = 0;
4020 for (i = 0; i < graph->n_edge; ++i)
4021 if (graph->edge[i].src->scc == wcc &&
4022 graph->edge[i].dst->scc == wcc)
4023 n_edge++;
4025 if (compute_sub_schedule(ctx, graph, n, n_edge,
4026 &node_scc_exactly,
4027 &edge_scc_exactly, wcc, 1) < 0)
4028 return -1;
4029 if (graph->n_total_row > n_total_row)
4030 n_total_row = graph->n_total_row;
4031 graph->n_total_row = orig_total_row;
4032 if (graph->n_band > n_band)
4033 n_band = graph->n_band;
4034 graph->n_band = orig_band;
4037 graph->n_total_row = n_total_row;
4038 graph->n_band = n_band;
4040 return pad_schedule(graph);
4043 /* Compute a schedule for the given dependence graph.
4044 * We first check if the graph is connected (through validity and conditional
4045 * validity dependences) and, if not, compute a schedule
4046 * for each component separately.
4047 * If schedule_fuse is set to minimal fusion, then we check for strongly
4048 * connected components instead and compute a separate schedule for
4049 * each such strongly connected component.
4051 static int compute_schedule(isl_ctx *ctx, struct isl_sched_graph *graph)
4053 if (ctx->opt->schedule_fuse == ISL_SCHEDULE_FUSE_MIN) {
4054 if (detect_sccs(ctx, graph) < 0)
4055 return -1;
4056 } else {
4057 if (detect_wccs(ctx, graph) < 0)
4058 return -1;
4061 if (graph->scc > 1)
4062 return compute_component_schedule(ctx, graph);
4064 return compute_schedule_wcc(ctx, graph);
4067 /* Compute a schedule on sc->domain that respects the given schedule
4068 * constraints.
4070 * In particular, the schedule respects all the validity dependences.
4071 * If the default isl scheduling algorithm is used, it tries to minimize
4072 * the dependence distances over the proximity dependences.
4073 * If Feautrier's scheduling algorithm is used, the proximity dependence
4074 * distances are only minimized during the extension to a full-dimensional
4075 * schedule.
4077 * If there are any condition and conditional validity dependences,
4078 * then the conditional validity dependences may be violated inside
4079 * a tilable band, provided they have no adjacent non-local
4080 * condition dependences.
4082 __isl_give isl_schedule *isl_schedule_constraints_compute_schedule(
4083 __isl_take isl_schedule_constraints *sc)
4085 isl_ctx *ctx = isl_schedule_constraints_get_ctx(sc);
4086 struct isl_sched_graph graph = { 0 };
4087 isl_schedule *sched;
4088 struct isl_extract_edge_data data;
4089 enum isl_edge_type i;
4091 sc = isl_schedule_constraints_align_params(sc);
4092 if (!sc)
4093 return NULL;
4095 graph.n = isl_union_set_n_set(sc->domain);
4096 if (graph.n == 0)
4097 goto empty;
4098 if (graph_alloc(ctx, &graph, graph.n,
4099 isl_schedule_constraints_n_map(sc)) < 0)
4100 goto error;
4101 if (compute_max_row(&graph, sc) < 0)
4102 goto error;
4103 graph.root = 1;
4104 graph.n = 0;
4105 if (isl_union_set_foreach_set(sc->domain, &extract_node, &graph) < 0)
4106 goto error;
4107 if (graph_init_table(ctx, &graph) < 0)
4108 goto error;
4109 for (i = isl_edge_first; i <= isl_edge_last; ++i)
4110 graph.max_edge[i] = isl_union_map_n_map(sc->constraint[i]);
4111 if (graph_init_edge_tables(ctx, &graph) < 0)
4112 goto error;
4113 graph.n_edge = 0;
4114 data.graph = &graph;
4115 for (i = isl_edge_first; i <= isl_edge_last; ++i) {
4116 data.type = i;
4117 if (isl_union_map_foreach_map(sc->constraint[i],
4118 &extract_edge, &data) < 0)
4119 goto error;
4122 if (compute_schedule(ctx, &graph) < 0)
4123 goto error;
4125 empty:
4126 sched = extract_schedule(&graph, isl_union_set_get_space(sc->domain));
4128 graph_free(ctx, &graph);
4129 isl_schedule_constraints_free(sc);
4131 return sched;
4132 error:
4133 graph_free(ctx, &graph);
4134 isl_schedule_constraints_free(sc);
4135 return NULL;
4138 /* Compute a schedule for the given union of domains that respects
4139 * all the validity dependences and minimizes
4140 * the dependence distances over the proximity dependences.
4142 * This function is kept for backward compatibility.
4144 __isl_give isl_schedule *isl_union_set_compute_schedule(
4145 __isl_take isl_union_set *domain,
4146 __isl_take isl_union_map *validity,
4147 __isl_take isl_union_map *proximity)
4149 isl_schedule_constraints *sc;
4151 sc = isl_schedule_constraints_on_domain(domain);
4152 sc = isl_schedule_constraints_set_validity(sc, validity);
4153 sc = isl_schedule_constraints_set_proximity(sc, proximity);
4155 return isl_schedule_constraints_compute_schedule(sc);