isl_scheduler.c: extract_edge: extract out skip_edge
[isl.git] / isl_scheduler.c
blobe3dbb7640f6035afb4e71a7b03573f5c7fc37ca6
1 /*
2 * Copyright 2011 INRIA Saclay
3 * Copyright 2012-2014 Ecole Normale Superieure
4 * Copyright 2015-2016 Sven Verdoolaege
5 * Copyright 2016 INRIA Paris
7 * Use of this software is governed by the MIT license
9 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
10 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
11 * 91893 Orsay, France
12 * and Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
13 * and Centre de Recherche Inria de Paris, 2 rue Simone Iff - Voie DQ12,
14 * CS 42112, 75589 Paris Cedex 12, France
17 #include <isl_ctx_private.h>
18 #include <isl_map_private.h>
19 #include <isl_space_private.h>
20 #include <isl_aff_private.h>
21 #include <isl/hash.h>
22 #include <isl/constraint.h>
23 #include <isl/schedule.h>
24 #include <isl_schedule_constraints.h>
25 #include <isl/schedule_node.h>
26 #include <isl_mat_private.h>
27 #include <isl_vec_private.h>
28 #include <isl/set.h>
29 #include <isl/union_set.h>
30 #include <isl_seq.h>
31 #include <isl_tab.h>
32 #include <isl_dim_map.h>
33 #include <isl/map_to_basic_set.h>
34 #include <isl_sort.h>
35 #include <isl_options_private.h>
36 #include <isl_tarjan.h>
37 #include <isl_morph.h>
38 #include <isl/ilp.h>
39 #include <isl_val_private.h>
42 * The scheduling algorithm implemented in this file was inspired by
43 * Bondhugula et al., "Automatic Transformations for Communication-Minimized
44 * Parallelization and Locality Optimization in the Polyhedral Model".
48 /* Internal information about a node that is used during the construction
49 * of a schedule.
50 * space represents the space in which the domain lives
51 * sched is a matrix representation of the schedule being constructed
52 * for this node; if compressed is set, then this schedule is
53 * defined over the compressed domain space
54 * sched_map is an isl_map representation of the same (partial) schedule
55 * sched_map may be NULL; if compressed is set, then this map
56 * is defined over the uncompressed domain space
57 * rank is the number of linearly independent rows in the linear part
58 * of sched
59 * the columns of cmap represent a change of basis for the schedule
60 * coefficients; the first rank columns span the linear part of
61 * the schedule rows
62 * cinv is the inverse of cmap.
63 * ctrans is the transpose of cmap.
64 * start is the first variable in the LP problem in the sequences that
65 * represents the schedule coefficients of this node
66 * nvar is the dimension of the domain
67 * nparam is the number of parameters or 0 if we are not constructing
68 * a parametric schedule
70 * If compressed is set, then hull represents the constraints
71 * that were used to derive the compression, while compress and
72 * decompress map the original space to the compressed space and
73 * vice versa.
75 * scc is the index of SCC (or WCC) this node belongs to
77 * "cluster" is only used inside extract_clusters and identifies
78 * the cluster of SCCs that the node belongs to.
80 * coincident contains a boolean for each of the rows of the schedule,
81 * indicating whether the corresponding scheduling dimension satisfies
82 * the coincidence constraints in the sense that the corresponding
83 * dependence distances are zero.
85 * If the schedule_treat_coalescing option is set, then
86 * "sizes" contains the sizes of the (compressed) instance set
87 * in each direction. If there is no fixed size in a given direction,
88 * then the corresponding size value is set to infinity.
89 * If the schedule_treat_coalescing option or the schedule_max_coefficient
90 * option is set, then "max" contains the maximal values for
91 * schedule coefficients of the (compressed) variables. If no bound
92 * needs to be imposed on a particular variable, then the corresponding
93 * value is negative.
95 struct isl_sched_node {
96 isl_space *space;
97 int compressed;
98 isl_set *hull;
99 isl_multi_aff *compress;
100 isl_multi_aff *decompress;
101 isl_mat *sched;
102 isl_map *sched_map;
103 int rank;
104 isl_mat *cmap;
105 isl_mat *cinv;
106 isl_mat *ctrans;
107 int start;
108 int nvar;
109 int nparam;
111 int scc;
112 int cluster;
114 int *coincident;
116 isl_multi_val *sizes;
117 isl_vec *max;
120 static int node_has_space(const void *entry, const void *val)
122 struct isl_sched_node *node = (struct isl_sched_node *)entry;
123 isl_space *dim = (isl_space *)val;
125 return isl_space_is_equal(node->space, dim);
128 static int node_scc_exactly(struct isl_sched_node *node, int scc)
130 return node->scc == scc;
133 static int node_scc_at_most(struct isl_sched_node *node, int scc)
135 return node->scc <= scc;
138 static int node_scc_at_least(struct isl_sched_node *node, int scc)
140 return node->scc >= scc;
143 /* An edge in the dependence graph. An edge may be used to
144 * ensure validity of the generated schedule, to minimize the dependence
145 * distance or both
147 * map is the dependence relation, with i -> j in the map if j depends on i
148 * tagged_condition and tagged_validity contain the union of all tagged
149 * condition or conditional validity dependence relations that
150 * specialize the dependence relation "map"; that is,
151 * if (i -> a) -> (j -> b) is an element of "tagged_condition"
152 * or "tagged_validity", then i -> j is an element of "map".
153 * If these fields are NULL, then they represent the empty relation.
154 * src is the source node
155 * dst is the sink node
157 * types is a bit vector containing the types of this edge.
158 * validity is set if the edge is used to ensure correctness
159 * coincidence is used to enforce zero dependence distances
160 * proximity is set if the edge is used to minimize dependence distances
161 * condition is set if the edge represents a condition
162 * for a conditional validity schedule constraint
163 * local can only be set for condition edges and indicates that
164 * the dependence distance over the edge should be zero
165 * conditional_validity is set if the edge is used to conditionally
166 * ensure correctness
168 * For validity edges, start and end mark the sequence of inequality
169 * constraints in the LP problem that encode the validity constraint
170 * corresponding to this edge.
172 * During clustering, an edge may be marked "no_merge" if it should
173 * not be used to merge clusters.
174 * The weight is also only used during clustering and it is
175 * an indication of how many schedule dimensions on either side
176 * of the schedule constraints can be aligned.
177 * If the weight is negative, then this means that this edge was postponed
178 * by has_bounded_distances or any_no_merge. The original weight can
179 * be retrieved by adding 1 + graph->max_weight, with "graph"
180 * the graph containing this edge.
182 struct isl_sched_edge {
183 isl_map *map;
184 isl_union_map *tagged_condition;
185 isl_union_map *tagged_validity;
187 struct isl_sched_node *src;
188 struct isl_sched_node *dst;
190 unsigned types;
192 int start;
193 int end;
195 int no_merge;
196 int weight;
199 /* Is "edge" marked as being of type "type"?
201 static int is_type(struct isl_sched_edge *edge, enum isl_edge_type type)
203 return ISL_FL_ISSET(edge->types, 1 << type);
206 /* Mark "edge" as being of type "type".
208 static void set_type(struct isl_sched_edge *edge, enum isl_edge_type type)
210 ISL_FL_SET(edge->types, 1 << type);
213 /* No longer mark "edge" as being of type "type"?
215 static void clear_type(struct isl_sched_edge *edge, enum isl_edge_type type)
217 ISL_FL_CLR(edge->types, 1 << type);
220 /* Is "edge" marked as a validity edge?
222 static int is_validity(struct isl_sched_edge *edge)
224 return is_type(edge, isl_edge_validity);
227 /* Mark "edge" as a validity edge.
229 static void set_validity(struct isl_sched_edge *edge)
231 set_type(edge, isl_edge_validity);
234 /* Is "edge" marked as a proximity edge?
236 static int is_proximity(struct isl_sched_edge *edge)
238 return is_type(edge, isl_edge_proximity);
241 /* Is "edge" marked as a local edge?
243 static int is_local(struct isl_sched_edge *edge)
245 return is_type(edge, isl_edge_local);
248 /* Mark "edge" as a local edge.
250 static void set_local(struct isl_sched_edge *edge)
252 set_type(edge, isl_edge_local);
255 /* No longer mark "edge" as a local edge.
257 static void clear_local(struct isl_sched_edge *edge)
259 clear_type(edge, isl_edge_local);
262 /* Is "edge" marked as a coincidence edge?
264 static int is_coincidence(struct isl_sched_edge *edge)
266 return is_type(edge, isl_edge_coincidence);
269 /* Is "edge" marked as a condition edge?
271 static int is_condition(struct isl_sched_edge *edge)
273 return is_type(edge, isl_edge_condition);
276 /* Is "edge" marked as a conditional validity edge?
278 static int is_conditional_validity(struct isl_sched_edge *edge)
280 return is_type(edge, isl_edge_conditional_validity);
283 /* Is "edge" of a type that can appear multiple times between
284 * the same pair of nodes?
286 * Condition edges and conditional validity edges may have tagged
287 * dependence relations, in which case an edge is added for each
288 * pair of tags.
290 static int is_multi_edge_type(struct isl_sched_edge *edge)
292 return is_condition(edge) || is_conditional_validity(edge);
295 /* Internal information about the dependence graph used during
296 * the construction of the schedule.
298 * intra_hmap is a cache, mapping dependence relations to their dual,
299 * for dependences from a node to itself
300 * inter_hmap is a cache, mapping dependence relations to their dual,
301 * for dependences between distinct nodes
302 * if compression is involved then the key for these maps
303 * is the original, uncompressed dependence relation, while
304 * the value is the dual of the compressed dependence relation.
306 * n is the number of nodes
307 * node is the list of nodes
308 * maxvar is the maximal number of variables over all nodes
309 * max_row is the allocated number of rows in the schedule
310 * n_row is the current (maximal) number of linearly independent
311 * rows in the node schedules
312 * n_total_row is the current number of rows in the node schedules
313 * band_start is the starting row in the node schedules of the current band
314 * root is set if this graph is the original dependence graph,
315 * without any splitting
317 * sorted contains a list of node indices sorted according to the
318 * SCC to which a node belongs
320 * n_edge is the number of edges
321 * edge is the list of edges
322 * max_edge contains the maximal number of edges of each type;
323 * in particular, it contains the number of edges in the inital graph.
324 * edge_table contains pointers into the edge array, hashed on the source
325 * and sink spaces; there is one such table for each type;
326 * a given edge may be referenced from more than one table
327 * if the corresponding relation appears in more than one of the
328 * sets of dependences; however, for each type there is only
329 * a single edge between a given pair of source and sink space
330 * in the entire graph
332 * node_table contains pointers into the node array, hashed on the space
334 * region contains a list of variable sequences that should be non-trivial
336 * lp contains the (I)LP problem used to obtain new schedule rows
338 * src_scc and dst_scc are the source and sink SCCs of an edge with
339 * conflicting constraints
341 * scc represents the number of components
342 * weak is set if the components are weakly connected
344 * max_weight is used during clustering and represents the maximal
345 * weight of the relevant proximity edges.
347 struct isl_sched_graph {
348 isl_map_to_basic_set *intra_hmap;
349 isl_map_to_basic_set *inter_hmap;
351 struct isl_sched_node *node;
352 int n;
353 int maxvar;
354 int max_row;
355 int n_row;
357 int *sorted;
359 int n_total_row;
360 int band_start;
362 int root;
364 struct isl_sched_edge *edge;
365 int n_edge;
366 int max_edge[isl_edge_last + 1];
367 struct isl_hash_table *edge_table[isl_edge_last + 1];
369 struct isl_hash_table *node_table;
370 struct isl_region *region;
372 isl_basic_set *lp;
374 int src_scc;
375 int dst_scc;
377 int scc;
378 int weak;
380 int max_weight;
383 /* Initialize node_table based on the list of nodes.
385 static int graph_init_table(isl_ctx *ctx, struct isl_sched_graph *graph)
387 int i;
389 graph->node_table = isl_hash_table_alloc(ctx, graph->n);
390 if (!graph->node_table)
391 return -1;
393 for (i = 0; i < graph->n; ++i) {
394 struct isl_hash_table_entry *entry;
395 uint32_t hash;
397 hash = isl_space_get_hash(graph->node[i].space);
398 entry = isl_hash_table_find(ctx, graph->node_table, hash,
399 &node_has_space,
400 graph->node[i].space, 1);
401 if (!entry)
402 return -1;
403 entry->data = &graph->node[i];
406 return 0;
409 /* Return a pointer to the node that lives within the given space,
410 * or NULL if there is no such node.
412 static struct isl_sched_node *graph_find_node(isl_ctx *ctx,
413 struct isl_sched_graph *graph, __isl_keep isl_space *dim)
415 struct isl_hash_table_entry *entry;
416 uint32_t hash;
418 hash = isl_space_get_hash(dim);
419 entry = isl_hash_table_find(ctx, graph->node_table, hash,
420 &node_has_space, dim, 0);
422 return entry ? entry->data : NULL;
425 static int edge_has_src_and_dst(const void *entry, const void *val)
427 const struct isl_sched_edge *edge = entry;
428 const struct isl_sched_edge *temp = val;
430 return edge->src == temp->src && edge->dst == temp->dst;
433 /* Add the given edge to graph->edge_table[type].
435 static isl_stat graph_edge_table_add(isl_ctx *ctx,
436 struct isl_sched_graph *graph, enum isl_edge_type type,
437 struct isl_sched_edge *edge)
439 struct isl_hash_table_entry *entry;
440 uint32_t hash;
442 hash = isl_hash_init();
443 hash = isl_hash_builtin(hash, edge->src);
444 hash = isl_hash_builtin(hash, edge->dst);
445 entry = isl_hash_table_find(ctx, graph->edge_table[type], hash,
446 &edge_has_src_and_dst, edge, 1);
447 if (!entry)
448 return isl_stat_error;
449 entry->data = edge;
451 return isl_stat_ok;
454 /* Add "edge" to all relevant edge tables.
455 * That is, for every type of the edge, add it to the corresponding table.
457 static isl_stat graph_edge_tables_add(isl_ctx *ctx,
458 struct isl_sched_graph *graph, struct isl_sched_edge *edge)
460 enum isl_edge_type t;
462 for (t = isl_edge_first; t <= isl_edge_last; ++t) {
463 if (!is_type(edge, t))
464 continue;
465 if (graph_edge_table_add(ctx, graph, t, edge) < 0)
466 return isl_stat_error;
469 return isl_stat_ok;
472 /* Allocate the edge_tables based on the maximal number of edges of
473 * each type.
475 static int graph_init_edge_tables(isl_ctx *ctx, struct isl_sched_graph *graph)
477 int i;
479 for (i = 0; i <= isl_edge_last; ++i) {
480 graph->edge_table[i] = isl_hash_table_alloc(ctx,
481 graph->max_edge[i]);
482 if (!graph->edge_table[i])
483 return -1;
486 return 0;
489 /* If graph->edge_table[type] contains an edge from the given source
490 * to the given destination, then return the hash table entry of this edge.
491 * Otherwise, return NULL.
493 static struct isl_hash_table_entry *graph_find_edge_entry(
494 struct isl_sched_graph *graph,
495 enum isl_edge_type type,
496 struct isl_sched_node *src, struct isl_sched_node *dst)
498 isl_ctx *ctx = isl_space_get_ctx(src->space);
499 uint32_t hash;
500 struct isl_sched_edge temp = { .src = src, .dst = dst };
502 hash = isl_hash_init();
503 hash = isl_hash_builtin(hash, temp.src);
504 hash = isl_hash_builtin(hash, temp.dst);
505 return isl_hash_table_find(ctx, graph->edge_table[type], hash,
506 &edge_has_src_and_dst, &temp, 0);
510 /* If graph->edge_table[type] contains an edge from the given source
511 * to the given destination, then return this edge.
512 * Otherwise, return NULL.
514 static struct isl_sched_edge *graph_find_edge(struct isl_sched_graph *graph,
515 enum isl_edge_type type,
516 struct isl_sched_node *src, struct isl_sched_node *dst)
518 struct isl_hash_table_entry *entry;
520 entry = graph_find_edge_entry(graph, type, src, dst);
521 if (!entry)
522 return NULL;
524 return entry->data;
527 /* Check whether the dependence graph has an edge of the given type
528 * between the given two nodes.
530 static isl_bool graph_has_edge(struct isl_sched_graph *graph,
531 enum isl_edge_type type,
532 struct isl_sched_node *src, struct isl_sched_node *dst)
534 struct isl_sched_edge *edge;
535 isl_bool empty;
537 edge = graph_find_edge(graph, type, src, dst);
538 if (!edge)
539 return 0;
541 empty = isl_map_plain_is_empty(edge->map);
542 if (empty < 0)
543 return isl_bool_error;
545 return !empty;
548 /* Look for any edge with the same src, dst and map fields as "model".
550 * Return the matching edge if one can be found.
551 * Return "model" if no matching edge is found.
552 * Return NULL on error.
554 static struct isl_sched_edge *graph_find_matching_edge(
555 struct isl_sched_graph *graph, struct isl_sched_edge *model)
557 enum isl_edge_type i;
558 struct isl_sched_edge *edge;
560 for (i = isl_edge_first; i <= isl_edge_last; ++i) {
561 int is_equal;
563 edge = graph_find_edge(graph, i, model->src, model->dst);
564 if (!edge)
565 continue;
566 is_equal = isl_map_plain_is_equal(model->map, edge->map);
567 if (is_equal < 0)
568 return NULL;
569 if (is_equal)
570 return edge;
573 return model;
576 /* Remove the given edge from all the edge_tables that refer to it.
578 static void graph_remove_edge(struct isl_sched_graph *graph,
579 struct isl_sched_edge *edge)
581 isl_ctx *ctx = isl_map_get_ctx(edge->map);
582 enum isl_edge_type i;
584 for (i = isl_edge_first; i <= isl_edge_last; ++i) {
585 struct isl_hash_table_entry *entry;
587 entry = graph_find_edge_entry(graph, i, edge->src, edge->dst);
588 if (!entry)
589 continue;
590 if (entry->data != edge)
591 continue;
592 isl_hash_table_remove(ctx, graph->edge_table[i], entry);
596 /* Check whether the dependence graph has any edge
597 * between the given two nodes.
599 static isl_bool graph_has_any_edge(struct isl_sched_graph *graph,
600 struct isl_sched_node *src, struct isl_sched_node *dst)
602 enum isl_edge_type i;
603 isl_bool r;
605 for (i = isl_edge_first; i <= isl_edge_last; ++i) {
606 r = graph_has_edge(graph, i, src, dst);
607 if (r < 0 || r)
608 return r;
611 return r;
614 /* Check whether the dependence graph has a validity edge
615 * between the given two nodes.
617 * Conditional validity edges are essentially validity edges that
618 * can be ignored if the corresponding condition edges are iteration private.
619 * Here, we are only checking for the presence of validity
620 * edges, so we need to consider the conditional validity edges too.
621 * In particular, this function is used during the detection
622 * of strongly connected components and we cannot ignore
623 * conditional validity edges during this detection.
625 static isl_bool graph_has_validity_edge(struct isl_sched_graph *graph,
626 struct isl_sched_node *src, struct isl_sched_node *dst)
628 isl_bool r;
630 r = graph_has_edge(graph, isl_edge_validity, src, dst);
631 if (r < 0 || r)
632 return r;
634 return graph_has_edge(graph, isl_edge_conditional_validity, src, dst);
637 static int graph_alloc(isl_ctx *ctx, struct isl_sched_graph *graph,
638 int n_node, int n_edge)
640 int i;
642 graph->n = n_node;
643 graph->n_edge = n_edge;
644 graph->node = isl_calloc_array(ctx, struct isl_sched_node, graph->n);
645 graph->sorted = isl_calloc_array(ctx, int, graph->n);
646 graph->region = isl_alloc_array(ctx, struct isl_region, graph->n);
647 graph->edge = isl_calloc_array(ctx,
648 struct isl_sched_edge, graph->n_edge);
650 graph->intra_hmap = isl_map_to_basic_set_alloc(ctx, 2 * n_edge);
651 graph->inter_hmap = isl_map_to_basic_set_alloc(ctx, 2 * n_edge);
653 if (!graph->node || !graph->region || (graph->n_edge && !graph->edge) ||
654 !graph->sorted)
655 return -1;
657 for(i = 0; i < graph->n; ++i)
658 graph->sorted[i] = i;
660 return 0;
663 static void graph_free(isl_ctx *ctx, struct isl_sched_graph *graph)
665 int i;
667 isl_map_to_basic_set_free(graph->intra_hmap);
668 isl_map_to_basic_set_free(graph->inter_hmap);
670 if (graph->node)
671 for (i = 0; i < graph->n; ++i) {
672 isl_space_free(graph->node[i].space);
673 isl_set_free(graph->node[i].hull);
674 isl_multi_aff_free(graph->node[i].compress);
675 isl_multi_aff_free(graph->node[i].decompress);
676 isl_mat_free(graph->node[i].sched);
677 isl_map_free(graph->node[i].sched_map);
678 isl_mat_free(graph->node[i].cmap);
679 isl_mat_free(graph->node[i].cinv);
680 isl_mat_free(graph->node[i].ctrans);
681 if (graph->root)
682 free(graph->node[i].coincident);
683 isl_multi_val_free(graph->node[i].sizes);
684 isl_vec_free(graph->node[i].max);
686 free(graph->node);
687 free(graph->sorted);
688 if (graph->edge)
689 for (i = 0; i < graph->n_edge; ++i) {
690 isl_map_free(graph->edge[i].map);
691 isl_union_map_free(graph->edge[i].tagged_condition);
692 isl_union_map_free(graph->edge[i].tagged_validity);
694 free(graph->edge);
695 free(graph->region);
696 for (i = 0; i <= isl_edge_last; ++i)
697 isl_hash_table_free(ctx, graph->edge_table[i]);
698 isl_hash_table_free(ctx, graph->node_table);
699 isl_basic_set_free(graph->lp);
702 /* For each "set" on which this function is called, increment
703 * graph->n by one and update graph->maxvar.
705 static isl_stat init_n_maxvar(__isl_take isl_set *set, void *user)
707 struct isl_sched_graph *graph = user;
708 int nvar = isl_set_dim(set, isl_dim_set);
710 graph->n++;
711 if (nvar > graph->maxvar)
712 graph->maxvar = nvar;
714 isl_set_free(set);
716 return isl_stat_ok;
719 /* Compute the number of rows that should be allocated for the schedule.
720 * In particular, we need one row for each variable or one row
721 * for each basic map in the dependences.
722 * Note that it is practically impossible to exhaust both
723 * the number of dependences and the number of variables.
725 static isl_stat compute_max_row(struct isl_sched_graph *graph,
726 __isl_keep isl_schedule_constraints *sc)
728 int n_edge;
729 isl_stat r;
730 isl_union_set *domain;
732 graph->n = 0;
733 graph->maxvar = 0;
734 domain = isl_schedule_constraints_get_domain(sc);
735 r = isl_union_set_foreach_set(domain, &init_n_maxvar, graph);
736 isl_union_set_free(domain);
737 if (r < 0)
738 return isl_stat_error;
739 n_edge = isl_schedule_constraints_n_basic_map(sc);
740 if (n_edge < 0)
741 return isl_stat_error;
742 graph->max_row = n_edge + graph->maxvar;
744 return isl_stat_ok;
747 /* Does "bset" have any defining equalities for its set variables?
749 static int has_any_defining_equality(__isl_keep isl_basic_set *bset)
751 int i, n;
753 if (!bset)
754 return -1;
756 n = isl_basic_set_dim(bset, isl_dim_set);
757 for (i = 0; i < n; ++i) {
758 int has;
760 has = isl_basic_set_has_defining_equality(bset, isl_dim_set, i,
761 NULL);
762 if (has < 0 || has)
763 return has;
766 return 0;
769 /* Set the entries of node->max to the value of the schedule_max_coefficient
770 * option, if set.
772 static isl_stat set_max_coefficient(isl_ctx *ctx, struct isl_sched_node *node)
774 int max;
776 max = isl_options_get_schedule_max_coefficient(ctx);
777 if (max == -1)
778 return isl_stat_ok;
780 node->max = isl_vec_alloc(ctx, node->nvar);
781 node->max = isl_vec_set_si(node->max, max);
782 if (!node->max)
783 return isl_stat_error;
785 return isl_stat_ok;
788 /* Set the entries of node->max to the minimum of the schedule_max_coefficient
789 * option (if set) and half of the minimum of the sizes in the other
790 * dimensions. If the minimum of the sizes is one, half of the size
791 * is zero and this value is reset to one.
792 * If the global minimum is unbounded (i.e., if both
793 * the schedule_max_coefficient is not set and the sizes in the other
794 * dimensions are unbounded), then store a negative value.
795 * If the schedule coefficient is close to the size of the instance set
796 * in another dimension, then the schedule may represent a loop
797 * coalescing transformation (especially if the coefficient
798 * in that other dimension is one). Forcing the coefficient to be
799 * smaller than or equal to half the minimal size should avoid this
800 * situation.
802 static isl_stat compute_max_coefficient(isl_ctx *ctx,
803 struct isl_sched_node *node)
805 int max;
806 int i, j;
807 isl_vec *v;
809 max = isl_options_get_schedule_max_coefficient(ctx);
810 v = isl_vec_alloc(ctx, node->nvar);
811 if (!v)
812 return isl_stat_error;
814 for (i = 0; i < node->nvar; ++i) {
815 isl_int_set_si(v->el[i], max);
816 isl_int_mul_si(v->el[i], v->el[i], 2);
819 for (i = 0; i < node->nvar; ++i) {
820 isl_val *size;
822 size = isl_multi_val_get_val(node->sizes, i);
823 if (!size)
824 goto error;
825 if (!isl_val_is_int(size)) {
826 isl_val_free(size);
827 continue;
829 for (j = 0; j < node->nvar; ++j) {
830 if (j == i)
831 continue;
832 if (isl_int_is_neg(v->el[j]) ||
833 isl_int_gt(v->el[j], size->n))
834 isl_int_set(v->el[j], size->n);
836 isl_val_free(size);
839 for (i = 0; i < node->nvar; ++i) {
840 isl_int_fdiv_q_ui(v->el[i], v->el[i], 2);
841 if (isl_int_is_zero(v->el[i]))
842 isl_int_set_si(v->el[i], 1);
845 node->max = v;
846 return isl_stat_ok;
847 error:
848 isl_vec_free(v);
849 return isl_stat_error;
852 /* Compute and return the size of "set" in dimension "dim".
853 * The size is taken to be the difference in values for that variable
854 * for fixed values of the other variables.
855 * In particular, the variable is first isolated from the other variables
856 * in the range of a map
858 * [i_0, ..., i_dim-1, i_dim+1, ...] -> [i_dim]
860 * and then duplicated
862 * [i_0, ..., i_dim-1, i_dim+1, ...] -> [[i_dim] -> [i_dim']]
864 * The shared variables are then projected out and the maximal value
865 * of i_dim' - i_dim is computed.
867 static __isl_give isl_val *compute_size(__isl_take isl_set *set, int dim)
869 isl_map *map;
870 isl_local_space *ls;
871 isl_aff *obj;
872 isl_val *v;
874 map = isl_set_project_onto_map(set, isl_dim_set, dim, 1);
875 map = isl_map_project_out(map, isl_dim_in, dim, 1);
876 map = isl_map_range_product(map, isl_map_copy(map));
877 map = isl_set_unwrap(isl_map_range(map));
878 set = isl_map_deltas(map);
879 ls = isl_local_space_from_space(isl_set_get_space(set));
880 obj = isl_aff_var_on_domain(ls, isl_dim_set, 0);
881 v = isl_set_max_val(set, obj);
882 isl_aff_free(obj);
883 isl_set_free(set);
885 return v;
888 /* Compute the size of the instance set "set" of "node", after compression,
889 * as well as bounds on the corresponding coefficients, if needed.
891 * The sizes are needed when the schedule_treat_coalescing option is set.
892 * The bounds are needed when the schedule_treat_coalescing option or
893 * the schedule_max_coefficient option is set.
895 * If the schedule_treat_coalescing option is not set, then at most
896 * the bounds need to be set and this is done in set_max_coefficient.
897 * Otherwise, compress the domain if needed, compute the size
898 * in each direction and store the results in node->size.
899 * Finally, set the bounds on the coefficients based on the sizes
900 * and the schedule_max_coefficient option in compute_max_coefficient.
902 static isl_stat compute_sizes_and_max(isl_ctx *ctx, struct isl_sched_node *node,
903 __isl_take isl_set *set)
905 int j, n;
906 isl_multi_val *mv;
908 if (!isl_options_get_schedule_treat_coalescing(ctx)) {
909 isl_set_free(set);
910 return set_max_coefficient(ctx, node);
913 if (node->compressed)
914 set = isl_set_preimage_multi_aff(set,
915 isl_multi_aff_copy(node->decompress));
916 mv = isl_multi_val_zero(isl_set_get_space(set));
917 n = isl_set_dim(set, isl_dim_set);
918 for (j = 0; j < n; ++j) {
919 isl_val *v;
921 v = compute_size(isl_set_copy(set), j);
922 mv = isl_multi_val_set_val(mv, j, v);
924 node->sizes = mv;
925 isl_set_free(set);
926 if (!node->sizes)
927 return isl_stat_error;
928 return compute_max_coefficient(ctx, node);
931 /* Add a new node to the graph representing the given instance set.
932 * "nvar" is the (possibly compressed) number of variables and
933 * may be smaller than then number of set variables in "set"
934 * if "compressed" is set.
935 * If "compressed" is set, then "hull" represents the constraints
936 * that were used to derive the compression, while "compress" and
937 * "decompress" map the original space to the compressed space and
938 * vice versa.
939 * If "compressed" is not set, then "hull", "compress" and "decompress"
940 * should be NULL.
942 * Compute the size of the instance set and bounds on the coefficients,
943 * if needed.
945 static isl_stat add_node(struct isl_sched_graph *graph,
946 __isl_take isl_set *set, int nvar, int compressed,
947 __isl_take isl_set *hull, __isl_take isl_multi_aff *compress,
948 __isl_take isl_multi_aff *decompress)
950 int nparam;
951 isl_ctx *ctx;
952 isl_mat *sched;
953 isl_space *space;
954 int *coincident;
955 struct isl_sched_node *node;
957 if (!set)
958 return isl_stat_error;
960 ctx = isl_set_get_ctx(set);
961 nparam = isl_set_dim(set, isl_dim_param);
962 if (!ctx->opt->schedule_parametric)
963 nparam = 0;
964 sched = isl_mat_alloc(ctx, 0, 1 + nparam + nvar);
965 node = &graph->node[graph->n];
966 graph->n++;
967 space = isl_set_get_space(set);
968 node->space = space;
969 node->nvar = nvar;
970 node->nparam = nparam;
971 node->sched = sched;
972 node->sched_map = NULL;
973 coincident = isl_calloc_array(ctx, int, graph->max_row);
974 node->coincident = coincident;
975 node->compressed = compressed;
976 node->hull = hull;
977 node->compress = compress;
978 node->decompress = decompress;
979 if (compute_sizes_and_max(ctx, node, set) < 0)
980 return isl_stat_error;
982 if (!space || !sched || (graph->max_row && !coincident))
983 return isl_stat_error;
984 if (compressed && (!hull || !compress || !decompress))
985 return isl_stat_error;
987 return isl_stat_ok;
990 /* Add a new node to the graph representing the given set.
992 * If any of the set variables is defined by an equality, then
993 * we perform variable compression such that we can perform
994 * the scheduling on the compressed domain.
996 static isl_stat extract_node(__isl_take isl_set *set, void *user)
998 int nvar;
999 int has_equality;
1000 isl_basic_set *hull;
1001 isl_set *hull_set;
1002 isl_morph *morph;
1003 isl_multi_aff *compress, *decompress;
1004 struct isl_sched_graph *graph = user;
1006 hull = isl_set_affine_hull(isl_set_copy(set));
1007 hull = isl_basic_set_remove_divs(hull);
1008 nvar = isl_set_dim(set, isl_dim_set);
1009 has_equality = has_any_defining_equality(hull);
1011 if (has_equality < 0)
1012 goto error;
1013 if (!has_equality) {
1014 isl_basic_set_free(hull);
1015 return add_node(graph, set, nvar, 0, NULL, NULL, NULL);
1018 morph = isl_basic_set_variable_compression(hull, isl_dim_set);
1019 nvar = isl_morph_ran_dim(morph, isl_dim_set);
1020 compress = isl_morph_get_var_multi_aff(morph);
1021 morph = isl_morph_inverse(morph);
1022 decompress = isl_morph_get_var_multi_aff(morph);
1023 isl_morph_free(morph);
1025 hull_set = isl_set_from_basic_set(hull);
1026 return add_node(graph, set, nvar, 1, hull_set, compress, decompress);
1027 error:
1028 isl_basic_set_free(hull);
1029 isl_set_free(set);
1030 return isl_stat_error;
1033 struct isl_extract_edge_data {
1034 enum isl_edge_type type;
1035 struct isl_sched_graph *graph;
1038 /* Merge edge2 into edge1, freeing the contents of edge2.
1039 * Return 0 on success and -1 on failure.
1041 * edge1 and edge2 are assumed to have the same value for the map field.
1043 static int merge_edge(struct isl_sched_edge *edge1,
1044 struct isl_sched_edge *edge2)
1046 edge1->types |= edge2->types;
1047 isl_map_free(edge2->map);
1049 if (is_condition(edge2)) {
1050 if (!edge1->tagged_condition)
1051 edge1->tagged_condition = edge2->tagged_condition;
1052 else
1053 edge1->tagged_condition =
1054 isl_union_map_union(edge1->tagged_condition,
1055 edge2->tagged_condition);
1058 if (is_conditional_validity(edge2)) {
1059 if (!edge1->tagged_validity)
1060 edge1->tagged_validity = edge2->tagged_validity;
1061 else
1062 edge1->tagged_validity =
1063 isl_union_map_union(edge1->tagged_validity,
1064 edge2->tagged_validity);
1067 if (is_condition(edge2) && !edge1->tagged_condition)
1068 return -1;
1069 if (is_conditional_validity(edge2) && !edge1->tagged_validity)
1070 return -1;
1072 return 0;
1075 /* Insert dummy tags in domain and range of "map".
1077 * In particular, if "map" is of the form
1079 * A -> B
1081 * then return
1083 * [A -> dummy_tag] -> [B -> dummy_tag]
1085 * where the dummy_tags are identical and equal to any dummy tags
1086 * introduced by any other call to this function.
1088 static __isl_give isl_map *insert_dummy_tags(__isl_take isl_map *map)
1090 static char dummy;
1091 isl_ctx *ctx;
1092 isl_id *id;
1093 isl_space *space;
1094 isl_set *domain, *range;
1096 ctx = isl_map_get_ctx(map);
1098 id = isl_id_alloc(ctx, NULL, &dummy);
1099 space = isl_space_params(isl_map_get_space(map));
1100 space = isl_space_set_from_params(space);
1101 space = isl_space_set_tuple_id(space, isl_dim_set, id);
1102 space = isl_space_map_from_set(space);
1104 domain = isl_map_wrap(map);
1105 range = isl_map_wrap(isl_map_universe(space));
1106 map = isl_map_from_domain_and_range(domain, range);
1107 map = isl_map_zip(map);
1109 return map;
1112 /* Given that at least one of "src" or "dst" is compressed, return
1113 * a map between the spaces of these nodes restricted to the affine
1114 * hull that was used in the compression.
1116 static __isl_give isl_map *extract_hull(struct isl_sched_node *src,
1117 struct isl_sched_node *dst)
1119 isl_set *dom, *ran;
1121 if (src->compressed)
1122 dom = isl_set_copy(src->hull);
1123 else
1124 dom = isl_set_universe(isl_space_copy(src->space));
1125 if (dst->compressed)
1126 ran = isl_set_copy(dst->hull);
1127 else
1128 ran = isl_set_universe(isl_space_copy(dst->space));
1130 return isl_map_from_domain_and_range(dom, ran);
1133 /* Intersect the domains of the nested relations in domain and range
1134 * of "tagged" with "map".
1136 static __isl_give isl_map *map_intersect_domains(__isl_take isl_map *tagged,
1137 __isl_keep isl_map *map)
1139 isl_set *set;
1141 tagged = isl_map_zip(tagged);
1142 set = isl_map_wrap(isl_map_copy(map));
1143 tagged = isl_map_intersect_domain(tagged, set);
1144 tagged = isl_map_zip(tagged);
1145 return tagged;
1148 /* Return a pointer to the node that lives in the domain space of "map"
1149 * or NULL if there is no such node.
1151 static struct isl_sched_node *find_domain_node(isl_ctx *ctx,
1152 struct isl_sched_graph *graph, __isl_keep isl_map *map)
1154 struct isl_sched_node *node;
1155 isl_space *space;
1157 space = isl_space_domain(isl_map_get_space(map));
1158 node = graph_find_node(ctx, graph, space);
1159 isl_space_free(space);
1161 return node;
1164 /* Return a pointer to the node that lives in the range space of "map"
1165 * or NULL if there is no such node.
1167 static struct isl_sched_node *find_range_node(isl_ctx *ctx,
1168 struct isl_sched_graph *graph, __isl_keep isl_map *map)
1170 struct isl_sched_node *node;
1171 isl_space *space;
1173 space = isl_space_range(isl_map_get_space(map));
1174 node = graph_find_node(ctx, graph, space);
1175 isl_space_free(space);
1177 return node;
1180 /* Refrain from adding a new edge based on "map".
1181 * Instead, just free the map.
1182 * "tagged" is either a copy of "map" with additional tags or NULL.
1184 static isl_stat skip_edge(__isl_take isl_map *map, __isl_take isl_map *tagged)
1186 isl_map_free(map);
1187 isl_map_free(tagged);
1189 return isl_stat_ok;
1192 /* Add a new edge to the graph based on the given map
1193 * and add it to data->graph->edge_table[data->type].
1194 * If a dependence relation of a given type happens to be identical
1195 * to one of the dependence relations of a type that was added before,
1196 * then we don't create a new edge, but instead mark the original edge
1197 * as also representing a dependence of the current type.
1199 * Edges of type isl_edge_condition or isl_edge_conditional_validity
1200 * may be specified as "tagged" dependence relations. That is, "map"
1201 * may contain elements (i -> a) -> (j -> b), where i -> j denotes
1202 * the dependence on iterations and a and b are tags.
1203 * edge->map is set to the relation containing the elements i -> j,
1204 * while edge->tagged_condition and edge->tagged_validity contain
1205 * the union of all the "map" relations
1206 * for which extract_edge is called that result in the same edge->map.
1208 * If the source or the destination node is compressed, then
1209 * intersect both "map" and "tagged" with the constraints that
1210 * were used to construct the compression.
1211 * This ensures that there are no schedule constraints defined
1212 * outside of these domains, while the scheduler no longer has
1213 * any control over those outside parts.
1215 static isl_stat extract_edge(__isl_take isl_map *map, void *user)
1217 isl_ctx *ctx = isl_map_get_ctx(map);
1218 struct isl_extract_edge_data *data = user;
1219 struct isl_sched_graph *graph = data->graph;
1220 struct isl_sched_node *src, *dst;
1221 struct isl_sched_edge *edge;
1222 isl_map *tagged = NULL;
1224 if (data->type == isl_edge_condition ||
1225 data->type == isl_edge_conditional_validity) {
1226 if (isl_map_can_zip(map)) {
1227 tagged = isl_map_copy(map);
1228 map = isl_set_unwrap(isl_map_domain(isl_map_zip(map)));
1229 } else {
1230 tagged = insert_dummy_tags(isl_map_copy(map));
1234 src = find_domain_node(ctx, graph, map);
1235 dst = find_range_node(ctx, graph, map);
1237 if (!src || !dst)
1238 return skip_edge(map, tagged);
1240 if (src->compressed || dst->compressed) {
1241 isl_map *hull;
1242 hull = extract_hull(src, dst);
1243 if (tagged)
1244 tagged = map_intersect_domains(tagged, hull);
1245 map = isl_map_intersect(map, hull);
1248 graph->edge[graph->n_edge].src = src;
1249 graph->edge[graph->n_edge].dst = dst;
1250 graph->edge[graph->n_edge].map = map;
1251 graph->edge[graph->n_edge].types = 0;
1252 graph->edge[graph->n_edge].tagged_condition = NULL;
1253 graph->edge[graph->n_edge].tagged_validity = NULL;
1254 set_type(&graph->edge[graph->n_edge], data->type);
1255 if (data->type == isl_edge_condition)
1256 graph->edge[graph->n_edge].tagged_condition =
1257 isl_union_map_from_map(tagged);
1258 if (data->type == isl_edge_conditional_validity)
1259 graph->edge[graph->n_edge].tagged_validity =
1260 isl_union_map_from_map(tagged);
1262 edge = graph_find_matching_edge(graph, &graph->edge[graph->n_edge]);
1263 if (!edge) {
1264 graph->n_edge++;
1265 return isl_stat_error;
1267 if (edge == &graph->edge[graph->n_edge])
1268 return graph_edge_table_add(ctx, graph, data->type,
1269 &graph->edge[graph->n_edge++]);
1271 if (merge_edge(edge, &graph->edge[graph->n_edge]) < 0)
1272 return -1;
1274 return graph_edge_table_add(ctx, graph, data->type, edge);
1277 /* Initialize the schedule graph "graph" from the schedule constraints "sc".
1279 * The context is included in the domain before the nodes of
1280 * the graphs are extracted in order to be able to exploit
1281 * any possible additional equalities.
1282 * Note that this intersection is only performed locally here.
1284 static isl_stat graph_init(struct isl_sched_graph *graph,
1285 __isl_keep isl_schedule_constraints *sc)
1287 isl_ctx *ctx;
1288 isl_union_set *domain;
1289 isl_union_map *c;
1290 struct isl_extract_edge_data data;
1291 enum isl_edge_type i;
1292 isl_stat r;
1294 if (!sc)
1295 return isl_stat_error;
1297 ctx = isl_schedule_constraints_get_ctx(sc);
1299 domain = isl_schedule_constraints_get_domain(sc);
1300 graph->n = isl_union_set_n_set(domain);
1301 isl_union_set_free(domain);
1303 if (graph_alloc(ctx, graph, graph->n,
1304 isl_schedule_constraints_n_map(sc)) < 0)
1305 return isl_stat_error;
1307 if (compute_max_row(graph, sc) < 0)
1308 return isl_stat_error;
1309 graph->root = 1;
1310 graph->n = 0;
1311 domain = isl_schedule_constraints_get_domain(sc);
1312 domain = isl_union_set_intersect_params(domain,
1313 isl_schedule_constraints_get_context(sc));
1314 r = isl_union_set_foreach_set(domain, &extract_node, graph);
1315 isl_union_set_free(domain);
1316 if (r < 0)
1317 return isl_stat_error;
1318 if (graph_init_table(ctx, graph) < 0)
1319 return isl_stat_error;
1320 for (i = isl_edge_first; i <= isl_edge_last; ++i) {
1321 c = isl_schedule_constraints_get(sc, i);
1322 graph->max_edge[i] = isl_union_map_n_map(c);
1323 isl_union_map_free(c);
1324 if (!c)
1325 return isl_stat_error;
1327 if (graph_init_edge_tables(ctx, graph) < 0)
1328 return isl_stat_error;
1329 graph->n_edge = 0;
1330 data.graph = graph;
1331 for (i = isl_edge_first; i <= isl_edge_last; ++i) {
1332 isl_stat r;
1334 data.type = i;
1335 c = isl_schedule_constraints_get(sc, i);
1336 r = isl_union_map_foreach_map(c, &extract_edge, &data);
1337 isl_union_map_free(c);
1338 if (r < 0)
1339 return isl_stat_error;
1342 return isl_stat_ok;
1345 /* Check whether there is any dependence from node[j] to node[i]
1346 * or from node[i] to node[j].
1348 static isl_bool node_follows_weak(int i, int j, void *user)
1350 isl_bool f;
1351 struct isl_sched_graph *graph = user;
1353 f = graph_has_any_edge(graph, &graph->node[j], &graph->node[i]);
1354 if (f < 0 || f)
1355 return f;
1356 return graph_has_any_edge(graph, &graph->node[i], &graph->node[j]);
1359 /* Check whether there is a (conditional) validity dependence from node[j]
1360 * to node[i], forcing node[i] to follow node[j].
1362 static isl_bool node_follows_strong(int i, int j, void *user)
1364 struct isl_sched_graph *graph = user;
1366 return graph_has_validity_edge(graph, &graph->node[j], &graph->node[i]);
1369 /* Use Tarjan's algorithm for computing the strongly connected components
1370 * in the dependence graph only considering those edges defined by "follows".
1372 static int detect_ccs(isl_ctx *ctx, struct isl_sched_graph *graph,
1373 isl_bool (*follows)(int i, int j, void *user))
1375 int i, n;
1376 struct isl_tarjan_graph *g = NULL;
1378 g = isl_tarjan_graph_init(ctx, graph->n, follows, graph);
1379 if (!g)
1380 return -1;
1382 graph->scc = 0;
1383 i = 0;
1384 n = graph->n;
1385 while (n) {
1386 while (g->order[i] != -1) {
1387 graph->node[g->order[i]].scc = graph->scc;
1388 --n;
1389 ++i;
1391 ++i;
1392 graph->scc++;
1395 isl_tarjan_graph_free(g);
1397 return 0;
1400 /* Apply Tarjan's algorithm to detect the strongly connected components
1401 * in the dependence graph.
1402 * Only consider the (conditional) validity dependences and clear "weak".
1404 static int detect_sccs(isl_ctx *ctx, struct isl_sched_graph *graph)
1406 graph->weak = 0;
1407 return detect_ccs(ctx, graph, &node_follows_strong);
1410 /* Apply Tarjan's algorithm to detect the (weakly) connected components
1411 * in the dependence graph.
1412 * Consider all dependences and set "weak".
1414 static int detect_wccs(isl_ctx *ctx, struct isl_sched_graph *graph)
1416 graph->weak = 1;
1417 return detect_ccs(ctx, graph, &node_follows_weak);
1420 static int cmp_scc(const void *a, const void *b, void *data)
1422 struct isl_sched_graph *graph = data;
1423 const int *i1 = a;
1424 const int *i2 = b;
1426 return graph->node[*i1].scc - graph->node[*i2].scc;
1429 /* Sort the elements of graph->sorted according to the corresponding SCCs.
1431 static int sort_sccs(struct isl_sched_graph *graph)
1433 return isl_sort(graph->sorted, graph->n, sizeof(int), &cmp_scc, graph);
1436 /* Given a dependence relation R from "node" to itself,
1437 * construct the set of coefficients of valid constraints for elements
1438 * in that dependence relation.
1439 * In particular, the result contains tuples of coefficients
1440 * c_0, c_n, c_x such that
1442 * c_0 + c_n n + c_x y - c_x x >= 0 for each (x,y) in R
1444 * or, equivalently,
1446 * c_0 + c_n n + c_x d >= 0 for each d in delta R = { y - x | (x,y) in R }
1448 * We choose here to compute the dual of delta R.
1449 * Alternatively, we could have computed the dual of R, resulting
1450 * in a set of tuples c_0, c_n, c_x, c_y, and then
1451 * plugged in (c_0, c_n, c_x, -c_x).
1453 * If "node" has been compressed, then the dependence relation
1454 * is also compressed before the set of coefficients is computed.
1456 static __isl_give isl_basic_set *intra_coefficients(
1457 struct isl_sched_graph *graph, struct isl_sched_node *node,
1458 __isl_take isl_map *map)
1460 isl_set *delta;
1461 isl_map *key;
1462 isl_basic_set *coef;
1463 isl_maybe_isl_basic_set m;
1465 m = isl_map_to_basic_set_try_get(graph->intra_hmap, map);
1466 if (m.valid < 0 || m.valid) {
1467 isl_map_free(map);
1468 return m.value;
1471 key = isl_map_copy(map);
1472 if (node->compressed) {
1473 map = isl_map_preimage_domain_multi_aff(map,
1474 isl_multi_aff_copy(node->decompress));
1475 map = isl_map_preimage_range_multi_aff(map,
1476 isl_multi_aff_copy(node->decompress));
1478 delta = isl_set_remove_divs(isl_map_deltas(map));
1479 coef = isl_set_coefficients(delta);
1480 graph->intra_hmap = isl_map_to_basic_set_set(graph->intra_hmap, key,
1481 isl_basic_set_copy(coef));
1483 return coef;
1486 /* Given a dependence relation R, construct the set of coefficients
1487 * of valid constraints for elements in that dependence relation.
1488 * In particular, the result contains tuples of coefficients
1489 * c_0, c_n, c_x, c_y such that
1491 * c_0 + c_n n + c_x x + c_y y >= 0 for each (x,y) in R
1493 * If the source or destination nodes of "edge" have been compressed,
1494 * then the dependence relation is also compressed before
1495 * the set of coefficients is computed.
1497 static __isl_give isl_basic_set *inter_coefficients(
1498 struct isl_sched_graph *graph, struct isl_sched_edge *edge,
1499 __isl_take isl_map *map)
1501 isl_set *set;
1502 isl_map *key;
1503 isl_basic_set *coef;
1504 isl_maybe_isl_basic_set m;
1506 m = isl_map_to_basic_set_try_get(graph->inter_hmap, map);
1507 if (m.valid < 0 || m.valid) {
1508 isl_map_free(map);
1509 return m.value;
1512 key = isl_map_copy(map);
1513 if (edge->src->compressed)
1514 map = isl_map_preimage_domain_multi_aff(map,
1515 isl_multi_aff_copy(edge->src->decompress));
1516 if (edge->dst->compressed)
1517 map = isl_map_preimage_range_multi_aff(map,
1518 isl_multi_aff_copy(edge->dst->decompress));
1519 set = isl_map_wrap(isl_map_remove_divs(map));
1520 coef = isl_set_coefficients(set);
1521 graph->inter_hmap = isl_map_to_basic_set_set(graph->inter_hmap, key,
1522 isl_basic_set_copy(coef));
1524 return coef;
1527 /* Return the position of the coefficients of the variables in
1528 * the coefficients constraints "coef".
1530 * The space of "coef" is of the form
1532 * { coefficients[[cst, params] -> S] }
1534 * Return the position of S.
1536 static int coef_var_offset(__isl_keep isl_basic_set *coef)
1538 int offset;
1539 isl_space *space;
1541 space = isl_space_unwrap(isl_basic_set_get_space(coef));
1542 offset = isl_space_dim(space, isl_dim_in);
1543 isl_space_free(space);
1545 return offset;
1548 /* Return the offset of the coefficients of the variables of "node"
1549 * within the (I)LP.
1551 * Within each node, the coefficients have the following order:
1552 * - c_i_0
1553 * - c_i_n (if parametric)
1554 * - positive and negative parts of c_i_x
1556 static int node_var_coef_offset(struct isl_sched_node *node)
1558 return node->start + 1 + node->nparam;
1561 /* Construct an isl_dim_map for mapping constraints on coefficients
1562 * for "node" to the corresponding positions in graph->lp.
1563 * "offset" is the offset of the coefficients for the variables
1564 * in the input constraints.
1565 * "s" is the sign of the mapping.
1567 * The input constraints are given in terms of the coefficients (c_0, c_n, c_x).
1568 * The mapping produced by this function essentially plugs in
1569 * (0, 0, c_i_x^+ - c_i_x^-) if s = 1 and
1570 * (0, 0, -c_i_x^+ + c_i_x^-) if s = -1.
1571 * In graph->lp, the c_i_x^- appear before their c_i_x^+ counterpart.
1573 * The caller can extend the mapping to also map the other coefficients
1574 * (and therefore not plug in 0).
1576 static __isl_give isl_dim_map *intra_dim_map(isl_ctx *ctx,
1577 struct isl_sched_graph *graph, struct isl_sched_node *node,
1578 int offset, int s)
1580 int pos;
1581 unsigned total;
1582 isl_dim_map *dim_map;
1584 if (!node || !graph->lp)
1585 return NULL;
1587 total = isl_basic_set_total_dim(graph->lp);
1588 pos = node_var_coef_offset(node);
1589 dim_map = isl_dim_map_alloc(ctx, total);
1590 isl_dim_map_range(dim_map, pos, 2, offset, 1, node->nvar, -s);
1591 isl_dim_map_range(dim_map, pos + 1, 2, offset, 1, node->nvar, s);
1593 return dim_map;
1596 /* Construct an isl_dim_map for mapping constraints on coefficients
1597 * for "src" (node i) and "dst" (node j) to the corresponding positions
1598 * in graph->lp.
1599 * "offset" is the offset of the coefficients for the variables of "src"
1600 * in the input constraints.
1601 * "s" is the sign of the mapping.
1603 * The input constraints are given in terms of the coefficients
1604 * (c_0, c_n, c_x, c_y).
1605 * The mapping produced by this function essentially plugs in
1606 * (c_j_0 - c_i_0, c_j_n - c_i_n,
1607 * c_j_x^+ - c_j_x^-, -(c_i_x^+ - c_i_x^-)) if s = 1 and
1608 * (-c_j_0 + c_i_0, -c_j_n + c_i_n,
1609 * - (c_j_x^+ - c_j_x^-), c_i_x^+ - c_i_x^-) if s = -1.
1610 * In graph->lp, the c_*^- appear before their c_*^+ counterpart.
1612 * The caller can further extend the mapping.
1614 static __isl_give isl_dim_map *inter_dim_map(isl_ctx *ctx,
1615 struct isl_sched_graph *graph, struct isl_sched_node *src,
1616 struct isl_sched_node *dst, int offset, int s)
1618 int pos;
1619 unsigned total;
1620 isl_dim_map *dim_map;
1622 if (!src || !dst || !graph->lp)
1623 return NULL;
1625 total = isl_basic_set_total_dim(graph->lp);
1626 dim_map = isl_dim_map_alloc(ctx, total);
1628 isl_dim_map_range(dim_map, dst->start, 0, 0, 0, 1, s);
1629 isl_dim_map_range(dim_map, dst->start + 1, 1, 1, 1, dst->nparam, s);
1630 pos = node_var_coef_offset(dst);
1631 isl_dim_map_range(dim_map, pos, 2, offset + src->nvar, 1,
1632 dst->nvar, -s);
1633 isl_dim_map_range(dim_map, pos + 1, 2, offset + src->nvar, 1,
1634 dst->nvar, s);
1636 isl_dim_map_range(dim_map, src->start, 0, 0, 0, 1, -s);
1637 isl_dim_map_range(dim_map, src->start + 1, 1, 1, 1, src->nparam, -s);
1638 pos = node_var_coef_offset(src);
1639 isl_dim_map_range(dim_map, pos, 2, offset, 1, src->nvar, s);
1640 isl_dim_map_range(dim_map, pos + 1, 2, offset, 1, src->nvar, -s);
1642 return dim_map;
1645 /* Add constraints to graph->lp that force validity for the given
1646 * dependence from a node i to itself.
1647 * That is, add constraints that enforce
1649 * (c_i_0 + c_i_n n + c_i_x y) - (c_i_0 + c_i_n n + c_i_x x)
1650 * = c_i_x (y - x) >= 0
1652 * for each (x,y) in R.
1653 * We obtain general constraints on coefficients (c_0, c_n, c_x)
1654 * of valid constraints for (y - x) and then plug in (0, 0, c_i_x^+ - c_i_x^-),
1655 * where c_i_x = c_i_x^+ - c_i_x^-, with c_i_x^+ and c_i_x^- non-negative.
1656 * In graph->lp, the c_i_x^- appear before their c_i_x^+ counterpart.
1658 * Actually, we do not construct constraints for the c_i_x themselves,
1659 * but for the coefficients of c_i_x written as a linear combination
1660 * of the columns in node->cmap.
1662 static isl_stat add_intra_validity_constraints(struct isl_sched_graph *graph,
1663 struct isl_sched_edge *edge)
1665 int offset;
1666 isl_map *map = isl_map_copy(edge->map);
1667 isl_ctx *ctx = isl_map_get_ctx(map);
1668 isl_dim_map *dim_map;
1669 isl_basic_set *coef;
1670 struct isl_sched_node *node = edge->src;
1672 coef = intra_coefficients(graph, node, map);
1674 offset = coef_var_offset(coef);
1676 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
1677 offset, isl_mat_copy(node->cmap));
1678 if (!coef)
1679 return isl_stat_error;
1681 dim_map = intra_dim_map(ctx, graph, node, offset, 1);
1682 graph->lp = isl_basic_set_extend_constraints(graph->lp,
1683 coef->n_eq, coef->n_ineq);
1684 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
1685 coef, dim_map);
1687 return isl_stat_ok;
1690 /* Add constraints to graph->lp that force validity for the given
1691 * dependence from node i to node j.
1692 * That is, add constraints that enforce
1694 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x) >= 0
1696 * for each (x,y) in R.
1697 * We obtain general constraints on coefficients (c_0, c_n, c_x, c_y)
1698 * of valid constraints for R and then plug in
1699 * (c_j_0 - c_i_0, c_j_n - c_i_n, c_j_x^+ - c_j_x^- - (c_i_x^+ - c_i_x^-)),
1700 * where c_* = c_*^+ - c_*^-, with c_*^+ and c_*^- non-negative.
1701 * In graph->lp, the c_*^- appear before their c_*^+ counterpart.
1703 * Actually, we do not construct constraints for the c_*_x themselves,
1704 * but for the coefficients of c_*_x written as a linear combination
1705 * of the columns in node->cmap.
1707 static isl_stat add_inter_validity_constraints(struct isl_sched_graph *graph,
1708 struct isl_sched_edge *edge)
1710 int offset;
1711 isl_map *map;
1712 isl_ctx *ctx;
1713 isl_dim_map *dim_map;
1714 isl_basic_set *coef;
1715 struct isl_sched_node *src = edge->src;
1716 struct isl_sched_node *dst = edge->dst;
1718 if (!graph->lp)
1719 return isl_stat_error;
1721 map = isl_map_copy(edge->map);
1722 ctx = isl_map_get_ctx(map);
1723 coef = inter_coefficients(graph, edge, map);
1725 offset = coef_var_offset(coef);
1727 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
1728 offset, isl_mat_copy(src->cmap));
1729 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
1730 offset + src->nvar, isl_mat_copy(dst->cmap));
1731 if (!coef)
1732 return isl_stat_error;
1734 dim_map = inter_dim_map(ctx, graph, src, dst, offset, 1);
1736 edge->start = graph->lp->n_ineq;
1737 graph->lp = isl_basic_set_extend_constraints(graph->lp,
1738 coef->n_eq, coef->n_ineq);
1739 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
1740 coef, dim_map);
1741 if (!graph->lp)
1742 return isl_stat_error;
1743 edge->end = graph->lp->n_ineq;
1745 return isl_stat_ok;
1748 /* Add constraints to graph->lp that bound the dependence distance for the given
1749 * dependence from a node i to itself.
1750 * If s = 1, we add the constraint
1752 * c_i_x (y - x) <= m_0 + m_n n
1754 * or
1756 * -c_i_x (y - x) + m_0 + m_n n >= 0
1758 * for each (x,y) in R.
1759 * If s = -1, we add the constraint
1761 * -c_i_x (y - x) <= m_0 + m_n n
1763 * or
1765 * c_i_x (y - x) + m_0 + m_n n >= 0
1767 * for each (x,y) in R.
1768 * We obtain general constraints on coefficients (c_0, c_n, c_x)
1769 * of valid constraints for (y - x) and then plug in (m_0, m_n, -s * c_i_x),
1770 * with each coefficient (except m_0) represented as a pair of non-negative
1771 * coefficients.
1773 * Actually, we do not construct constraints for the c_i_x themselves,
1774 * but for the coefficients of c_i_x written as a linear combination
1775 * of the columns in node->cmap.
1778 * If "local" is set, then we add constraints
1780 * c_i_x (y - x) <= 0
1782 * or
1784 * -c_i_x (y - x) <= 0
1786 * instead, forcing the dependence distance to be (less than or) equal to 0.
1787 * That is, we plug in (0, 0, -s * c_i_x),
1788 * Note that dependences marked local are treated as validity constraints
1789 * by add_all_validity_constraints and therefore also have
1790 * their distances bounded by 0 from below.
1792 static isl_stat add_intra_proximity_constraints(struct isl_sched_graph *graph,
1793 struct isl_sched_edge *edge, int s, int local)
1795 int offset;
1796 unsigned nparam;
1797 isl_map *map = isl_map_copy(edge->map);
1798 isl_ctx *ctx = isl_map_get_ctx(map);
1799 isl_dim_map *dim_map;
1800 isl_basic_set *coef;
1801 struct isl_sched_node *node = edge->src;
1803 coef = intra_coefficients(graph, node, map);
1805 offset = coef_var_offset(coef);
1807 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
1808 offset, isl_mat_copy(node->cmap));
1809 if (!coef)
1810 return isl_stat_error;
1812 nparam = isl_space_dim(node->space, isl_dim_param);
1813 dim_map = intra_dim_map(ctx, graph, node, offset, -s);
1815 if (!local) {
1816 isl_dim_map_range(dim_map, 1, 0, 0, 0, 1, 1);
1817 isl_dim_map_range(dim_map, 4, 2, 1, 1, nparam, -1);
1818 isl_dim_map_range(dim_map, 5, 2, 1, 1, nparam, 1);
1820 graph->lp = isl_basic_set_extend_constraints(graph->lp,
1821 coef->n_eq, coef->n_ineq);
1822 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
1823 coef, dim_map);
1825 return isl_stat_ok;
1828 /* Add constraints to graph->lp that bound the dependence distance for the given
1829 * dependence from node i to node j.
1830 * If s = 1, we add the constraint
1832 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x)
1833 * <= m_0 + m_n n
1835 * or
1837 * -(c_j_0 + c_j_n n + c_j_x y) + (c_i_0 + c_i_n n + c_i_x x) +
1838 * m_0 + m_n n >= 0
1840 * for each (x,y) in R.
1841 * If s = -1, we add the constraint
1843 * -((c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x))
1844 * <= m_0 + m_n n
1846 * or
1848 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x) +
1849 * m_0 + m_n n >= 0
1851 * for each (x,y) in R.
1852 * We obtain general constraints on coefficients (c_0, c_n, c_x, c_y)
1853 * of valid constraints for R and then plug in
1854 * (m_0 - s*c_j_0 + s*c_i_0, m_n - s*c_j_n + s*c_i_n,
1855 * -s*c_j_x+s*c_i_x)
1856 * with each coefficient (except m_0, c_*_0 and c_*_n)
1857 * represented as a pair of non-negative coefficients.
1859 * Actually, we do not construct constraints for the c_*_x themselves,
1860 * but for the coefficients of c_*_x written as a linear combination
1861 * of the columns in node->cmap.
1864 * If "local" is set, then we add constraints
1866 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x) <= 0
1868 * or
1870 * -((c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x)) <= 0
1872 * instead, forcing the dependence distance to be (less than or) equal to 0.
1873 * That is, we plug in
1874 * (-s*c_j_0 + s*c_i_0, -s*c_j_n + s*c_i_n, -s*c_j_x+s*c_i_x).
1875 * Note that dependences marked local are treated as validity constraints
1876 * by add_all_validity_constraints and therefore also have
1877 * their distances bounded by 0 from below.
1879 static isl_stat add_inter_proximity_constraints(struct isl_sched_graph *graph,
1880 struct isl_sched_edge *edge, int s, int local)
1882 int offset;
1883 unsigned nparam;
1884 isl_map *map = isl_map_copy(edge->map);
1885 isl_ctx *ctx = isl_map_get_ctx(map);
1886 isl_dim_map *dim_map;
1887 isl_basic_set *coef;
1888 struct isl_sched_node *src = edge->src;
1889 struct isl_sched_node *dst = edge->dst;
1891 coef = inter_coefficients(graph, edge, map);
1893 offset = coef_var_offset(coef);
1895 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
1896 offset, isl_mat_copy(src->cmap));
1897 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
1898 offset + src->nvar, isl_mat_copy(dst->cmap));
1899 if (!coef)
1900 return isl_stat_error;
1902 nparam = isl_space_dim(src->space, isl_dim_param);
1903 dim_map = inter_dim_map(ctx, graph, src, dst, offset, -s);
1905 if (!local) {
1906 isl_dim_map_range(dim_map, 1, 0, 0, 0, 1, 1);
1907 isl_dim_map_range(dim_map, 4, 2, 1, 1, nparam, -1);
1908 isl_dim_map_range(dim_map, 5, 2, 1, 1, nparam, 1);
1911 graph->lp = isl_basic_set_extend_constraints(graph->lp,
1912 coef->n_eq, coef->n_ineq);
1913 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
1914 coef, dim_map);
1916 return isl_stat_ok;
1919 /* Add all validity constraints to graph->lp.
1921 * An edge that is forced to be local needs to have its dependence
1922 * distances equal to zero. We take care of bounding them by 0 from below
1923 * here. add_all_proximity_constraints takes care of bounding them by 0
1924 * from above.
1926 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
1927 * Otherwise, we ignore them.
1929 static int add_all_validity_constraints(struct isl_sched_graph *graph,
1930 int use_coincidence)
1932 int i;
1934 for (i = 0; i < graph->n_edge; ++i) {
1935 struct isl_sched_edge *edge= &graph->edge[i];
1936 int local;
1938 local = is_local(edge) ||
1939 (is_coincidence(edge) && use_coincidence);
1940 if (!is_validity(edge) && !local)
1941 continue;
1942 if (edge->src != edge->dst)
1943 continue;
1944 if (add_intra_validity_constraints(graph, edge) < 0)
1945 return -1;
1948 for (i = 0; i < graph->n_edge; ++i) {
1949 struct isl_sched_edge *edge = &graph->edge[i];
1950 int local;
1952 local = is_local(edge) ||
1953 (is_coincidence(edge) && use_coincidence);
1954 if (!is_validity(edge) && !local)
1955 continue;
1956 if (edge->src == edge->dst)
1957 continue;
1958 if (add_inter_validity_constraints(graph, edge) < 0)
1959 return -1;
1962 return 0;
1965 /* Add constraints to graph->lp that bound the dependence distance
1966 * for all dependence relations.
1967 * If a given proximity dependence is identical to a validity
1968 * dependence, then the dependence distance is already bounded
1969 * from below (by zero), so we only need to bound the distance
1970 * from above. (This includes the case of "local" dependences
1971 * which are treated as validity dependence by add_all_validity_constraints.)
1972 * Otherwise, we need to bound the distance both from above and from below.
1974 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
1975 * Otherwise, we ignore them.
1977 static int add_all_proximity_constraints(struct isl_sched_graph *graph,
1978 int use_coincidence)
1980 int i;
1982 for (i = 0; i < graph->n_edge; ++i) {
1983 struct isl_sched_edge *edge= &graph->edge[i];
1984 int local;
1986 local = is_local(edge) ||
1987 (is_coincidence(edge) && use_coincidence);
1988 if (!is_proximity(edge) && !local)
1989 continue;
1990 if (edge->src == edge->dst &&
1991 add_intra_proximity_constraints(graph, edge, 1, local) < 0)
1992 return -1;
1993 if (edge->src != edge->dst &&
1994 add_inter_proximity_constraints(graph, edge, 1, local) < 0)
1995 return -1;
1996 if (is_validity(edge) || local)
1997 continue;
1998 if (edge->src == edge->dst &&
1999 add_intra_proximity_constraints(graph, edge, -1, 0) < 0)
2000 return -1;
2001 if (edge->src != edge->dst &&
2002 add_inter_proximity_constraints(graph, edge, -1, 0) < 0)
2003 return -1;
2006 return 0;
2009 /* Compute a basis for the rows in the linear part of the schedule
2010 * and extend this basis to a full basis. The remaining rows
2011 * can then be used to force linear independence from the rows
2012 * in the schedule.
2014 * In particular, given the schedule rows S, we compute
2016 * S = H Q
2017 * S U = H
2019 * with H the Hermite normal form of S. That is, all but the
2020 * first rank columns of H are zero and so each row in S is
2021 * a linear combination of the first rank rows of Q.
2022 * The matrix Q is then transposed because we will write the
2023 * coefficients of the next schedule row as a column vector s
2024 * and express this s as a linear combination s = Q c of the
2025 * computed basis.
2026 * Similarly, the matrix U is transposed such that we can
2027 * compute the coefficients c = U s from a schedule row s.
2029 static int node_update_cmap(struct isl_sched_node *node)
2031 isl_mat *H, *U, *Q;
2032 int n_row = isl_mat_rows(node->sched);
2034 H = isl_mat_sub_alloc(node->sched, 0, n_row,
2035 1 + node->nparam, node->nvar);
2037 H = isl_mat_left_hermite(H, 0, &U, &Q);
2038 isl_mat_free(node->cmap);
2039 isl_mat_free(node->cinv);
2040 isl_mat_free(node->ctrans);
2041 node->ctrans = isl_mat_copy(Q);
2042 node->cmap = isl_mat_transpose(Q);
2043 node->cinv = isl_mat_transpose(U);
2044 node->rank = isl_mat_initial_non_zero_cols(H);
2045 isl_mat_free(H);
2047 if (!node->cmap || !node->cinv || !node->ctrans || node->rank < 0)
2048 return -1;
2049 return 0;
2052 /* Is "edge" marked as a validity or a conditional validity edge?
2054 static int is_any_validity(struct isl_sched_edge *edge)
2056 return is_validity(edge) || is_conditional_validity(edge);
2059 /* How many times should we count the constraints in "edge"?
2061 * If carry is set, then we are counting the number of
2062 * (validity or conditional validity) constraints that will be added
2063 * in setup_carry_lp and we count each edge exactly once.
2065 * Otherwise, we count as follows
2066 * validity -> 1 (>= 0)
2067 * validity+proximity -> 2 (>= 0 and upper bound)
2068 * proximity -> 2 (lower and upper bound)
2069 * local(+any) -> 2 (>= 0 and <= 0)
2071 * If an edge is only marked conditional_validity then it counts
2072 * as zero since it is only checked afterwards.
2074 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
2075 * Otherwise, we ignore them.
2077 static int edge_multiplicity(struct isl_sched_edge *edge, int carry,
2078 int use_coincidence)
2080 if (carry)
2081 return 1;
2082 if (is_proximity(edge) || is_local(edge))
2083 return 2;
2084 if (use_coincidence && is_coincidence(edge))
2085 return 2;
2086 if (is_validity(edge))
2087 return 1;
2088 return 0;
2091 /* Count the number of equality and inequality constraints
2092 * that will be added for the given map.
2094 * "use_coincidence" is set if we should take into account coincidence edges.
2096 static int count_map_constraints(struct isl_sched_graph *graph,
2097 struct isl_sched_edge *edge, __isl_take isl_map *map,
2098 int *n_eq, int *n_ineq, int carry, int use_coincidence)
2100 isl_basic_set *coef;
2101 int f = edge_multiplicity(edge, carry, use_coincidence);
2103 if (f == 0) {
2104 isl_map_free(map);
2105 return 0;
2108 if (edge->src == edge->dst)
2109 coef = intra_coefficients(graph, edge->src, map);
2110 else
2111 coef = inter_coefficients(graph, edge, map);
2112 if (!coef)
2113 return -1;
2114 *n_eq += f * coef->n_eq;
2115 *n_ineq += f * coef->n_ineq;
2116 isl_basic_set_free(coef);
2118 return 0;
2121 /* Count the number of equality and inequality constraints
2122 * that will be added to the main lp problem.
2123 * We count as follows
2124 * validity -> 1 (>= 0)
2125 * validity+proximity -> 2 (>= 0 and upper bound)
2126 * proximity -> 2 (lower and upper bound)
2127 * local(+any) -> 2 (>= 0 and <= 0)
2129 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
2130 * Otherwise, we ignore them.
2132 static int count_constraints(struct isl_sched_graph *graph,
2133 int *n_eq, int *n_ineq, int use_coincidence)
2135 int i;
2137 *n_eq = *n_ineq = 0;
2138 for (i = 0; i < graph->n_edge; ++i) {
2139 struct isl_sched_edge *edge= &graph->edge[i];
2140 isl_map *map = isl_map_copy(edge->map);
2142 if (count_map_constraints(graph, edge, map, n_eq, n_ineq,
2143 0, use_coincidence) < 0)
2144 return -1;
2147 return 0;
2150 /* Count the number of constraints that will be added by
2151 * add_bound_constant_constraints to bound the values of the constant terms
2152 * and increment *n_eq and *n_ineq accordingly.
2154 * In practice, add_bound_constant_constraints only adds inequalities.
2156 static isl_stat count_bound_constant_constraints(isl_ctx *ctx,
2157 struct isl_sched_graph *graph, int *n_eq, int *n_ineq)
2159 if (isl_options_get_schedule_max_constant_term(ctx) == -1)
2160 return isl_stat_ok;
2162 *n_ineq += graph->n;
2164 return isl_stat_ok;
2167 /* Add constraints to bound the values of the constant terms in the schedule,
2168 * if requested by the user.
2170 * The maximal value of the constant terms is defined by the option
2171 * "schedule_max_constant_term".
2173 * Within each node, the coefficients have the following order:
2174 * - c_i_0
2175 * - c_i_n (if parametric)
2176 * - positive and negative parts of c_i_x
2178 static isl_stat add_bound_constant_constraints(isl_ctx *ctx,
2179 struct isl_sched_graph *graph)
2181 int i, k;
2182 int max;
2183 int total;
2185 max = isl_options_get_schedule_max_constant_term(ctx);
2186 if (max == -1)
2187 return isl_stat_ok;
2189 total = isl_basic_set_dim(graph->lp, isl_dim_set);
2191 for (i = 0; i < graph->n; ++i) {
2192 struct isl_sched_node *node = &graph->node[i];
2193 k = isl_basic_set_alloc_inequality(graph->lp);
2194 if (k < 0)
2195 return isl_stat_error;
2196 isl_seq_clr(graph->lp->ineq[k], 1 + total);
2197 isl_int_set_si(graph->lp->ineq[k][1 + node->start], -1);
2198 isl_int_set_si(graph->lp->ineq[k][0], max);
2201 return isl_stat_ok;
2204 /* Count the number of constraints that will be added by
2205 * add_bound_coefficient_constraints and increment *n_eq and *n_ineq
2206 * accordingly.
2208 * In practice, add_bound_coefficient_constraints only adds inequalities.
2210 static int count_bound_coefficient_constraints(isl_ctx *ctx,
2211 struct isl_sched_graph *graph, int *n_eq, int *n_ineq)
2213 int i;
2215 if (isl_options_get_schedule_max_coefficient(ctx) == -1 &&
2216 !isl_options_get_schedule_treat_coalescing(ctx))
2217 return 0;
2219 for (i = 0; i < graph->n; ++i)
2220 *n_ineq += graph->node[i].nparam + 2 * graph->node[i].nvar;
2222 return 0;
2225 /* Add constraints to graph->lp that bound the values of
2226 * the parameter schedule coefficients of "node" to "max" and
2227 * the variable schedule coefficients to the corresponding entry
2228 * in node->max.
2229 * In either case, a negative value means that no bound needs to be imposed.
2231 * For parameter coefficients, this amounts to adding a constraint
2233 * c_n <= max
2235 * i.e.,
2237 * -c_n + max >= 0
2239 * The variables coefficients are, however, not represented directly.
2240 * Instead, the variables coefficients c_x are written as a linear
2241 * combination c_x = cmap c_z of some other coefficients c_z,
2242 * which are in turn encoded as c_z = c_z^+ - c_z^-.
2243 * Let a_j be the elements of row i of node->cmap, then
2245 * -max_i <= c_x_i <= max_i
2247 * is encoded as
2249 * -max_i <= \sum_j a_j (c_z_j^+ - c_z_j^-) <= max_i
2251 * or
2253 * -\sum_j a_j (c_z_j^+ - c_z_j^-) + max_i >= 0
2254 * \sum_j a_j (c_z_j^+ - c_z_j^-) + max_i >= 0
2256 static isl_stat node_add_coefficient_constraints(isl_ctx *ctx,
2257 struct isl_sched_graph *graph, struct isl_sched_node *node, int max)
2259 int i, j, k;
2260 int total;
2261 isl_vec *ineq;
2263 total = isl_basic_set_dim(graph->lp, isl_dim_set);
2265 for (j = 0; j < node->nparam; ++j) {
2266 int dim;
2268 if (max < 0)
2269 continue;
2271 k = isl_basic_set_alloc_inequality(graph->lp);
2272 if (k < 0)
2273 return isl_stat_error;
2274 dim = 1 + node->start + 1 + j;
2275 isl_seq_clr(graph->lp->ineq[k], 1 + total);
2276 isl_int_set_si(graph->lp->ineq[k][dim], -1);
2277 isl_int_set_si(graph->lp->ineq[k][0], max);
2280 ineq = isl_vec_alloc(ctx, 1 + total);
2281 ineq = isl_vec_clr(ineq);
2282 if (!ineq)
2283 return isl_stat_error;
2284 for (i = 0; i < node->nvar; ++i) {
2285 int pos = 1 + node_var_coef_offset(node);
2287 if (isl_int_is_neg(node->max->el[i]))
2288 continue;
2290 for (j = 0; j < node->nvar; ++j) {
2291 isl_int_set(ineq->el[pos + 2 * j],
2292 node->cmap->row[i][j]);
2293 isl_int_neg(ineq->el[pos + 2 * j + 1],
2294 node->cmap->row[i][j]);
2296 isl_int_set(ineq->el[0], node->max->el[i]);
2298 k = isl_basic_set_alloc_inequality(graph->lp);
2299 if (k < 0)
2300 goto error;
2301 isl_seq_cpy(graph->lp->ineq[k], ineq->el, 1 + total);
2303 isl_seq_neg(ineq->el + pos, ineq->el + pos, 2 * node->nvar);
2304 k = isl_basic_set_alloc_inequality(graph->lp);
2305 if (k < 0)
2306 goto error;
2307 isl_seq_cpy(graph->lp->ineq[k], ineq->el, 1 + total);
2309 isl_vec_free(ineq);
2311 return isl_stat_ok;
2312 error:
2313 isl_vec_free(ineq);
2314 return isl_stat_error;
2317 /* Add constraints that bound the values of the variable and parameter
2318 * coefficients of the schedule.
2320 * The maximal value of the coefficients is defined by the option
2321 * 'schedule_max_coefficient' and the entries in node->max.
2322 * These latter entries are only set if either the schedule_max_coefficient
2323 * option or the schedule_treat_coalescing option is set.
2325 static isl_stat add_bound_coefficient_constraints(isl_ctx *ctx,
2326 struct isl_sched_graph *graph)
2328 int i;
2329 int max;
2331 max = isl_options_get_schedule_max_coefficient(ctx);
2333 if (max == -1 && !isl_options_get_schedule_treat_coalescing(ctx))
2334 return isl_stat_ok;
2336 for (i = 0; i < graph->n; ++i) {
2337 struct isl_sched_node *node = &graph->node[i];
2339 if (node_add_coefficient_constraints(ctx, graph, node, max) < 0)
2340 return isl_stat_error;
2343 return isl_stat_ok;
2346 /* Add a constraint to graph->lp that equates the value at position
2347 * "sum_pos" to the sum of the "n" values starting at "first".
2349 static isl_stat add_sum_constraint(struct isl_sched_graph *graph,
2350 int sum_pos, int first, int n)
2352 int i, k;
2353 int total;
2355 total = isl_basic_set_dim(graph->lp, isl_dim_set);
2357 k = isl_basic_set_alloc_equality(graph->lp);
2358 if (k < 0)
2359 return isl_stat_error;
2360 isl_seq_clr(graph->lp->eq[k], 1 + total);
2361 isl_int_set_si(graph->lp->eq[k][1 + sum_pos], -1);
2362 for (i = 0; i < n; ++i)
2363 isl_int_set_si(graph->lp->eq[k][1 + first + i], 1);
2365 return isl_stat_ok;
2368 /* Add a constraint to graph->lp that equates the value at position
2369 * "sum_pos" to the sum of the parameter coefficients of all nodes.
2371 * Within each node, the coefficients have the following order:
2372 * - c_i_0
2373 * - c_i_n (if parametric)
2374 * - positive and negative parts of c_i_x
2376 static isl_stat add_param_sum_constraint(struct isl_sched_graph *graph,
2377 int sum_pos)
2379 int i, j, k;
2380 int total;
2382 total = isl_basic_set_dim(graph->lp, isl_dim_set);
2384 k = isl_basic_set_alloc_equality(graph->lp);
2385 if (k < 0)
2386 return isl_stat_error;
2387 isl_seq_clr(graph->lp->eq[k], 1 + total);
2388 isl_int_set_si(graph->lp->eq[k][1 + sum_pos], -1);
2389 for (i = 0; i < graph->n; ++i) {
2390 int pos = 1 + graph->node[i].start + 1;
2392 for (j = 0; j < graph->node[i].nparam; ++j)
2393 isl_int_set_si(graph->lp->eq[k][pos + j], 1);
2396 return isl_stat_ok;
2399 /* Add a constraint to graph->lp that equates the value at position
2400 * "sum_pos" to the sum of the variable coefficients of all nodes.
2402 * Within each node, the coefficients have the following order:
2403 * - c_i_0
2404 * - c_i_n (if parametric)
2405 * - positive and negative parts of c_i_x
2407 static isl_stat add_var_sum_constraint(struct isl_sched_graph *graph,
2408 int sum_pos)
2410 int i, j, k;
2411 int total;
2413 total = isl_basic_set_dim(graph->lp, isl_dim_set);
2415 k = isl_basic_set_alloc_equality(graph->lp);
2416 if (k < 0)
2417 return isl_stat_error;
2418 isl_seq_clr(graph->lp->eq[k], 1 + total);
2419 isl_int_set_si(graph->lp->eq[k][1 + sum_pos], -1);
2420 for (i = 0; i < graph->n; ++i) {
2421 struct isl_sched_node *node = &graph->node[i];
2422 int pos = 1 + node_var_coef_offset(node);
2424 for (j = 0; j < 2 * node->nvar; ++j)
2425 isl_int_set_si(graph->lp->eq[k][pos + j], 1);
2428 return isl_stat_ok;
2431 /* Construct an ILP problem for finding schedule coefficients
2432 * that result in non-negative, but small dependence distances
2433 * over all dependences.
2434 * In particular, the dependence distances over proximity edges
2435 * are bounded by m_0 + m_n n and we compute schedule coefficients
2436 * with small values (preferably zero) of m_n and m_0.
2438 * All variables of the ILP are non-negative. The actual coefficients
2439 * may be negative, so each coefficient is represented as the difference
2440 * of two non-negative variables. The negative part always appears
2441 * immediately before the positive part.
2442 * Other than that, the variables have the following order
2444 * - sum of positive and negative parts of m_n coefficients
2445 * - m_0
2446 * - sum of all c_n coefficients
2447 * (unconstrained when computing non-parametric schedules)
2448 * - sum of positive and negative parts of all c_x coefficients
2449 * - positive and negative parts of m_n coefficients
2450 * - for each node
2451 * - c_i_0
2452 * - c_i_n (if parametric)
2453 * - positive and negative parts of c_i_x
2455 * The c_i_x are not represented directly, but through the columns of
2456 * node->cmap. That is, the computed values are for variable t_i_x
2457 * such that c_i_x = Q t_i_x with Q equal to node->cmap.
2459 * The constraints are those from the edges plus two or three equalities
2460 * to express the sums.
2462 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
2463 * Otherwise, we ignore them.
2465 static isl_stat setup_lp(isl_ctx *ctx, struct isl_sched_graph *graph,
2466 int use_coincidence)
2468 int i;
2469 unsigned nparam;
2470 unsigned total;
2471 isl_space *space;
2472 int parametric;
2473 int param_pos;
2474 int n_eq, n_ineq;
2476 parametric = ctx->opt->schedule_parametric;
2477 nparam = isl_space_dim(graph->node[0].space, isl_dim_param);
2478 param_pos = 4;
2479 total = param_pos + 2 * nparam;
2480 for (i = 0; i < graph->n; ++i) {
2481 struct isl_sched_node *node = &graph->node[graph->sorted[i]];
2482 if (node_update_cmap(node) < 0)
2483 return isl_stat_error;
2484 node->start = total;
2485 total += 1 + node->nparam + 2 * node->nvar;
2488 if (count_constraints(graph, &n_eq, &n_ineq, use_coincidence) < 0)
2489 return isl_stat_error;
2490 if (count_bound_constant_constraints(ctx, graph, &n_eq, &n_ineq) < 0)
2491 return isl_stat_error;
2492 if (count_bound_coefficient_constraints(ctx, graph, &n_eq, &n_ineq) < 0)
2493 return isl_stat_error;
2495 space = isl_space_set_alloc(ctx, 0, total);
2496 isl_basic_set_free(graph->lp);
2497 n_eq += 2 + parametric;
2499 graph->lp = isl_basic_set_alloc_space(space, 0, n_eq, n_ineq);
2501 if (add_sum_constraint(graph, 0, param_pos, 2 * nparam) < 0)
2502 return isl_stat_error;
2503 if (parametric && add_param_sum_constraint(graph, 2) < 0)
2504 return isl_stat_error;
2505 if (add_var_sum_constraint(graph, 3) < 0)
2506 return isl_stat_error;
2507 if (add_bound_constant_constraints(ctx, graph) < 0)
2508 return isl_stat_error;
2509 if (add_bound_coefficient_constraints(ctx, graph) < 0)
2510 return isl_stat_error;
2511 if (add_all_validity_constraints(graph, use_coincidence) < 0)
2512 return isl_stat_error;
2513 if (add_all_proximity_constraints(graph, use_coincidence) < 0)
2514 return isl_stat_error;
2516 return isl_stat_ok;
2519 /* Analyze the conflicting constraint found by
2520 * isl_tab_basic_set_non_trivial_lexmin. If it corresponds to the validity
2521 * constraint of one of the edges between distinct nodes, living, moreover
2522 * in distinct SCCs, then record the source and sink SCC as this may
2523 * be a good place to cut between SCCs.
2525 static int check_conflict(int con, void *user)
2527 int i;
2528 struct isl_sched_graph *graph = user;
2530 if (graph->src_scc >= 0)
2531 return 0;
2533 con -= graph->lp->n_eq;
2535 if (con >= graph->lp->n_ineq)
2536 return 0;
2538 for (i = 0; i < graph->n_edge; ++i) {
2539 if (!is_validity(&graph->edge[i]))
2540 continue;
2541 if (graph->edge[i].src == graph->edge[i].dst)
2542 continue;
2543 if (graph->edge[i].src->scc == graph->edge[i].dst->scc)
2544 continue;
2545 if (graph->edge[i].start > con)
2546 continue;
2547 if (graph->edge[i].end <= con)
2548 continue;
2549 graph->src_scc = graph->edge[i].src->scc;
2550 graph->dst_scc = graph->edge[i].dst->scc;
2553 return 0;
2556 /* Check whether the next schedule row of the given node needs to be
2557 * non-trivial. Lower-dimensional domains may have some trivial rows,
2558 * but as soon as the number of remaining required non-trivial rows
2559 * is as large as the number or remaining rows to be computed,
2560 * all remaining rows need to be non-trivial.
2562 static int needs_row(struct isl_sched_graph *graph, struct isl_sched_node *node)
2564 return node->nvar - node->rank >= graph->maxvar - graph->n_row;
2567 /* Solve the ILP problem constructed in setup_lp.
2568 * For each node such that all the remaining rows of its schedule
2569 * need to be non-trivial, we construct a non-triviality region.
2570 * This region imposes that the next row is independent of previous rows.
2571 * In particular the coefficients c_i_x are represented by t_i_x
2572 * variables with c_i_x = Q t_i_x and Q a unimodular matrix such that
2573 * its first columns span the rows of the previously computed part
2574 * of the schedule. The non-triviality region enforces that at least
2575 * one of the remaining components of t_i_x is non-zero, i.e.,
2576 * that the new schedule row depends on at least one of the remaining
2577 * columns of Q.
2579 static __isl_give isl_vec *solve_lp(struct isl_sched_graph *graph)
2581 int i;
2582 isl_vec *sol;
2583 isl_basic_set *lp;
2585 for (i = 0; i < graph->n; ++i) {
2586 struct isl_sched_node *node = &graph->node[i];
2587 int skip = node->rank;
2588 graph->region[i].pos = node_var_coef_offset(node) + 2 * skip;
2589 if (needs_row(graph, node))
2590 graph->region[i].len = 2 * (node->nvar - skip);
2591 else
2592 graph->region[i].len = 0;
2594 lp = isl_basic_set_copy(graph->lp);
2595 sol = isl_tab_basic_set_non_trivial_lexmin(lp, 2, graph->n,
2596 graph->region, &check_conflict, graph);
2597 return sol;
2600 /* Extract the coefficients for the variables of "node" from "sol".
2602 * Within each node, the coefficients have the following order:
2603 * - c_i_0
2604 * - c_i_n (if parametric)
2605 * - positive and negative parts of c_i_x
2607 * The c_i_x^- appear before their c_i_x^+ counterpart.
2609 * Return c_i_x = c_i_x^+ - c_i_x^-
2611 static __isl_give isl_vec *extract_var_coef(struct isl_sched_node *node,
2612 __isl_keep isl_vec *sol)
2614 int i;
2615 int pos;
2616 isl_vec *csol;
2618 if (!sol)
2619 return NULL;
2620 csol = isl_vec_alloc(isl_vec_get_ctx(sol), node->nvar);
2621 if (!csol)
2622 return NULL;
2624 pos = 1 + node_var_coef_offset(node);
2625 for (i = 0; i < node->nvar; ++i)
2626 isl_int_sub(csol->el[i],
2627 sol->el[pos + 2 * i + 1], sol->el[pos + 2 * i]);
2629 return csol;
2632 /* Update the schedules of all nodes based on the given solution
2633 * of the LP problem.
2634 * The new row is added to the current band.
2635 * All possibly negative coefficients are encoded as a difference
2636 * of two non-negative variables, so we need to perform the subtraction
2637 * here. Moreover, if use_cmap is set, then the solution does
2638 * not refer to the actual coefficients c_i_x, but instead to variables
2639 * t_i_x such that c_i_x = Q t_i_x and Q is equal to node->cmap.
2640 * In this case, we then also need to perform this multiplication
2641 * to obtain the values of c_i_x.
2643 * If coincident is set, then the caller guarantees that the new
2644 * row satisfies the coincidence constraints.
2646 static int update_schedule(struct isl_sched_graph *graph,
2647 __isl_take isl_vec *sol, int use_cmap, int coincident)
2649 int i, j;
2650 isl_vec *csol = NULL;
2652 if (!sol)
2653 goto error;
2654 if (sol->size == 0)
2655 isl_die(sol->ctx, isl_error_internal,
2656 "no solution found", goto error);
2657 if (graph->n_total_row >= graph->max_row)
2658 isl_die(sol->ctx, isl_error_internal,
2659 "too many schedule rows", goto error);
2661 for (i = 0; i < graph->n; ++i) {
2662 struct isl_sched_node *node = &graph->node[i];
2663 int pos = node->start;
2664 int row = isl_mat_rows(node->sched);
2666 isl_vec_free(csol);
2667 csol = extract_var_coef(node, sol);
2668 if (!csol)
2669 goto error;
2671 isl_map_free(node->sched_map);
2672 node->sched_map = NULL;
2673 node->sched = isl_mat_add_rows(node->sched, 1);
2674 if (!node->sched)
2675 goto error;
2676 for (j = 0; j < 1 + node->nparam; ++j)
2677 node->sched = isl_mat_set_element(node->sched,
2678 row, j, sol->el[1 + pos + j]);
2679 if (use_cmap)
2680 csol = isl_mat_vec_product(isl_mat_copy(node->cmap),
2681 csol);
2682 if (!csol)
2683 goto error;
2684 for (j = 0; j < node->nvar; ++j)
2685 node->sched = isl_mat_set_element(node->sched,
2686 row, 1 + node->nparam + j, csol->el[j]);
2687 node->coincident[graph->n_total_row] = coincident;
2689 isl_vec_free(sol);
2690 isl_vec_free(csol);
2692 graph->n_row++;
2693 graph->n_total_row++;
2695 return 0;
2696 error:
2697 isl_vec_free(sol);
2698 isl_vec_free(csol);
2699 return -1;
2702 /* Convert row "row" of node->sched into an isl_aff living in "ls"
2703 * and return this isl_aff.
2705 static __isl_give isl_aff *extract_schedule_row(__isl_take isl_local_space *ls,
2706 struct isl_sched_node *node, int row)
2708 int j;
2709 isl_int v;
2710 isl_aff *aff;
2712 isl_int_init(v);
2714 aff = isl_aff_zero_on_domain(ls);
2715 isl_mat_get_element(node->sched, row, 0, &v);
2716 aff = isl_aff_set_constant(aff, v);
2717 for (j = 0; j < node->nparam; ++j) {
2718 isl_mat_get_element(node->sched, row, 1 + j, &v);
2719 aff = isl_aff_set_coefficient(aff, isl_dim_param, j, v);
2721 for (j = 0; j < node->nvar; ++j) {
2722 isl_mat_get_element(node->sched, row, 1 + node->nparam + j, &v);
2723 aff = isl_aff_set_coefficient(aff, isl_dim_in, j, v);
2726 isl_int_clear(v);
2728 return aff;
2731 /* Convert the "n" rows starting at "first" of node->sched into a multi_aff
2732 * and return this multi_aff.
2734 * The result is defined over the uncompressed node domain.
2736 static __isl_give isl_multi_aff *node_extract_partial_schedule_multi_aff(
2737 struct isl_sched_node *node, int first, int n)
2739 int i;
2740 isl_space *space;
2741 isl_local_space *ls;
2742 isl_aff *aff;
2743 isl_multi_aff *ma;
2744 int nrow;
2746 if (!node)
2747 return NULL;
2748 nrow = isl_mat_rows(node->sched);
2749 if (node->compressed)
2750 space = isl_multi_aff_get_domain_space(node->decompress);
2751 else
2752 space = isl_space_copy(node->space);
2753 ls = isl_local_space_from_space(isl_space_copy(space));
2754 space = isl_space_from_domain(space);
2755 space = isl_space_add_dims(space, isl_dim_out, n);
2756 ma = isl_multi_aff_zero(space);
2758 for (i = first; i < first + n; ++i) {
2759 aff = extract_schedule_row(isl_local_space_copy(ls), node, i);
2760 ma = isl_multi_aff_set_aff(ma, i - first, aff);
2763 isl_local_space_free(ls);
2765 if (node->compressed)
2766 ma = isl_multi_aff_pullback_multi_aff(ma,
2767 isl_multi_aff_copy(node->compress));
2769 return ma;
2772 /* Convert node->sched into a multi_aff and return this multi_aff.
2774 * The result is defined over the uncompressed node domain.
2776 static __isl_give isl_multi_aff *node_extract_schedule_multi_aff(
2777 struct isl_sched_node *node)
2779 int nrow;
2781 nrow = isl_mat_rows(node->sched);
2782 return node_extract_partial_schedule_multi_aff(node, 0, nrow);
2785 /* Convert node->sched into a map and return this map.
2787 * The result is cached in node->sched_map, which needs to be released
2788 * whenever node->sched is updated.
2789 * It is defined over the uncompressed node domain.
2791 static __isl_give isl_map *node_extract_schedule(struct isl_sched_node *node)
2793 if (!node->sched_map) {
2794 isl_multi_aff *ma;
2796 ma = node_extract_schedule_multi_aff(node);
2797 node->sched_map = isl_map_from_multi_aff(ma);
2800 return isl_map_copy(node->sched_map);
2803 /* Construct a map that can be used to update a dependence relation
2804 * based on the current schedule.
2805 * That is, construct a map expressing that source and sink
2806 * are executed within the same iteration of the current schedule.
2807 * This map can then be intersected with the dependence relation.
2808 * This is not the most efficient way, but this shouldn't be a critical
2809 * operation.
2811 static __isl_give isl_map *specializer(struct isl_sched_node *src,
2812 struct isl_sched_node *dst)
2814 isl_map *src_sched, *dst_sched;
2816 src_sched = node_extract_schedule(src);
2817 dst_sched = node_extract_schedule(dst);
2818 return isl_map_apply_range(src_sched, isl_map_reverse(dst_sched));
2821 /* Intersect the domains of the nested relations in domain and range
2822 * of "umap" with "map".
2824 static __isl_give isl_union_map *intersect_domains(
2825 __isl_take isl_union_map *umap, __isl_keep isl_map *map)
2827 isl_union_set *uset;
2829 umap = isl_union_map_zip(umap);
2830 uset = isl_union_set_from_set(isl_map_wrap(isl_map_copy(map)));
2831 umap = isl_union_map_intersect_domain(umap, uset);
2832 umap = isl_union_map_zip(umap);
2833 return umap;
2836 /* Update the dependence relation of the given edge based
2837 * on the current schedule.
2838 * If the dependence is carried completely by the current schedule, then
2839 * it is removed from the edge_tables. It is kept in the list of edges
2840 * as otherwise all edge_tables would have to be recomputed.
2842 * If the edge is of a type that can appear multiple times
2843 * between the same pair of nodes, then it is added to
2844 * the edge table (again). This prevents the situation
2845 * where none of these edges is referenced from the edge table
2846 * because the one that was referenced turned out to be empty and
2847 * was therefore removed from the table.
2849 static int update_edge(isl_ctx *ctx, struct isl_sched_graph *graph,
2850 struct isl_sched_edge *edge)
2852 int empty;
2853 isl_map *id;
2855 id = specializer(edge->src, edge->dst);
2856 edge->map = isl_map_intersect(edge->map, isl_map_copy(id));
2857 if (!edge->map)
2858 goto error;
2860 if (edge->tagged_condition) {
2861 edge->tagged_condition =
2862 intersect_domains(edge->tagged_condition, id);
2863 if (!edge->tagged_condition)
2864 goto error;
2866 if (edge->tagged_validity) {
2867 edge->tagged_validity =
2868 intersect_domains(edge->tagged_validity, id);
2869 if (!edge->tagged_validity)
2870 goto error;
2873 empty = isl_map_plain_is_empty(edge->map);
2874 if (empty < 0)
2875 goto error;
2876 if (empty) {
2877 graph_remove_edge(graph, edge);
2878 } else if (is_multi_edge_type(edge)) {
2879 if (graph_edge_tables_add(ctx, graph, edge) < 0)
2880 goto error;
2883 isl_map_free(id);
2884 return 0;
2885 error:
2886 isl_map_free(id);
2887 return -1;
2890 /* Does the domain of "umap" intersect "uset"?
2892 static int domain_intersects(__isl_keep isl_union_map *umap,
2893 __isl_keep isl_union_set *uset)
2895 int empty;
2897 umap = isl_union_map_copy(umap);
2898 umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(uset));
2899 empty = isl_union_map_is_empty(umap);
2900 isl_union_map_free(umap);
2902 return empty < 0 ? -1 : !empty;
2905 /* Does the range of "umap" intersect "uset"?
2907 static int range_intersects(__isl_keep isl_union_map *umap,
2908 __isl_keep isl_union_set *uset)
2910 int empty;
2912 umap = isl_union_map_copy(umap);
2913 umap = isl_union_map_intersect_range(umap, isl_union_set_copy(uset));
2914 empty = isl_union_map_is_empty(umap);
2915 isl_union_map_free(umap);
2917 return empty < 0 ? -1 : !empty;
2920 /* Are the condition dependences of "edge" local with respect to
2921 * the current schedule?
2923 * That is, are domain and range of the condition dependences mapped
2924 * to the same point?
2926 * In other words, is the condition false?
2928 static int is_condition_false(struct isl_sched_edge *edge)
2930 isl_union_map *umap;
2931 isl_map *map, *sched, *test;
2932 int empty, local;
2934 empty = isl_union_map_is_empty(edge->tagged_condition);
2935 if (empty < 0 || empty)
2936 return empty;
2938 umap = isl_union_map_copy(edge->tagged_condition);
2939 umap = isl_union_map_zip(umap);
2940 umap = isl_union_set_unwrap(isl_union_map_domain(umap));
2941 map = isl_map_from_union_map(umap);
2943 sched = node_extract_schedule(edge->src);
2944 map = isl_map_apply_domain(map, sched);
2945 sched = node_extract_schedule(edge->dst);
2946 map = isl_map_apply_range(map, sched);
2948 test = isl_map_identity(isl_map_get_space(map));
2949 local = isl_map_is_subset(map, test);
2950 isl_map_free(map);
2951 isl_map_free(test);
2953 return local;
2956 /* For each conditional validity constraint that is adjacent
2957 * to a condition with domain in condition_source or range in condition_sink,
2958 * turn it into an unconditional validity constraint.
2960 static int unconditionalize_adjacent_validity(struct isl_sched_graph *graph,
2961 __isl_take isl_union_set *condition_source,
2962 __isl_take isl_union_set *condition_sink)
2964 int i;
2966 condition_source = isl_union_set_coalesce(condition_source);
2967 condition_sink = isl_union_set_coalesce(condition_sink);
2969 for (i = 0; i < graph->n_edge; ++i) {
2970 int adjacent;
2971 isl_union_map *validity;
2973 if (!is_conditional_validity(&graph->edge[i]))
2974 continue;
2975 if (is_validity(&graph->edge[i]))
2976 continue;
2978 validity = graph->edge[i].tagged_validity;
2979 adjacent = domain_intersects(validity, condition_sink);
2980 if (adjacent >= 0 && !adjacent)
2981 adjacent = range_intersects(validity, condition_source);
2982 if (adjacent < 0)
2983 goto error;
2984 if (!adjacent)
2985 continue;
2987 set_validity(&graph->edge[i]);
2990 isl_union_set_free(condition_source);
2991 isl_union_set_free(condition_sink);
2992 return 0;
2993 error:
2994 isl_union_set_free(condition_source);
2995 isl_union_set_free(condition_sink);
2996 return -1;
2999 /* Update the dependence relations of all edges based on the current schedule
3000 * and enforce conditional validity constraints that are adjacent
3001 * to satisfied condition constraints.
3003 * First check if any of the condition constraints are satisfied
3004 * (i.e., not local to the outer schedule) and keep track of
3005 * their domain and range.
3006 * Then update all dependence relations (which removes the non-local
3007 * constraints).
3008 * Finally, if any condition constraints turned out to be satisfied,
3009 * then turn all adjacent conditional validity constraints into
3010 * unconditional validity constraints.
3012 static int update_edges(isl_ctx *ctx, struct isl_sched_graph *graph)
3014 int i;
3015 int any = 0;
3016 isl_union_set *source, *sink;
3018 source = isl_union_set_empty(isl_space_params_alloc(ctx, 0));
3019 sink = isl_union_set_empty(isl_space_params_alloc(ctx, 0));
3020 for (i = 0; i < graph->n_edge; ++i) {
3021 int local;
3022 isl_union_set *uset;
3023 isl_union_map *umap;
3025 if (!is_condition(&graph->edge[i]))
3026 continue;
3027 if (is_local(&graph->edge[i]))
3028 continue;
3029 local = is_condition_false(&graph->edge[i]);
3030 if (local < 0)
3031 goto error;
3032 if (local)
3033 continue;
3035 any = 1;
3037 umap = isl_union_map_copy(graph->edge[i].tagged_condition);
3038 uset = isl_union_map_domain(umap);
3039 source = isl_union_set_union(source, uset);
3041 umap = isl_union_map_copy(graph->edge[i].tagged_condition);
3042 uset = isl_union_map_range(umap);
3043 sink = isl_union_set_union(sink, uset);
3046 for (i = 0; i < graph->n_edge; ++i) {
3047 if (update_edge(ctx, graph, &graph->edge[i]) < 0)
3048 goto error;
3051 if (any)
3052 return unconditionalize_adjacent_validity(graph, source, sink);
3054 isl_union_set_free(source);
3055 isl_union_set_free(sink);
3056 return 0;
3057 error:
3058 isl_union_set_free(source);
3059 isl_union_set_free(sink);
3060 return -1;
3063 static void next_band(struct isl_sched_graph *graph)
3065 graph->band_start = graph->n_total_row;
3068 /* Return the union of the universe domains of the nodes in "graph"
3069 * that satisfy "pred".
3071 static __isl_give isl_union_set *isl_sched_graph_domain(isl_ctx *ctx,
3072 struct isl_sched_graph *graph,
3073 int (*pred)(struct isl_sched_node *node, int data), int data)
3075 int i;
3076 isl_set *set;
3077 isl_union_set *dom;
3079 for (i = 0; i < graph->n; ++i)
3080 if (pred(&graph->node[i], data))
3081 break;
3083 if (i >= graph->n)
3084 isl_die(ctx, isl_error_internal,
3085 "empty component", return NULL);
3087 set = isl_set_universe(isl_space_copy(graph->node[i].space));
3088 dom = isl_union_set_from_set(set);
3090 for (i = i + 1; i < graph->n; ++i) {
3091 if (!pred(&graph->node[i], data))
3092 continue;
3093 set = isl_set_universe(isl_space_copy(graph->node[i].space));
3094 dom = isl_union_set_union(dom, isl_union_set_from_set(set));
3097 return dom;
3100 /* Return a list of unions of universe domains, where each element
3101 * in the list corresponds to an SCC (or WCC) indexed by node->scc.
3103 static __isl_give isl_union_set_list *extract_sccs(isl_ctx *ctx,
3104 struct isl_sched_graph *graph)
3106 int i;
3107 isl_union_set_list *filters;
3109 filters = isl_union_set_list_alloc(ctx, graph->scc);
3110 for (i = 0; i < graph->scc; ++i) {
3111 isl_union_set *dom;
3113 dom = isl_sched_graph_domain(ctx, graph, &node_scc_exactly, i);
3114 filters = isl_union_set_list_add(filters, dom);
3117 return filters;
3120 /* Return a list of two unions of universe domains, one for the SCCs up
3121 * to and including graph->src_scc and another for the other SCCs.
3123 static __isl_give isl_union_set_list *extract_split(isl_ctx *ctx,
3124 struct isl_sched_graph *graph)
3126 isl_union_set *dom;
3127 isl_union_set_list *filters;
3129 filters = isl_union_set_list_alloc(ctx, 2);
3130 dom = isl_sched_graph_domain(ctx, graph,
3131 &node_scc_at_most, graph->src_scc);
3132 filters = isl_union_set_list_add(filters, dom);
3133 dom = isl_sched_graph_domain(ctx, graph,
3134 &node_scc_at_least, graph->src_scc + 1);
3135 filters = isl_union_set_list_add(filters, dom);
3137 return filters;
3140 /* Copy nodes that satisfy node_pred from the src dependence graph
3141 * to the dst dependence graph.
3143 static int copy_nodes(struct isl_sched_graph *dst, struct isl_sched_graph *src,
3144 int (*node_pred)(struct isl_sched_node *node, int data), int data)
3146 int i;
3148 dst->n = 0;
3149 for (i = 0; i < src->n; ++i) {
3150 int j;
3152 if (!node_pred(&src->node[i], data))
3153 continue;
3155 j = dst->n;
3156 dst->node[j].space = isl_space_copy(src->node[i].space);
3157 dst->node[j].compressed = src->node[i].compressed;
3158 dst->node[j].hull = isl_set_copy(src->node[i].hull);
3159 dst->node[j].compress =
3160 isl_multi_aff_copy(src->node[i].compress);
3161 dst->node[j].decompress =
3162 isl_multi_aff_copy(src->node[i].decompress);
3163 dst->node[j].nvar = src->node[i].nvar;
3164 dst->node[j].nparam = src->node[i].nparam;
3165 dst->node[j].sched = isl_mat_copy(src->node[i].sched);
3166 dst->node[j].sched_map = isl_map_copy(src->node[i].sched_map);
3167 dst->node[j].coincident = src->node[i].coincident;
3168 dst->node[j].sizes = isl_multi_val_copy(src->node[i].sizes);
3169 dst->node[j].max = isl_vec_copy(src->node[i].max);
3170 dst->n++;
3172 if (!dst->node[j].space || !dst->node[j].sched)
3173 return -1;
3174 if (dst->node[j].compressed &&
3175 (!dst->node[j].hull || !dst->node[j].compress ||
3176 !dst->node[j].decompress))
3177 return -1;
3180 return 0;
3183 /* Copy non-empty edges that satisfy edge_pred from the src dependence graph
3184 * to the dst dependence graph.
3185 * If the source or destination node of the edge is not in the destination
3186 * graph, then it must be a backward proximity edge and it should simply
3187 * be ignored.
3189 static int copy_edges(isl_ctx *ctx, struct isl_sched_graph *dst,
3190 struct isl_sched_graph *src,
3191 int (*edge_pred)(struct isl_sched_edge *edge, int data), int data)
3193 int i;
3195 dst->n_edge = 0;
3196 for (i = 0; i < src->n_edge; ++i) {
3197 struct isl_sched_edge *edge = &src->edge[i];
3198 isl_map *map;
3199 isl_union_map *tagged_condition;
3200 isl_union_map *tagged_validity;
3201 struct isl_sched_node *dst_src, *dst_dst;
3203 if (!edge_pred(edge, data))
3204 continue;
3206 if (isl_map_plain_is_empty(edge->map))
3207 continue;
3209 dst_src = graph_find_node(ctx, dst, edge->src->space);
3210 dst_dst = graph_find_node(ctx, dst, edge->dst->space);
3211 if (!dst_src || !dst_dst) {
3212 if (is_validity(edge) || is_conditional_validity(edge))
3213 isl_die(ctx, isl_error_internal,
3214 "backward (conditional) validity edge",
3215 return -1);
3216 continue;
3219 map = isl_map_copy(edge->map);
3220 tagged_condition = isl_union_map_copy(edge->tagged_condition);
3221 tagged_validity = isl_union_map_copy(edge->tagged_validity);
3223 dst->edge[dst->n_edge].src = dst_src;
3224 dst->edge[dst->n_edge].dst = dst_dst;
3225 dst->edge[dst->n_edge].map = map;
3226 dst->edge[dst->n_edge].tagged_condition = tagged_condition;
3227 dst->edge[dst->n_edge].tagged_validity = tagged_validity;
3228 dst->edge[dst->n_edge].types = edge->types;
3229 dst->n_edge++;
3231 if (edge->tagged_condition && !tagged_condition)
3232 return -1;
3233 if (edge->tagged_validity && !tagged_validity)
3234 return -1;
3236 if (graph_edge_tables_add(ctx, dst,
3237 &dst->edge[dst->n_edge - 1]) < 0)
3238 return -1;
3241 return 0;
3244 /* Compute the maximal number of variables over all nodes.
3245 * This is the maximal number of linearly independent schedule
3246 * rows that we need to compute.
3247 * Just in case we end up in a part of the dependence graph
3248 * with only lower-dimensional domains, we make sure we will
3249 * compute the required amount of extra linearly independent rows.
3251 static int compute_maxvar(struct isl_sched_graph *graph)
3253 int i;
3255 graph->maxvar = 0;
3256 for (i = 0; i < graph->n; ++i) {
3257 struct isl_sched_node *node = &graph->node[i];
3258 int nvar;
3260 if (node_update_cmap(node) < 0)
3261 return -1;
3262 nvar = node->nvar + graph->n_row - node->rank;
3263 if (nvar > graph->maxvar)
3264 graph->maxvar = nvar;
3267 return 0;
3270 /* Extract the subgraph of "graph" that consists of the node satisfying
3271 * "node_pred" and the edges satisfying "edge_pred" and store
3272 * the result in "sub".
3274 static int extract_sub_graph(isl_ctx *ctx, struct isl_sched_graph *graph,
3275 int (*node_pred)(struct isl_sched_node *node, int data),
3276 int (*edge_pred)(struct isl_sched_edge *edge, int data),
3277 int data, struct isl_sched_graph *sub)
3279 int i, n = 0, n_edge = 0;
3280 int t;
3282 for (i = 0; i < graph->n; ++i)
3283 if (node_pred(&graph->node[i], data))
3284 ++n;
3285 for (i = 0; i < graph->n_edge; ++i)
3286 if (edge_pred(&graph->edge[i], data))
3287 ++n_edge;
3288 if (graph_alloc(ctx, sub, n, n_edge) < 0)
3289 return -1;
3290 if (copy_nodes(sub, graph, node_pred, data) < 0)
3291 return -1;
3292 if (graph_init_table(ctx, sub) < 0)
3293 return -1;
3294 for (t = 0; t <= isl_edge_last; ++t)
3295 sub->max_edge[t] = graph->max_edge[t];
3296 if (graph_init_edge_tables(ctx, sub) < 0)
3297 return -1;
3298 if (copy_edges(ctx, sub, graph, edge_pred, data) < 0)
3299 return -1;
3300 sub->n_row = graph->n_row;
3301 sub->max_row = graph->max_row;
3302 sub->n_total_row = graph->n_total_row;
3303 sub->band_start = graph->band_start;
3305 return 0;
3308 static __isl_give isl_schedule_node *compute_schedule(isl_schedule_node *node,
3309 struct isl_sched_graph *graph);
3310 static __isl_give isl_schedule_node *compute_schedule_wcc(
3311 isl_schedule_node *node, struct isl_sched_graph *graph);
3313 /* Compute a schedule for a subgraph of "graph". In particular, for
3314 * the graph composed of nodes that satisfy node_pred and edges that
3315 * that satisfy edge_pred.
3316 * If the subgraph is known to consist of a single component, then wcc should
3317 * be set and then we call compute_schedule_wcc on the constructed subgraph.
3318 * Otherwise, we call compute_schedule, which will check whether the subgraph
3319 * is connected.
3321 * The schedule is inserted at "node" and the updated schedule node
3322 * is returned.
3324 static __isl_give isl_schedule_node *compute_sub_schedule(
3325 __isl_take isl_schedule_node *node, isl_ctx *ctx,
3326 struct isl_sched_graph *graph,
3327 int (*node_pred)(struct isl_sched_node *node, int data),
3328 int (*edge_pred)(struct isl_sched_edge *edge, int data),
3329 int data, int wcc)
3331 struct isl_sched_graph split = { 0 };
3333 if (extract_sub_graph(ctx, graph, node_pred, edge_pred, data,
3334 &split) < 0)
3335 goto error;
3337 if (wcc)
3338 node = compute_schedule_wcc(node, &split);
3339 else
3340 node = compute_schedule(node, &split);
3342 graph_free(ctx, &split);
3343 return node;
3344 error:
3345 graph_free(ctx, &split);
3346 return isl_schedule_node_free(node);
3349 static int edge_scc_exactly(struct isl_sched_edge *edge, int scc)
3351 return edge->src->scc == scc && edge->dst->scc == scc;
3354 static int edge_dst_scc_at_most(struct isl_sched_edge *edge, int scc)
3356 return edge->dst->scc <= scc;
3359 static int edge_src_scc_at_least(struct isl_sched_edge *edge, int scc)
3361 return edge->src->scc >= scc;
3364 /* Reset the current band by dropping all its schedule rows.
3366 static int reset_band(struct isl_sched_graph *graph)
3368 int i;
3369 int drop;
3371 drop = graph->n_total_row - graph->band_start;
3372 graph->n_total_row -= drop;
3373 graph->n_row -= drop;
3375 for (i = 0; i < graph->n; ++i) {
3376 struct isl_sched_node *node = &graph->node[i];
3378 isl_map_free(node->sched_map);
3379 node->sched_map = NULL;
3381 node->sched = isl_mat_drop_rows(node->sched,
3382 graph->band_start, drop);
3384 if (!node->sched)
3385 return -1;
3388 return 0;
3391 /* Split the current graph into two parts and compute a schedule for each
3392 * part individually. In particular, one part consists of all SCCs up
3393 * to and including graph->src_scc, while the other part contains the other
3394 * SCCs. The split is enforced by a sequence node inserted at position "node"
3395 * in the schedule tree. Return the updated schedule node.
3396 * If either of these two parts consists of a sequence, then it is spliced
3397 * into the sequence containing the two parts.
3399 * The current band is reset. It would be possible to reuse
3400 * the previously computed rows as the first rows in the next
3401 * band, but recomputing them may result in better rows as we are looking
3402 * at a smaller part of the dependence graph.
3404 static __isl_give isl_schedule_node *compute_split_schedule(
3405 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph)
3407 int is_seq;
3408 isl_ctx *ctx;
3409 isl_union_set_list *filters;
3411 if (!node)
3412 return NULL;
3414 if (reset_band(graph) < 0)
3415 return isl_schedule_node_free(node);
3417 next_band(graph);
3419 ctx = isl_schedule_node_get_ctx(node);
3420 filters = extract_split(ctx, graph);
3421 node = isl_schedule_node_insert_sequence(node, filters);
3422 node = isl_schedule_node_child(node, 1);
3423 node = isl_schedule_node_child(node, 0);
3425 node = compute_sub_schedule(node, ctx, graph,
3426 &node_scc_at_least, &edge_src_scc_at_least,
3427 graph->src_scc + 1, 0);
3428 is_seq = isl_schedule_node_get_type(node) == isl_schedule_node_sequence;
3429 node = isl_schedule_node_parent(node);
3430 node = isl_schedule_node_parent(node);
3431 if (is_seq)
3432 node = isl_schedule_node_sequence_splice_child(node, 1);
3433 node = isl_schedule_node_child(node, 0);
3434 node = isl_schedule_node_child(node, 0);
3435 node = compute_sub_schedule(node, ctx, graph,
3436 &node_scc_at_most, &edge_dst_scc_at_most,
3437 graph->src_scc, 0);
3438 is_seq = isl_schedule_node_get_type(node) == isl_schedule_node_sequence;
3439 node = isl_schedule_node_parent(node);
3440 node = isl_schedule_node_parent(node);
3441 if (is_seq)
3442 node = isl_schedule_node_sequence_splice_child(node, 0);
3444 return node;
3447 /* Insert a band node at position "node" in the schedule tree corresponding
3448 * to the current band in "graph". Mark the band node permutable
3449 * if "permutable" is set.
3450 * The partial schedules and the coincidence property are extracted
3451 * from the graph nodes.
3452 * Return the updated schedule node.
3454 static __isl_give isl_schedule_node *insert_current_band(
3455 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph,
3456 int permutable)
3458 int i;
3459 int start, end, n;
3460 isl_multi_aff *ma;
3461 isl_multi_pw_aff *mpa;
3462 isl_multi_union_pw_aff *mupa;
3464 if (!node)
3465 return NULL;
3467 if (graph->n < 1)
3468 isl_die(isl_schedule_node_get_ctx(node), isl_error_internal,
3469 "graph should have at least one node",
3470 return isl_schedule_node_free(node));
3472 start = graph->band_start;
3473 end = graph->n_total_row;
3474 n = end - start;
3476 ma = node_extract_partial_schedule_multi_aff(&graph->node[0], start, n);
3477 mpa = isl_multi_pw_aff_from_multi_aff(ma);
3478 mupa = isl_multi_union_pw_aff_from_multi_pw_aff(mpa);
3480 for (i = 1; i < graph->n; ++i) {
3481 isl_multi_union_pw_aff *mupa_i;
3483 ma = node_extract_partial_schedule_multi_aff(&graph->node[i],
3484 start, n);
3485 mpa = isl_multi_pw_aff_from_multi_aff(ma);
3486 mupa_i = isl_multi_union_pw_aff_from_multi_pw_aff(mpa);
3487 mupa = isl_multi_union_pw_aff_union_add(mupa, mupa_i);
3489 node = isl_schedule_node_insert_partial_schedule(node, mupa);
3491 for (i = 0; i < n; ++i)
3492 node = isl_schedule_node_band_member_set_coincident(node, i,
3493 graph->node[0].coincident[start + i]);
3494 node = isl_schedule_node_band_set_permutable(node, permutable);
3496 return node;
3499 /* Update the dependence relations based on the current schedule,
3500 * add the current band to "node" and then continue with the computation
3501 * of the next band.
3502 * Return the updated schedule node.
3504 static __isl_give isl_schedule_node *compute_next_band(
3505 __isl_take isl_schedule_node *node,
3506 struct isl_sched_graph *graph, int permutable)
3508 isl_ctx *ctx;
3510 if (!node)
3511 return NULL;
3513 ctx = isl_schedule_node_get_ctx(node);
3514 if (update_edges(ctx, graph) < 0)
3515 return isl_schedule_node_free(node);
3516 node = insert_current_band(node, graph, permutable);
3517 next_band(graph);
3519 node = isl_schedule_node_child(node, 0);
3520 node = compute_schedule(node, graph);
3521 node = isl_schedule_node_parent(node);
3523 return node;
3526 /* Add constraints to graph->lp that force the dependence "map" (which
3527 * is part of the dependence relation of "edge")
3528 * to be respected and attempt to carry it, where the edge is one from
3529 * a node j to itself. "pos" is the sequence number of the given map.
3530 * That is, add constraints that enforce
3532 * (c_j_0 + c_j_n n + c_j_x y) - (c_j_0 + c_j_n n + c_j_x x)
3533 * = c_j_x (y - x) >= e_i
3535 * for each (x,y) in R.
3536 * We obtain general constraints on coefficients (c_0, c_n, c_x)
3537 * of valid constraints for (y - x) and then plug in (-e_i, 0, c_j_x),
3538 * with each coefficient in c_j_x represented as a pair of non-negative
3539 * coefficients.
3541 static int add_intra_constraints(struct isl_sched_graph *graph,
3542 struct isl_sched_edge *edge, __isl_take isl_map *map, int pos)
3544 int offset;
3545 isl_ctx *ctx = isl_map_get_ctx(map);
3546 isl_dim_map *dim_map;
3547 isl_basic_set *coef;
3548 struct isl_sched_node *node = edge->src;
3550 coef = intra_coefficients(graph, node, map);
3551 if (!coef)
3552 return -1;
3554 offset = coef_var_offset(coef);
3555 dim_map = intra_dim_map(ctx, graph, node, offset, 1);
3556 isl_dim_map_range(dim_map, 3 + pos, 0, 0, 0, 1, -1);
3557 graph->lp = isl_basic_set_extend_constraints(graph->lp,
3558 coef->n_eq, coef->n_ineq);
3559 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
3560 coef, dim_map);
3562 return 0;
3565 /* Add constraints to graph->lp that force the dependence "map" (which
3566 * is part of the dependence relation of "edge")
3567 * to be respected and attempt to carry it, where the edge is one from
3568 * node j to node k. "pos" is the sequence number of the given map.
3569 * That is, add constraints that enforce
3571 * (c_k_0 + c_k_n n + c_k_x y) - (c_j_0 + c_j_n n + c_j_x x) >= e_i
3573 * for each (x,y) in R.
3574 * We obtain general constraints on coefficients (c_0, c_n, c_x)
3575 * of valid constraints for R and then plug in
3576 * (-e_i + c_k_0 - c_j_0, c_k_n - c_j_n, c_k_x - c_j_x)
3577 * with each coefficient (except e_i, c_*_0 and c_*_n)
3578 * represented as a pair of non-negative coefficients.
3580 static int add_inter_constraints(struct isl_sched_graph *graph,
3581 struct isl_sched_edge *edge, __isl_take isl_map *map, int pos)
3583 int offset;
3584 isl_ctx *ctx = isl_map_get_ctx(map);
3585 isl_dim_map *dim_map;
3586 isl_basic_set *coef;
3587 struct isl_sched_node *src = edge->src;
3588 struct isl_sched_node *dst = edge->dst;
3590 coef = inter_coefficients(graph, edge, map);
3591 if (!coef)
3592 return -1;
3594 offset = coef_var_offset(coef);
3595 dim_map = inter_dim_map(ctx, graph, src, dst, offset, 1);
3596 isl_dim_map_range(dim_map, 3 + pos, 0, 0, 0, 1, -1);
3597 graph->lp = isl_basic_set_extend_constraints(graph->lp,
3598 coef->n_eq, coef->n_ineq);
3599 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
3600 coef, dim_map);
3602 return 0;
3605 /* Add constraints to graph->lp that force all (conditional) validity
3606 * dependences to be respected and attempt to carry them.
3608 static int add_all_constraints(struct isl_sched_graph *graph)
3610 int i, j;
3611 int pos;
3613 pos = 0;
3614 for (i = 0; i < graph->n_edge; ++i) {
3615 struct isl_sched_edge *edge= &graph->edge[i];
3617 if (!is_any_validity(edge))
3618 continue;
3620 for (j = 0; j < edge->map->n; ++j) {
3621 isl_basic_map *bmap;
3622 isl_map *map;
3624 bmap = isl_basic_map_copy(edge->map->p[j]);
3625 map = isl_map_from_basic_map(bmap);
3627 if (edge->src == edge->dst &&
3628 add_intra_constraints(graph, edge, map, pos) < 0)
3629 return -1;
3630 if (edge->src != edge->dst &&
3631 add_inter_constraints(graph, edge, map, pos) < 0)
3632 return -1;
3633 ++pos;
3637 return 0;
3640 /* Count the number of equality and inequality constraints
3641 * that will be added to the carry_lp problem.
3642 * We count each edge exactly once.
3644 static int count_all_constraints(struct isl_sched_graph *graph,
3645 int *n_eq, int *n_ineq)
3647 int i, j;
3649 *n_eq = *n_ineq = 0;
3650 for (i = 0; i < graph->n_edge; ++i) {
3651 struct isl_sched_edge *edge= &graph->edge[i];
3653 if (!is_any_validity(edge))
3654 continue;
3656 for (j = 0; j < edge->map->n; ++j) {
3657 isl_basic_map *bmap;
3658 isl_map *map;
3660 bmap = isl_basic_map_copy(edge->map->p[j]);
3661 map = isl_map_from_basic_map(bmap);
3663 if (count_map_constraints(graph, edge, map,
3664 n_eq, n_ineq, 1, 0) < 0)
3665 return -1;
3669 return 0;
3672 /* Return the total number of (validity) edges that carry_dependences will
3673 * attempt to carry.
3675 static int count_carry_edges(struct isl_sched_graph *graph)
3677 int i;
3678 int n_edge;
3680 n_edge = 0;
3681 for (i = 0; i < graph->n_edge; ++i) {
3682 struct isl_sched_edge *edge = &graph->edge[i];
3684 if (!is_any_validity(edge))
3685 continue;
3687 n_edge += isl_map_n_basic_map(edge->map);
3690 return n_edge;
3693 /* Construct an LP problem for finding schedule coefficients
3694 * such that the schedule carries as many validity dependences as possible.
3695 * In particular, for each dependence i, we bound the dependence distance
3696 * from below by e_i, with 0 <= e_i <= 1 and then maximize the sum
3697 * of all e_i's. Dependences with e_i = 0 in the solution are simply
3698 * respected, while those with e_i > 0 (in practice e_i = 1) are carried.
3699 * Note that if the dependence relation is a union of basic maps,
3700 * then we have to consider each basic map individually as it may only
3701 * be possible to carry the dependences expressed by some of those
3702 * basic maps and not all of them.
3703 * Below, we consider each of those basic maps as a separate "edge".
3705 * All variables of the LP are non-negative. The actual coefficients
3706 * may be negative, so each coefficient is represented as the difference
3707 * of two non-negative variables. The negative part always appears
3708 * immediately before the positive part.
3709 * Other than that, the variables have the following order
3711 * - sum of (1 - e_i) over all edges
3712 * - sum of all c_n coefficients
3713 * (unconstrained when computing non-parametric schedules)
3714 * - sum of positive and negative parts of all c_x coefficients
3715 * - for each edge
3716 * - e_i
3717 * - for each node
3718 * - c_i_0
3719 * - c_i_n (if parametric)
3720 * - positive and negative parts of c_i_x
3722 * The constraints are those from the (validity) edges plus three equalities
3723 * to express the sums and n_edge inequalities to express e_i <= 1.
3725 static isl_stat setup_carry_lp(isl_ctx *ctx, struct isl_sched_graph *graph)
3727 int i;
3728 int k;
3729 isl_space *dim;
3730 unsigned total;
3731 int n_eq, n_ineq;
3732 int n_edge;
3734 n_edge = count_carry_edges(graph);
3736 total = 3 + n_edge;
3737 for (i = 0; i < graph->n; ++i) {
3738 struct isl_sched_node *node = &graph->node[graph->sorted[i]];
3739 node->start = total;
3740 total += 1 + node->nparam + 2 * node->nvar;
3743 if (count_all_constraints(graph, &n_eq, &n_ineq) < 0)
3744 return isl_stat_error;
3746 dim = isl_space_set_alloc(ctx, 0, total);
3747 isl_basic_set_free(graph->lp);
3748 n_eq += 3;
3749 n_ineq += n_edge;
3750 graph->lp = isl_basic_set_alloc_space(dim, 0, n_eq, n_ineq);
3751 graph->lp = isl_basic_set_set_rational(graph->lp);
3753 k = isl_basic_set_alloc_equality(graph->lp);
3754 if (k < 0)
3755 return isl_stat_error;
3756 isl_seq_clr(graph->lp->eq[k], 1 + total);
3757 isl_int_set_si(graph->lp->eq[k][0], -n_edge);
3758 isl_int_set_si(graph->lp->eq[k][1], 1);
3759 for (i = 0; i < n_edge; ++i)
3760 isl_int_set_si(graph->lp->eq[k][4 + i], 1);
3762 if (add_param_sum_constraint(graph, 1) < 0)
3763 return isl_stat_error;
3764 if (add_var_sum_constraint(graph, 2) < 0)
3765 return isl_stat_error;
3767 for (i = 0; i < n_edge; ++i) {
3768 k = isl_basic_set_alloc_inequality(graph->lp);
3769 if (k < 0)
3770 return isl_stat_error;
3771 isl_seq_clr(graph->lp->ineq[k], 1 + total);
3772 isl_int_set_si(graph->lp->ineq[k][4 + i], -1);
3773 isl_int_set_si(graph->lp->ineq[k][0], 1);
3776 if (add_all_constraints(graph) < 0)
3777 return isl_stat_error;
3779 return isl_stat_ok;
3782 static __isl_give isl_schedule_node *compute_component_schedule(
3783 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph,
3784 int wcc);
3786 /* Comparison function for sorting the statements based on
3787 * the corresponding value in "r".
3789 static int smaller_value(const void *a, const void *b, void *data)
3791 isl_vec *r = data;
3792 const int *i1 = a;
3793 const int *i2 = b;
3795 return isl_int_cmp(r->el[*i1], r->el[*i2]);
3798 /* If the schedule_split_scaled option is set and if the linear
3799 * parts of the scheduling rows for all nodes in the graphs have
3800 * a non-trivial common divisor, then split off the remainder of the
3801 * constant term modulo this common divisor from the linear part.
3802 * Otherwise, insert a band node directly and continue with
3803 * the construction of the schedule.
3805 * If a non-trivial common divisor is found, then
3806 * the linear part is reduced and the remainder is enforced
3807 * by a sequence node with the children placed in the order
3808 * of this remainder.
3809 * In particular, we assign an scc index based on the remainder and
3810 * then rely on compute_component_schedule to insert the sequence and
3811 * to continue the schedule construction on each part.
3813 static __isl_give isl_schedule_node *split_scaled(
3814 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph)
3816 int i;
3817 int row;
3818 int scc;
3819 isl_ctx *ctx;
3820 isl_int gcd, gcd_i;
3821 isl_vec *r;
3822 int *order;
3824 if (!node)
3825 return NULL;
3827 ctx = isl_schedule_node_get_ctx(node);
3828 if (!ctx->opt->schedule_split_scaled)
3829 return compute_next_band(node, graph, 0);
3830 if (graph->n <= 1)
3831 return compute_next_band(node, graph, 0);
3833 isl_int_init(gcd);
3834 isl_int_init(gcd_i);
3836 isl_int_set_si(gcd, 0);
3838 row = isl_mat_rows(graph->node[0].sched) - 1;
3840 for (i = 0; i < graph->n; ++i) {
3841 struct isl_sched_node *node = &graph->node[i];
3842 int cols = isl_mat_cols(node->sched);
3844 isl_seq_gcd(node->sched->row[row] + 1, cols - 1, &gcd_i);
3845 isl_int_gcd(gcd, gcd, gcd_i);
3848 isl_int_clear(gcd_i);
3850 if (isl_int_cmp_si(gcd, 1) <= 0) {
3851 isl_int_clear(gcd);
3852 return compute_next_band(node, graph, 0);
3855 r = isl_vec_alloc(ctx, graph->n);
3856 order = isl_calloc_array(ctx, int, graph->n);
3857 if (!r || !order)
3858 goto error;
3860 for (i = 0; i < graph->n; ++i) {
3861 struct isl_sched_node *node = &graph->node[i];
3863 order[i] = i;
3864 isl_int_fdiv_r(r->el[i], node->sched->row[row][0], gcd);
3865 isl_int_fdiv_q(node->sched->row[row][0],
3866 node->sched->row[row][0], gcd);
3867 isl_int_mul(node->sched->row[row][0],
3868 node->sched->row[row][0], gcd);
3869 node->sched = isl_mat_scale_down_row(node->sched, row, gcd);
3870 if (!node->sched)
3871 goto error;
3874 if (isl_sort(order, graph->n, sizeof(order[0]), &smaller_value, r) < 0)
3875 goto error;
3877 scc = 0;
3878 for (i = 0; i < graph->n; ++i) {
3879 if (i > 0 && isl_int_ne(r->el[order[i - 1]], r->el[order[i]]))
3880 ++scc;
3881 graph->node[order[i]].scc = scc;
3883 graph->scc = ++scc;
3884 graph->weak = 0;
3886 isl_int_clear(gcd);
3887 isl_vec_free(r);
3888 free(order);
3890 if (update_edges(ctx, graph) < 0)
3891 return isl_schedule_node_free(node);
3892 node = insert_current_band(node, graph, 0);
3893 next_band(graph);
3895 node = isl_schedule_node_child(node, 0);
3896 node = compute_component_schedule(node, graph, 0);
3897 node = isl_schedule_node_parent(node);
3899 return node;
3900 error:
3901 isl_vec_free(r);
3902 free(order);
3903 isl_int_clear(gcd);
3904 return isl_schedule_node_free(node);
3907 /* Is the schedule row "sol" trivial on node "node"?
3908 * That is, is the solution zero on the dimensions orthogonal to
3909 * the previously found solutions?
3910 * Return 1 if the solution is trivial, 0 if it is not and -1 on error.
3912 * Each coefficient is represented as the difference between
3913 * two non-negative values in "sol". "sol" has been computed
3914 * in terms of the original iterators (i.e., without use of cmap).
3915 * We construct the schedule row s and write it as a linear
3916 * combination of (linear combinations of) previously computed schedule rows.
3917 * s = Q c or c = U s.
3918 * If the final entries of c are all zero, then the solution is trivial.
3920 static int is_trivial(struct isl_sched_node *node, __isl_keep isl_vec *sol)
3922 int trivial;
3923 isl_vec *node_sol;
3925 if (!sol)
3926 return -1;
3927 if (node->nvar == node->rank)
3928 return 0;
3930 node_sol = extract_var_coef(node, sol);
3931 node_sol = isl_mat_vec_product(isl_mat_copy(node->cinv), node_sol);
3932 if (!node_sol)
3933 return -1;
3935 trivial = isl_seq_first_non_zero(node_sol->el + node->rank,
3936 node->nvar - node->rank) == -1;
3938 isl_vec_free(node_sol);
3940 return trivial;
3943 /* Is the schedule row "sol" trivial on any node where it should
3944 * not be trivial?
3945 * "sol" has been computed in terms of the original iterators
3946 * (i.e., without use of cmap).
3947 * Return 1 if any solution is trivial, 0 if they are not and -1 on error.
3949 static int is_any_trivial(struct isl_sched_graph *graph,
3950 __isl_keep isl_vec *sol)
3952 int i;
3954 for (i = 0; i < graph->n; ++i) {
3955 struct isl_sched_node *node = &graph->node[i];
3956 int trivial;
3958 if (!needs_row(graph, node))
3959 continue;
3960 trivial = is_trivial(node, sol);
3961 if (trivial < 0 || trivial)
3962 return trivial;
3965 return 0;
3968 /* Does the schedule represented by "sol" perform loop coalescing on "node"?
3969 * If so, return the position of the coalesced dimension.
3970 * Otherwise, return node->nvar or -1 on error.
3972 * In particular, look for pairs of coefficients c_i and c_j such that
3973 * |c_j/c_i| >= size_i, i.e., |c_j| >= |c_i * size_i|.
3974 * If any such pair is found, then return i.
3975 * If size_i is infinity, then no check on c_i needs to be performed.
3977 static int find_node_coalescing(struct isl_sched_node *node,
3978 __isl_keep isl_vec *sol)
3980 int i, j;
3981 isl_int max;
3982 isl_vec *csol;
3984 if (node->nvar <= 1)
3985 return node->nvar;
3987 csol = extract_var_coef(node, sol);
3988 if (!csol)
3989 return -1;
3990 isl_int_init(max);
3991 for (i = 0; i < node->nvar; ++i) {
3992 isl_val *v;
3994 if (isl_int_is_zero(csol->el[i]))
3995 continue;
3996 v = isl_multi_val_get_val(node->sizes, i);
3997 if (!v)
3998 goto error;
3999 if (!isl_val_is_int(v)) {
4000 isl_val_free(v);
4001 continue;
4003 isl_int_mul(max, v->n, csol->el[i]);
4004 isl_val_free(v);
4006 for (j = 0; j < node->nvar; ++j) {
4007 if (j == i)
4008 continue;
4009 if (isl_int_abs_ge(csol->el[j], max))
4010 break;
4012 if (j < node->nvar)
4013 break;
4016 isl_int_clear(max);
4017 isl_vec_free(csol);
4018 return i;
4019 error:
4020 isl_int_clear(max);
4021 isl_vec_free(csol);
4022 return -1;
4025 /* Force the schedule coefficient at position "pos" of "node" to be zero
4026 * in "tl".
4027 * The coefficient is encoded as the difference between two non-negative
4028 * variables. Force these two variables to have the same value.
4030 static __isl_give isl_tab_lexmin *zero_out_node_coef(
4031 __isl_take isl_tab_lexmin *tl, struct isl_sched_node *node, int pos)
4033 int dim;
4034 isl_ctx *ctx;
4035 isl_vec *eq;
4037 ctx = isl_space_get_ctx(node->space);
4038 dim = isl_tab_lexmin_dim(tl);
4039 if (dim < 0)
4040 return isl_tab_lexmin_free(tl);
4041 eq = isl_vec_alloc(ctx, 1 + dim);
4042 eq = isl_vec_clr(eq);
4043 if (!eq)
4044 return isl_tab_lexmin_free(tl);
4046 pos = 1 + node_var_coef_offset(node) + 2 * pos;
4047 isl_int_set_si(eq->el[pos], 1);
4048 isl_int_set_si(eq->el[pos + 1], -1);
4049 tl = isl_tab_lexmin_add_eq(tl, eq->el);
4050 isl_vec_free(eq);
4052 return tl;
4055 /* Return the lexicographically smallest rational point in the basic set
4056 * from which "tl" was constructed, double checking that this input set
4057 * was not empty.
4059 static __isl_give isl_vec *non_empty_solution(__isl_keep isl_tab_lexmin *tl)
4061 isl_vec *sol;
4063 sol = isl_tab_lexmin_get_solution(tl);
4064 if (!sol)
4065 return NULL;
4066 if (sol->size == 0)
4067 isl_die(isl_vec_get_ctx(sol), isl_error_internal,
4068 "error in schedule construction",
4069 return isl_vec_free(sol));
4070 return sol;
4073 /* Does the solution "sol" of the LP problem constructed by setup_carry_lp
4074 * carry any of the "n_edge" groups of dependences?
4075 * The value in the first position is the sum of (1 - e_i) over all "n_edge"
4076 * edges, with 0 <= e_i <= 1 equal to 1 when the dependences represented
4077 * by the edge are carried by the solution.
4078 * If the sum of the (1 - e_i) is smaller than "n_edge" then at least
4079 * one of those is carried.
4081 * Note that despite the fact that the problem is solved using a rational
4082 * solver, the solution is guaranteed to be integral.
4083 * Specifically, the dependence distance lower bounds e_i (and therefore
4084 * also their sum) are integers. See Lemma 5 of [1].
4086 * Any potential denominator of the sum is cleared by this function.
4087 * The denominator is not relevant for any of the other elements
4088 * in the solution.
4090 * [1] P. Feautrier, Some Efficient Solutions to the Affine Scheduling
4091 * Problem, Part II: Multi-Dimensional Time.
4092 * In Intl. Journal of Parallel Programming, 1992.
4094 static int carries_dependences(__isl_keep isl_vec *sol, int n_edge)
4096 isl_int_divexact(sol->el[1], sol->el[1], sol->el[0]);
4097 isl_int_set_si(sol->el[0], 1);
4098 return isl_int_cmp_si(sol->el[1], n_edge) < 0;
4101 /* Return the lexicographically smallest rational point in "lp",
4102 * assuming that all variables are non-negative and performing some
4103 * additional sanity checks.
4104 * In particular, "lp" should not be empty by construction.
4105 * Double check that this is the case.
4106 * Also, check that dependences are carried for at least one of
4107 * the "n_edge" edges.
4109 * If the computed schedule performs loop coalescing on a given node,
4110 * i.e., if it is of the form
4112 * c_i i + c_j j + ...
4114 * with |c_j/c_i| >= size_i, then force the coefficient c_i to be zero
4115 * to cut out this solution. Repeat this process until no more loop
4116 * coalescing occurs or until no more dependences can be carried.
4117 * In the latter case, revert to the previously computed solution.
4119 static __isl_give isl_vec *non_neg_lexmin(struct isl_sched_graph *graph,
4120 __isl_take isl_basic_set *lp, int n_edge)
4122 int i, pos;
4123 isl_ctx *ctx;
4124 isl_tab_lexmin *tl;
4125 isl_vec *sol, *prev = NULL;
4126 int treat_coalescing;
4128 if (!lp)
4129 return NULL;
4130 ctx = isl_basic_set_get_ctx(lp);
4131 treat_coalescing = isl_options_get_schedule_treat_coalescing(ctx);
4132 tl = isl_tab_lexmin_from_basic_set(lp);
4134 do {
4135 sol = non_empty_solution(tl);
4136 if (!sol)
4137 goto error;
4139 if (!carries_dependences(sol, n_edge)) {
4140 if (!prev)
4141 isl_die(ctx, isl_error_unknown,
4142 "unable to carry dependences",
4143 goto error);
4144 isl_vec_free(sol);
4145 sol = prev;
4146 break;
4148 prev = isl_vec_free(prev);
4149 if (!treat_coalescing)
4150 break;
4151 for (i = 0; i < graph->n; ++i) {
4152 struct isl_sched_node *node = &graph->node[i];
4154 pos = find_node_coalescing(node, sol);
4155 if (pos < 0)
4156 goto error;
4157 if (pos < node->nvar)
4158 break;
4160 if (i < graph->n) {
4161 prev = sol;
4162 tl = zero_out_node_coef(tl, &graph->node[i], pos);
4164 } while (i < graph->n);
4166 isl_tab_lexmin_free(tl);
4168 return sol;
4169 error:
4170 isl_tab_lexmin_free(tl);
4171 isl_vec_free(prev);
4172 isl_vec_free(sol);
4173 return NULL;
4176 /* Construct a schedule row for each node such that as many validity dependences
4177 * as possible are carried and then continue with the next band.
4179 * If there are no validity dependences, then no dependence can be carried and
4180 * the procedure is guaranteed to fail. If there is more than one component,
4181 * then try computing a schedule on each component separately
4182 * to prevent or at least postpone this failure.
4184 * If the computed schedule row turns out to be trivial on one or
4185 * more nodes where it should not be trivial, then we throw it away
4186 * and try again on each component separately.
4188 * If there is only one component, then we accept the schedule row anyway,
4189 * but we do not consider it as a complete row and therefore do not
4190 * increment graph->n_row. Note that the ranks of the nodes that
4191 * do get a non-trivial schedule part will get updated regardless and
4192 * graph->maxvar is computed based on these ranks. The test for
4193 * whether more schedule rows are required in compute_schedule_wcc
4194 * is therefore not affected.
4196 * Insert a band corresponding to the schedule row at position "node"
4197 * of the schedule tree and continue with the construction of the schedule.
4198 * This insertion and the continued construction is performed by split_scaled
4199 * after optionally checking for non-trivial common divisors.
4201 static __isl_give isl_schedule_node *carry_dependences(
4202 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph)
4204 int n_edge;
4205 int trivial;
4206 isl_ctx *ctx;
4207 isl_vec *sol;
4208 isl_basic_set *lp;
4210 if (!node)
4211 return NULL;
4213 n_edge = count_carry_edges(graph);
4214 if (n_edge == 0 && graph->scc > 1)
4215 return compute_component_schedule(node, graph, 1);
4217 ctx = isl_schedule_node_get_ctx(node);
4218 if (setup_carry_lp(ctx, graph) < 0)
4219 return isl_schedule_node_free(node);
4221 lp = isl_basic_set_copy(graph->lp);
4222 sol = non_neg_lexmin(graph, lp, n_edge);
4223 if (!sol)
4224 return isl_schedule_node_free(node);
4226 trivial = is_any_trivial(graph, sol);
4227 if (trivial < 0) {
4228 sol = isl_vec_free(sol);
4229 } else if (trivial && graph->scc > 1) {
4230 isl_vec_free(sol);
4231 return compute_component_schedule(node, graph, 1);
4234 if (update_schedule(graph, sol, 0, 0) < 0)
4235 return isl_schedule_node_free(node);
4236 if (trivial)
4237 graph->n_row--;
4239 return split_scaled(node, graph);
4242 /* Topologically sort statements mapped to the same schedule iteration
4243 * and add insert a sequence node in front of "node"
4244 * corresponding to this order.
4245 * If "initialized" is set, then it may be assumed that compute_maxvar
4246 * has been called on the current band. Otherwise, call
4247 * compute_maxvar if and before carry_dependences gets called.
4249 * If it turns out to be impossible to sort the statements apart,
4250 * because different dependences impose different orderings
4251 * on the statements, then we extend the schedule such that
4252 * it carries at least one more dependence.
4254 static __isl_give isl_schedule_node *sort_statements(
4255 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph,
4256 int initialized)
4258 isl_ctx *ctx;
4259 isl_union_set_list *filters;
4261 if (!node)
4262 return NULL;
4264 ctx = isl_schedule_node_get_ctx(node);
4265 if (graph->n < 1)
4266 isl_die(ctx, isl_error_internal,
4267 "graph should have at least one node",
4268 return isl_schedule_node_free(node));
4270 if (graph->n == 1)
4271 return node;
4273 if (update_edges(ctx, graph) < 0)
4274 return isl_schedule_node_free(node);
4276 if (graph->n_edge == 0)
4277 return node;
4279 if (detect_sccs(ctx, graph) < 0)
4280 return isl_schedule_node_free(node);
4282 next_band(graph);
4283 if (graph->scc < graph->n) {
4284 if (!initialized && compute_maxvar(graph) < 0)
4285 return isl_schedule_node_free(node);
4286 return carry_dependences(node, graph);
4289 filters = extract_sccs(ctx, graph);
4290 node = isl_schedule_node_insert_sequence(node, filters);
4292 return node;
4295 /* Are there any (non-empty) (conditional) validity edges in the graph?
4297 static int has_validity_edges(struct isl_sched_graph *graph)
4299 int i;
4301 for (i = 0; i < graph->n_edge; ++i) {
4302 int empty;
4304 empty = isl_map_plain_is_empty(graph->edge[i].map);
4305 if (empty < 0)
4306 return -1;
4307 if (empty)
4308 continue;
4309 if (is_any_validity(&graph->edge[i]))
4310 return 1;
4313 return 0;
4316 /* Should we apply a Feautrier step?
4317 * That is, did the user request the Feautrier algorithm and are
4318 * there any validity dependences (left)?
4320 static int need_feautrier_step(isl_ctx *ctx, struct isl_sched_graph *graph)
4322 if (ctx->opt->schedule_algorithm != ISL_SCHEDULE_ALGORITHM_FEAUTRIER)
4323 return 0;
4325 return has_validity_edges(graph);
4328 /* Compute a schedule for a connected dependence graph using Feautrier's
4329 * multi-dimensional scheduling algorithm and return the updated schedule node.
4331 * The original algorithm is described in [1].
4332 * The main idea is to minimize the number of scheduling dimensions, by
4333 * trying to satisfy as many dependences as possible per scheduling dimension.
4335 * [1] P. Feautrier, Some Efficient Solutions to the Affine Scheduling
4336 * Problem, Part II: Multi-Dimensional Time.
4337 * In Intl. Journal of Parallel Programming, 1992.
4339 static __isl_give isl_schedule_node *compute_schedule_wcc_feautrier(
4340 isl_schedule_node *node, struct isl_sched_graph *graph)
4342 return carry_dependences(node, graph);
4345 /* Turn off the "local" bit on all (condition) edges.
4347 static void clear_local_edges(struct isl_sched_graph *graph)
4349 int i;
4351 for (i = 0; i < graph->n_edge; ++i)
4352 if (is_condition(&graph->edge[i]))
4353 clear_local(&graph->edge[i]);
4356 /* Does "graph" have both condition and conditional validity edges?
4358 static int need_condition_check(struct isl_sched_graph *graph)
4360 int i;
4361 int any_condition = 0;
4362 int any_conditional_validity = 0;
4364 for (i = 0; i < graph->n_edge; ++i) {
4365 if (is_condition(&graph->edge[i]))
4366 any_condition = 1;
4367 if (is_conditional_validity(&graph->edge[i]))
4368 any_conditional_validity = 1;
4371 return any_condition && any_conditional_validity;
4374 /* Does "graph" contain any coincidence edge?
4376 static int has_any_coincidence(struct isl_sched_graph *graph)
4378 int i;
4380 for (i = 0; i < graph->n_edge; ++i)
4381 if (is_coincidence(&graph->edge[i]))
4382 return 1;
4384 return 0;
4387 /* Extract the final schedule row as a map with the iteration domain
4388 * of "node" as domain.
4390 static __isl_give isl_map *final_row(struct isl_sched_node *node)
4392 isl_multi_aff *ma;
4393 int row;
4395 row = isl_mat_rows(node->sched) - 1;
4396 ma = node_extract_partial_schedule_multi_aff(node, row, 1);
4397 return isl_map_from_multi_aff(ma);
4400 /* Is the conditional validity dependence in the edge with index "edge_index"
4401 * violated by the latest (i.e., final) row of the schedule?
4402 * That is, is i scheduled after j
4403 * for any conditional validity dependence i -> j?
4405 static int is_violated(struct isl_sched_graph *graph, int edge_index)
4407 isl_map *src_sched, *dst_sched, *map;
4408 struct isl_sched_edge *edge = &graph->edge[edge_index];
4409 int empty;
4411 src_sched = final_row(edge->src);
4412 dst_sched = final_row(edge->dst);
4413 map = isl_map_copy(edge->map);
4414 map = isl_map_apply_domain(map, src_sched);
4415 map = isl_map_apply_range(map, dst_sched);
4416 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_out, 0);
4417 empty = isl_map_is_empty(map);
4418 isl_map_free(map);
4420 if (empty < 0)
4421 return -1;
4423 return !empty;
4426 /* Does "graph" have any satisfied condition edges that
4427 * are adjacent to the conditional validity constraint with
4428 * domain "conditional_source" and range "conditional_sink"?
4430 * A satisfied condition is one that is not local.
4431 * If a condition was forced to be local already (i.e., marked as local)
4432 * then there is no need to check if it is in fact local.
4434 * Additionally, mark all adjacent condition edges found as local.
4436 static int has_adjacent_true_conditions(struct isl_sched_graph *graph,
4437 __isl_keep isl_union_set *conditional_source,
4438 __isl_keep isl_union_set *conditional_sink)
4440 int i;
4441 int any = 0;
4443 for (i = 0; i < graph->n_edge; ++i) {
4444 int adjacent, local;
4445 isl_union_map *condition;
4447 if (!is_condition(&graph->edge[i]))
4448 continue;
4449 if (is_local(&graph->edge[i]))
4450 continue;
4452 condition = graph->edge[i].tagged_condition;
4453 adjacent = domain_intersects(condition, conditional_sink);
4454 if (adjacent >= 0 && !adjacent)
4455 adjacent = range_intersects(condition,
4456 conditional_source);
4457 if (adjacent < 0)
4458 return -1;
4459 if (!adjacent)
4460 continue;
4462 set_local(&graph->edge[i]);
4464 local = is_condition_false(&graph->edge[i]);
4465 if (local < 0)
4466 return -1;
4467 if (!local)
4468 any = 1;
4471 return any;
4474 /* Are there any violated conditional validity dependences with
4475 * adjacent condition dependences that are not local with respect
4476 * to the current schedule?
4477 * That is, is the conditional validity constraint violated?
4479 * Additionally, mark all those adjacent condition dependences as local.
4480 * We also mark those adjacent condition dependences that were not marked
4481 * as local before, but just happened to be local already. This ensures
4482 * that they remain local if the schedule is recomputed.
4484 * We first collect domain and range of all violated conditional validity
4485 * dependences and then check if there are any adjacent non-local
4486 * condition dependences.
4488 static int has_violated_conditional_constraint(isl_ctx *ctx,
4489 struct isl_sched_graph *graph)
4491 int i;
4492 int any = 0;
4493 isl_union_set *source, *sink;
4495 source = isl_union_set_empty(isl_space_params_alloc(ctx, 0));
4496 sink = isl_union_set_empty(isl_space_params_alloc(ctx, 0));
4497 for (i = 0; i < graph->n_edge; ++i) {
4498 isl_union_set *uset;
4499 isl_union_map *umap;
4500 int violated;
4502 if (!is_conditional_validity(&graph->edge[i]))
4503 continue;
4505 violated = is_violated(graph, i);
4506 if (violated < 0)
4507 goto error;
4508 if (!violated)
4509 continue;
4511 any = 1;
4513 umap = isl_union_map_copy(graph->edge[i].tagged_validity);
4514 uset = isl_union_map_domain(umap);
4515 source = isl_union_set_union(source, uset);
4516 source = isl_union_set_coalesce(source);
4518 umap = isl_union_map_copy(graph->edge[i].tagged_validity);
4519 uset = isl_union_map_range(umap);
4520 sink = isl_union_set_union(sink, uset);
4521 sink = isl_union_set_coalesce(sink);
4524 if (any)
4525 any = has_adjacent_true_conditions(graph, source, sink);
4527 isl_union_set_free(source);
4528 isl_union_set_free(sink);
4529 return any;
4530 error:
4531 isl_union_set_free(source);
4532 isl_union_set_free(sink);
4533 return -1;
4536 /* Examine the current band (the rows between graph->band_start and
4537 * graph->n_total_row), deciding whether to drop it or add it to "node"
4538 * and then continue with the computation of the next band, if any.
4539 * If "initialized" is set, then it may be assumed that compute_maxvar
4540 * has been called on the current band. Otherwise, call
4541 * compute_maxvar if and before carry_dependences gets called.
4543 * The caller keeps looking for a new row as long as
4544 * graph->n_row < graph->maxvar. If the latest attempt to find
4545 * such a row failed (i.e., we still have graph->n_row < graph->maxvar),
4546 * then we either
4547 * - split between SCCs and start over (assuming we found an interesting
4548 * pair of SCCs between which to split)
4549 * - continue with the next band (assuming the current band has at least
4550 * one row)
4551 * - try to carry as many dependences as possible and continue with the next
4552 * band
4553 * In each case, we first insert a band node in the schedule tree
4554 * if any rows have been computed.
4556 * If the caller managed to complete the schedule, we insert a band node
4557 * (if any schedule rows were computed) and we finish off by topologically
4558 * sorting the statements based on the remaining dependences.
4560 static __isl_give isl_schedule_node *compute_schedule_finish_band(
4561 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph,
4562 int initialized)
4564 int insert;
4566 if (!node)
4567 return NULL;
4569 if (graph->n_row < graph->maxvar) {
4570 isl_ctx *ctx;
4571 int empty = graph->n_total_row == graph->band_start;
4573 ctx = isl_schedule_node_get_ctx(node);
4574 if (!ctx->opt->schedule_maximize_band_depth && !empty)
4575 return compute_next_band(node, graph, 1);
4576 if (graph->src_scc >= 0)
4577 return compute_split_schedule(node, graph);
4578 if (!empty)
4579 return compute_next_band(node, graph, 1);
4580 if (!initialized && compute_maxvar(graph) < 0)
4581 return isl_schedule_node_free(node);
4582 return carry_dependences(node, graph);
4585 insert = graph->n_total_row > graph->band_start;
4586 if (insert) {
4587 node = insert_current_band(node, graph, 1);
4588 node = isl_schedule_node_child(node, 0);
4590 node = sort_statements(node, graph, initialized);
4591 if (insert)
4592 node = isl_schedule_node_parent(node);
4594 return node;
4597 /* Construct a band of schedule rows for a connected dependence graph.
4598 * The caller is responsible for determining the strongly connected
4599 * components and calling compute_maxvar first.
4601 * We try to find a sequence of as many schedule rows as possible that result
4602 * in non-negative dependence distances (independent of the previous rows
4603 * in the sequence, i.e., such that the sequence is tilable), with as
4604 * many of the initial rows as possible satisfying the coincidence constraints.
4605 * The computation stops if we can't find any more rows or if we have found
4606 * all the rows we wanted to find.
4608 * If ctx->opt->schedule_outer_coincidence is set, then we force the
4609 * outermost dimension to satisfy the coincidence constraints. If this
4610 * turns out to be impossible, we fall back on the general scheme above
4611 * and try to carry as many dependences as possible.
4613 * If "graph" contains both condition and conditional validity dependences,
4614 * then we need to check that that the conditional schedule constraint
4615 * is satisfied, i.e., there are no violated conditional validity dependences
4616 * that are adjacent to any non-local condition dependences.
4617 * If there are, then we mark all those adjacent condition dependences
4618 * as local and recompute the current band. Those dependences that
4619 * are marked local will then be forced to be local.
4620 * The initial computation is performed with no dependences marked as local.
4621 * If we are lucky, then there will be no violated conditional validity
4622 * dependences adjacent to any non-local condition dependences.
4623 * Otherwise, we mark some additional condition dependences as local and
4624 * recompute. We continue this process until there are no violations left or
4625 * until we are no longer able to compute a schedule.
4626 * Since there are only a finite number of dependences,
4627 * there will only be a finite number of iterations.
4629 static isl_stat compute_schedule_wcc_band(isl_ctx *ctx,
4630 struct isl_sched_graph *graph)
4632 int has_coincidence;
4633 int use_coincidence;
4634 int force_coincidence = 0;
4635 int check_conditional;
4637 if (sort_sccs(graph) < 0)
4638 return isl_stat_error;
4640 clear_local_edges(graph);
4641 check_conditional = need_condition_check(graph);
4642 has_coincidence = has_any_coincidence(graph);
4644 if (ctx->opt->schedule_outer_coincidence)
4645 force_coincidence = 1;
4647 use_coincidence = has_coincidence;
4648 while (graph->n_row < graph->maxvar) {
4649 isl_vec *sol;
4650 int violated;
4651 int coincident;
4653 graph->src_scc = -1;
4654 graph->dst_scc = -1;
4656 if (setup_lp(ctx, graph, use_coincidence) < 0)
4657 return isl_stat_error;
4658 sol = solve_lp(graph);
4659 if (!sol)
4660 return isl_stat_error;
4661 if (sol->size == 0) {
4662 int empty = graph->n_total_row == graph->band_start;
4664 isl_vec_free(sol);
4665 if (use_coincidence && (!force_coincidence || !empty)) {
4666 use_coincidence = 0;
4667 continue;
4669 return isl_stat_ok;
4671 coincident = !has_coincidence || use_coincidence;
4672 if (update_schedule(graph, sol, 1, coincident) < 0)
4673 return isl_stat_error;
4675 if (!check_conditional)
4676 continue;
4677 violated = has_violated_conditional_constraint(ctx, graph);
4678 if (violated < 0)
4679 return isl_stat_error;
4680 if (!violated)
4681 continue;
4682 if (reset_band(graph) < 0)
4683 return isl_stat_error;
4684 use_coincidence = has_coincidence;
4687 return isl_stat_ok;
4690 /* Compute a schedule for a connected dependence graph by considering
4691 * the graph as a whole and return the updated schedule node.
4693 * The actual schedule rows of the current band are computed by
4694 * compute_schedule_wcc_band. compute_schedule_finish_band takes
4695 * care of integrating the band into "node" and continuing
4696 * the computation.
4698 static __isl_give isl_schedule_node *compute_schedule_wcc_whole(
4699 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph)
4701 isl_ctx *ctx;
4703 if (!node)
4704 return NULL;
4706 ctx = isl_schedule_node_get_ctx(node);
4707 if (compute_schedule_wcc_band(ctx, graph) < 0)
4708 return isl_schedule_node_free(node);
4710 return compute_schedule_finish_band(node, graph, 1);
4713 /* Clustering information used by compute_schedule_wcc_clustering.
4715 * "n" is the number of SCCs in the original dependence graph
4716 * "scc" is an array of "n" elements, each representing an SCC
4717 * of the original dependence graph. All entries in the same cluster
4718 * have the same number of schedule rows.
4719 * "scc_cluster" maps each SCC index to the cluster to which it belongs,
4720 * where each cluster is represented by the index of the first SCC
4721 * in the cluster. Initially, each SCC belongs to a cluster containing
4722 * only that SCC.
4724 * "scc_in_merge" is used by merge_clusters_along_edge to keep
4725 * track of which SCCs need to be merged.
4727 * "cluster" contains the merged clusters of SCCs after the clustering
4728 * has completed.
4730 * "scc_node" is a temporary data structure used inside copy_partial.
4731 * For each SCC, it keeps track of the number of nodes in the SCC
4732 * that have already been copied.
4734 struct isl_clustering {
4735 int n;
4736 struct isl_sched_graph *scc;
4737 struct isl_sched_graph *cluster;
4738 int *scc_cluster;
4739 int *scc_node;
4740 int *scc_in_merge;
4743 /* Initialize the clustering data structure "c" from "graph".
4745 * In particular, allocate memory, extract the SCCs from "graph"
4746 * into c->scc, initialize scc_cluster and construct
4747 * a band of schedule rows for each SCC.
4748 * Within each SCC, there is only one SCC by definition.
4749 * Each SCC initially belongs to a cluster containing only that SCC.
4751 static isl_stat clustering_init(isl_ctx *ctx, struct isl_clustering *c,
4752 struct isl_sched_graph *graph)
4754 int i;
4756 c->n = graph->scc;
4757 c->scc = isl_calloc_array(ctx, struct isl_sched_graph, c->n);
4758 c->cluster = isl_calloc_array(ctx, struct isl_sched_graph, c->n);
4759 c->scc_cluster = isl_calloc_array(ctx, int, c->n);
4760 c->scc_node = isl_calloc_array(ctx, int, c->n);
4761 c->scc_in_merge = isl_calloc_array(ctx, int, c->n);
4762 if (!c->scc || !c->cluster ||
4763 !c->scc_cluster || !c->scc_node || !c->scc_in_merge)
4764 return isl_stat_error;
4766 for (i = 0; i < c->n; ++i) {
4767 if (extract_sub_graph(ctx, graph, &node_scc_exactly,
4768 &edge_scc_exactly, i, &c->scc[i]) < 0)
4769 return isl_stat_error;
4770 c->scc[i].scc = 1;
4771 if (compute_maxvar(&c->scc[i]) < 0)
4772 return isl_stat_error;
4773 if (compute_schedule_wcc_band(ctx, &c->scc[i]) < 0)
4774 return isl_stat_error;
4775 c->scc_cluster[i] = i;
4778 return isl_stat_ok;
4781 /* Free all memory allocated for "c".
4783 static void clustering_free(isl_ctx *ctx, struct isl_clustering *c)
4785 int i;
4787 if (c->scc)
4788 for (i = 0; i < c->n; ++i)
4789 graph_free(ctx, &c->scc[i]);
4790 free(c->scc);
4791 if (c->cluster)
4792 for (i = 0; i < c->n; ++i)
4793 graph_free(ctx, &c->cluster[i]);
4794 free(c->cluster);
4795 free(c->scc_cluster);
4796 free(c->scc_node);
4797 free(c->scc_in_merge);
4800 /* Should we refrain from merging the cluster in "graph" with
4801 * any other cluster?
4802 * In particular, is its current schedule band empty and incomplete.
4804 static int bad_cluster(struct isl_sched_graph *graph)
4806 return graph->n_row < graph->maxvar &&
4807 graph->n_total_row == graph->band_start;
4810 /* Return the index of an edge in "graph" that can be used to merge
4811 * two clusters in "c".
4812 * Return graph->n_edge if no such edge can be found.
4813 * Return -1 on error.
4815 * In particular, return a proximity edge between two clusters
4816 * that is not marked "no_merge" and such that neither of the
4817 * two clusters has an incomplete, empty band.
4819 * If there are multiple such edges, then try and find the most
4820 * appropriate edge to use for merging. In particular, pick the edge
4821 * with the greatest weight. If there are multiple of those,
4822 * then pick one with the shortest distance between
4823 * the two cluster representatives.
4825 static int find_proximity(struct isl_sched_graph *graph,
4826 struct isl_clustering *c)
4828 int i, best = graph->n_edge, best_dist, best_weight;
4830 for (i = 0; i < graph->n_edge; ++i) {
4831 struct isl_sched_edge *edge = &graph->edge[i];
4832 int dist, weight;
4834 if (!is_proximity(edge))
4835 continue;
4836 if (edge->no_merge)
4837 continue;
4838 if (bad_cluster(&c->scc[edge->src->scc]) ||
4839 bad_cluster(&c->scc[edge->dst->scc]))
4840 continue;
4841 dist = c->scc_cluster[edge->dst->scc] -
4842 c->scc_cluster[edge->src->scc];
4843 if (dist == 0)
4844 continue;
4845 weight = edge->weight;
4846 if (best < graph->n_edge) {
4847 if (best_weight > weight)
4848 continue;
4849 if (best_weight == weight && best_dist <= dist)
4850 continue;
4852 best = i;
4853 best_dist = dist;
4854 best_weight = weight;
4857 return best;
4860 /* Internal data structure used in mark_merge_sccs.
4862 * "graph" is the dependence graph in which a strongly connected
4863 * component is constructed.
4864 * "scc_cluster" maps each SCC index to the cluster to which it belongs.
4865 * "src" and "dst" are the indices of the nodes that are being merged.
4867 struct isl_mark_merge_sccs_data {
4868 struct isl_sched_graph *graph;
4869 int *scc_cluster;
4870 int src;
4871 int dst;
4874 /* Check whether the cluster containing node "i" depends on the cluster
4875 * containing node "j". If "i" and "j" belong to the same cluster,
4876 * then they are taken to depend on each other to ensure that
4877 * the resulting strongly connected component consists of complete
4878 * clusters. Furthermore, if "i" and "j" are the two nodes that
4879 * are being merged, then they are taken to depend on each other as well.
4880 * Otherwise, check if there is a (conditional) validity dependence
4881 * from node[j] to node[i], forcing node[i] to follow node[j].
4883 static isl_bool cluster_follows(int i, int j, void *user)
4885 struct isl_mark_merge_sccs_data *data = user;
4886 struct isl_sched_graph *graph = data->graph;
4887 int *scc_cluster = data->scc_cluster;
4889 if (data->src == i && data->dst == j)
4890 return isl_bool_true;
4891 if (data->src == j && data->dst == i)
4892 return isl_bool_true;
4893 if (scc_cluster[graph->node[i].scc] == scc_cluster[graph->node[j].scc])
4894 return isl_bool_true;
4896 return graph_has_validity_edge(graph, &graph->node[j], &graph->node[i]);
4899 /* Mark all SCCs that belong to either of the two clusters in "c"
4900 * connected by the edge in "graph" with index "edge", or to any
4901 * of the intermediate clusters.
4902 * The marking is recorded in c->scc_in_merge.
4904 * The given edge has been selected for merging two clusters,
4905 * meaning that there is at least a proximity edge between the two nodes.
4906 * However, there may also be (indirect) validity dependences
4907 * between the two nodes. When merging the two clusters, all clusters
4908 * containing one or more of the intermediate nodes along the
4909 * indirect validity dependences need to be merged in as well.
4911 * First collect all such nodes by computing the strongly connected
4912 * component (SCC) containing the two nodes connected by the edge, where
4913 * the two nodes are considered to depend on each other to make
4914 * sure they end up in the same SCC. Similarly, each node is considered
4915 * to depend on every other node in the same cluster to ensure
4916 * that the SCC consists of complete clusters.
4918 * Then the original SCCs that contain any of these nodes are marked
4919 * in c->scc_in_merge.
4921 static isl_stat mark_merge_sccs(isl_ctx *ctx, struct isl_sched_graph *graph,
4922 int edge, struct isl_clustering *c)
4924 struct isl_mark_merge_sccs_data data;
4925 struct isl_tarjan_graph *g;
4926 int i;
4928 for (i = 0; i < c->n; ++i)
4929 c->scc_in_merge[i] = 0;
4931 data.graph = graph;
4932 data.scc_cluster = c->scc_cluster;
4933 data.src = graph->edge[edge].src - graph->node;
4934 data.dst = graph->edge[edge].dst - graph->node;
4936 g = isl_tarjan_graph_component(ctx, graph->n, data.dst,
4937 &cluster_follows, &data);
4938 if (!g)
4939 goto error;
4941 i = g->op;
4942 if (i < 3)
4943 isl_die(ctx, isl_error_internal,
4944 "expecting at least two nodes in component",
4945 goto error);
4946 if (g->order[--i] != -1)
4947 isl_die(ctx, isl_error_internal,
4948 "expecting end of component marker", goto error);
4950 for (--i; i >= 0 && g->order[i] != -1; --i) {
4951 int scc = graph->node[g->order[i]].scc;
4952 c->scc_in_merge[scc] = 1;
4955 isl_tarjan_graph_free(g);
4956 return isl_stat_ok;
4957 error:
4958 isl_tarjan_graph_free(g);
4959 return isl_stat_error;
4962 /* Construct the identifier "cluster_i".
4964 static __isl_give isl_id *cluster_id(isl_ctx *ctx, int i)
4966 char name[40];
4968 snprintf(name, sizeof(name), "cluster_%d", i);
4969 return isl_id_alloc(ctx, name, NULL);
4972 /* Construct the space of the cluster with index "i" containing
4973 * the strongly connected component "scc".
4975 * In particular, construct a space called cluster_i with dimension equal
4976 * to the number of schedule rows in the current band of "scc".
4978 static __isl_give isl_space *cluster_space(struct isl_sched_graph *scc, int i)
4980 int nvar;
4981 isl_space *space;
4982 isl_id *id;
4984 nvar = scc->n_total_row - scc->band_start;
4985 space = isl_space_copy(scc->node[0].space);
4986 space = isl_space_params(space);
4987 space = isl_space_set_from_params(space);
4988 space = isl_space_add_dims(space, isl_dim_set, nvar);
4989 id = cluster_id(isl_space_get_ctx(space), i);
4990 space = isl_space_set_tuple_id(space, isl_dim_set, id);
4992 return space;
4995 /* Collect the domain of the graph for merging clusters.
4997 * In particular, for each cluster with first SCC "i", construct
4998 * a set in the space called cluster_i with dimension equal
4999 * to the number of schedule rows in the current band of the cluster.
5001 static __isl_give isl_union_set *collect_domain(isl_ctx *ctx,
5002 struct isl_sched_graph *graph, struct isl_clustering *c)
5004 int i;
5005 isl_space *space;
5006 isl_union_set *domain;
5008 space = isl_space_params_alloc(ctx, 0);
5009 domain = isl_union_set_empty(space);
5011 for (i = 0; i < graph->scc; ++i) {
5012 isl_space *space;
5014 if (!c->scc_in_merge[i])
5015 continue;
5016 if (c->scc_cluster[i] != i)
5017 continue;
5018 space = cluster_space(&c->scc[i], i);
5019 domain = isl_union_set_add_set(domain, isl_set_universe(space));
5022 return domain;
5025 /* Construct a map from the original instances to the corresponding
5026 * cluster instance in the current bands of the clusters in "c".
5028 static __isl_give isl_union_map *collect_cluster_map(isl_ctx *ctx,
5029 struct isl_sched_graph *graph, struct isl_clustering *c)
5031 int i, j;
5032 isl_space *space;
5033 isl_union_map *cluster_map;
5035 space = isl_space_params_alloc(ctx, 0);
5036 cluster_map = isl_union_map_empty(space);
5037 for (i = 0; i < graph->scc; ++i) {
5038 int start, n;
5039 isl_id *id;
5041 if (!c->scc_in_merge[i])
5042 continue;
5044 id = cluster_id(ctx, c->scc_cluster[i]);
5045 start = c->scc[i].band_start;
5046 n = c->scc[i].n_total_row - start;
5047 for (j = 0; j < c->scc[i].n; ++j) {
5048 isl_multi_aff *ma;
5049 isl_map *map;
5050 struct isl_sched_node *node = &c->scc[i].node[j];
5052 ma = node_extract_partial_schedule_multi_aff(node,
5053 start, n);
5054 ma = isl_multi_aff_set_tuple_id(ma, isl_dim_out,
5055 isl_id_copy(id));
5056 map = isl_map_from_multi_aff(ma);
5057 cluster_map = isl_union_map_add_map(cluster_map, map);
5059 isl_id_free(id);
5062 return cluster_map;
5065 /* Add "umap" to the schedule constraints "sc" of all types of "edge"
5066 * that are not isl_edge_condition or isl_edge_conditional_validity.
5068 static __isl_give isl_schedule_constraints *add_non_conditional_constraints(
5069 struct isl_sched_edge *edge, __isl_keep isl_union_map *umap,
5070 __isl_take isl_schedule_constraints *sc)
5072 enum isl_edge_type t;
5074 if (!sc)
5075 return NULL;
5077 for (t = isl_edge_first; t <= isl_edge_last; ++t) {
5078 if (t == isl_edge_condition ||
5079 t == isl_edge_conditional_validity)
5080 continue;
5081 if (!is_type(edge, t))
5082 continue;
5083 sc = isl_schedule_constraints_add(sc, t,
5084 isl_union_map_copy(umap));
5087 return sc;
5090 /* Add schedule constraints of types isl_edge_condition and
5091 * isl_edge_conditional_validity to "sc" by applying "umap" to
5092 * the domains of the wrapped relations in domain and range
5093 * of the corresponding tagged constraints of "edge".
5095 static __isl_give isl_schedule_constraints *add_conditional_constraints(
5096 struct isl_sched_edge *edge, __isl_keep isl_union_map *umap,
5097 __isl_take isl_schedule_constraints *sc)
5099 enum isl_edge_type t;
5100 isl_union_map *tagged;
5102 for (t = isl_edge_condition; t <= isl_edge_conditional_validity; ++t) {
5103 if (!is_type(edge, t))
5104 continue;
5105 if (t == isl_edge_condition)
5106 tagged = isl_union_map_copy(edge->tagged_condition);
5107 else
5108 tagged = isl_union_map_copy(edge->tagged_validity);
5109 tagged = isl_union_map_zip(tagged);
5110 tagged = isl_union_map_apply_domain(tagged,
5111 isl_union_map_copy(umap));
5112 tagged = isl_union_map_zip(tagged);
5113 sc = isl_schedule_constraints_add(sc, t, tagged);
5114 if (!sc)
5115 return NULL;
5118 return sc;
5121 /* Given a mapping "cluster_map" from the original instances to
5122 * the cluster instances, add schedule constraints on the clusters
5123 * to "sc" corresponding to the original constraints represented by "edge".
5125 * For non-tagged dependence constraints, the cluster constraints
5126 * are obtained by applying "cluster_map" to the edge->map.
5128 * For tagged dependence constraints, "cluster_map" needs to be applied
5129 * to the domains of the wrapped relations in domain and range
5130 * of the tagged dependence constraints. Pick out the mappings
5131 * from these domains from "cluster_map" and construct their product.
5132 * This mapping can then be applied to the pair of domains.
5134 static __isl_give isl_schedule_constraints *collect_edge_constraints(
5135 struct isl_sched_edge *edge, __isl_keep isl_union_map *cluster_map,
5136 __isl_take isl_schedule_constraints *sc)
5138 isl_union_map *umap;
5139 isl_space *space;
5140 isl_union_set *uset;
5141 isl_union_map *umap1, *umap2;
5143 if (!sc)
5144 return NULL;
5146 umap = isl_union_map_from_map(isl_map_copy(edge->map));
5147 umap = isl_union_map_apply_domain(umap,
5148 isl_union_map_copy(cluster_map));
5149 umap = isl_union_map_apply_range(umap,
5150 isl_union_map_copy(cluster_map));
5151 sc = add_non_conditional_constraints(edge, umap, sc);
5152 isl_union_map_free(umap);
5154 if (!sc || (!is_condition(edge) && !is_conditional_validity(edge)))
5155 return sc;
5157 space = isl_space_domain(isl_map_get_space(edge->map));
5158 uset = isl_union_set_from_set(isl_set_universe(space));
5159 umap1 = isl_union_map_copy(cluster_map);
5160 umap1 = isl_union_map_intersect_domain(umap1, uset);
5161 space = isl_space_range(isl_map_get_space(edge->map));
5162 uset = isl_union_set_from_set(isl_set_universe(space));
5163 umap2 = isl_union_map_copy(cluster_map);
5164 umap2 = isl_union_map_intersect_domain(umap2, uset);
5165 umap = isl_union_map_product(umap1, umap2);
5167 sc = add_conditional_constraints(edge, umap, sc);
5169 isl_union_map_free(umap);
5170 return sc;
5173 /* Given a mapping "cluster_map" from the original instances to
5174 * the cluster instances, add schedule constraints on the clusters
5175 * to "sc" corresponding to all edges in "graph" between nodes that
5176 * belong to SCCs that are marked for merging in "scc_in_merge".
5178 static __isl_give isl_schedule_constraints *collect_constraints(
5179 struct isl_sched_graph *graph, int *scc_in_merge,
5180 __isl_keep isl_union_map *cluster_map,
5181 __isl_take isl_schedule_constraints *sc)
5183 int i;
5185 for (i = 0; i < graph->n_edge; ++i) {
5186 struct isl_sched_edge *edge = &graph->edge[i];
5188 if (!scc_in_merge[edge->src->scc])
5189 continue;
5190 if (!scc_in_merge[edge->dst->scc])
5191 continue;
5192 sc = collect_edge_constraints(edge, cluster_map, sc);
5195 return sc;
5198 /* Construct a dependence graph for scheduling clusters with respect
5199 * to each other and store the result in "merge_graph".
5200 * In particular, the nodes of the graph correspond to the schedule
5201 * dimensions of the current bands of those clusters that have been
5202 * marked for merging in "c".
5204 * First construct an isl_schedule_constraints object for this domain
5205 * by transforming the edges in "graph" to the domain.
5206 * Then initialize a dependence graph for scheduling from these
5207 * constraints.
5209 static isl_stat init_merge_graph(isl_ctx *ctx, struct isl_sched_graph *graph,
5210 struct isl_clustering *c, struct isl_sched_graph *merge_graph)
5212 isl_union_set *domain;
5213 isl_union_map *cluster_map;
5214 isl_schedule_constraints *sc;
5215 isl_stat r;
5217 domain = collect_domain(ctx, graph, c);
5218 sc = isl_schedule_constraints_on_domain(domain);
5219 if (!sc)
5220 return isl_stat_error;
5221 cluster_map = collect_cluster_map(ctx, graph, c);
5222 sc = collect_constraints(graph, c->scc_in_merge, cluster_map, sc);
5223 isl_union_map_free(cluster_map);
5225 r = graph_init(merge_graph, sc);
5227 isl_schedule_constraints_free(sc);
5229 return r;
5232 /* Compute the maximal number of remaining schedule rows that still need
5233 * to be computed for the nodes that belong to clusters with the maximal
5234 * dimension for the current band (i.e., the band that is to be merged).
5235 * Only clusters that are about to be merged are considered.
5236 * "maxvar" is the maximal dimension for the current band.
5237 * "c" contains information about the clusters.
5239 * Return the maximal number of remaining schedule rows or -1 on error.
5241 static int compute_maxvar_max_slack(int maxvar, struct isl_clustering *c)
5243 int i, j;
5244 int max_slack;
5246 max_slack = 0;
5247 for (i = 0; i < c->n; ++i) {
5248 int nvar;
5249 struct isl_sched_graph *scc;
5251 if (!c->scc_in_merge[i])
5252 continue;
5253 scc = &c->scc[i];
5254 nvar = scc->n_total_row - scc->band_start;
5255 if (nvar != maxvar)
5256 continue;
5257 for (j = 0; j < scc->n; ++j) {
5258 struct isl_sched_node *node = &scc->node[j];
5259 int slack;
5261 if (node_update_cmap(node) < 0)
5262 return -1;
5263 slack = node->nvar - node->rank;
5264 if (slack > max_slack)
5265 max_slack = slack;
5269 return max_slack;
5272 /* If there are any clusters where the dimension of the current band
5273 * (i.e., the band that is to be merged) is smaller than "maxvar" and
5274 * if there are any nodes in such a cluster where the number
5275 * of remaining schedule rows that still need to be computed
5276 * is greater than "max_slack", then return the smallest current band
5277 * dimension of all these clusters. Otherwise return the original value
5278 * of "maxvar". Return -1 in case of any error.
5279 * Only clusters that are about to be merged are considered.
5280 * "c" contains information about the clusters.
5282 static int limit_maxvar_to_slack(int maxvar, int max_slack,
5283 struct isl_clustering *c)
5285 int i, j;
5287 for (i = 0; i < c->n; ++i) {
5288 int nvar;
5289 struct isl_sched_graph *scc;
5291 if (!c->scc_in_merge[i])
5292 continue;
5293 scc = &c->scc[i];
5294 nvar = scc->n_total_row - scc->band_start;
5295 if (nvar >= maxvar)
5296 continue;
5297 for (j = 0; j < scc->n; ++j) {
5298 struct isl_sched_node *node = &scc->node[j];
5299 int slack;
5301 if (node_update_cmap(node) < 0)
5302 return -1;
5303 slack = node->nvar - node->rank;
5304 if (slack > max_slack) {
5305 maxvar = nvar;
5306 break;
5311 return maxvar;
5314 /* Adjust merge_graph->maxvar based on the number of remaining schedule rows
5315 * that still need to be computed. In particular, if there is a node
5316 * in a cluster where the dimension of the current band is smaller
5317 * than merge_graph->maxvar, but the number of remaining schedule rows
5318 * is greater than that of any node in a cluster with the maximal
5319 * dimension for the current band (i.e., merge_graph->maxvar),
5320 * then adjust merge_graph->maxvar to the (smallest) current band dimension
5321 * of those clusters. Without this adjustment, the total number of
5322 * schedule dimensions would be increased, resulting in a skewed view
5323 * of the number of coincident dimensions.
5324 * "c" contains information about the clusters.
5326 * If the maximize_band_depth option is set and merge_graph->maxvar is reduced,
5327 * then there is no point in attempting any merge since it will be rejected
5328 * anyway. Set merge_graph->maxvar to zero in such cases.
5330 static isl_stat adjust_maxvar_to_slack(isl_ctx *ctx,
5331 struct isl_sched_graph *merge_graph, struct isl_clustering *c)
5333 int max_slack, maxvar;
5335 max_slack = compute_maxvar_max_slack(merge_graph->maxvar, c);
5336 if (max_slack < 0)
5337 return isl_stat_error;
5338 maxvar = limit_maxvar_to_slack(merge_graph->maxvar, max_slack, c);
5339 if (maxvar < 0)
5340 return isl_stat_error;
5342 if (maxvar < merge_graph->maxvar) {
5343 if (isl_options_get_schedule_maximize_band_depth(ctx))
5344 merge_graph->maxvar = 0;
5345 else
5346 merge_graph->maxvar = maxvar;
5349 return isl_stat_ok;
5352 /* Return the number of coincident dimensions in the current band of "graph",
5353 * where the nodes of "graph" are assumed to be scheduled by a single band.
5355 static int get_n_coincident(struct isl_sched_graph *graph)
5357 int i;
5359 for (i = graph->band_start; i < graph->n_total_row; ++i)
5360 if (!graph->node[0].coincident[i])
5361 break;
5363 return i - graph->band_start;
5366 /* Should the clusters be merged based on the cluster schedule
5367 * in the current (and only) band of "merge_graph", given that
5368 * coincidence should be maximized?
5370 * If the number of coincident schedule dimensions in the merged band
5371 * would be less than the maximal number of coincident schedule dimensions
5372 * in any of the merged clusters, then the clusters should not be merged.
5374 static isl_bool ok_to_merge_coincident(struct isl_clustering *c,
5375 struct isl_sched_graph *merge_graph)
5377 int i;
5378 int n_coincident;
5379 int max_coincident;
5381 max_coincident = 0;
5382 for (i = 0; i < c->n; ++i) {
5383 if (!c->scc_in_merge[i])
5384 continue;
5385 n_coincident = get_n_coincident(&c->scc[i]);
5386 if (n_coincident > max_coincident)
5387 max_coincident = n_coincident;
5390 n_coincident = get_n_coincident(merge_graph);
5392 return n_coincident >= max_coincident;
5395 /* Return the transformation on "node" expressed by the current (and only)
5396 * band of "merge_graph" applied to the clusters in "c".
5398 * First find the representation of "node" in its SCC in "c" and
5399 * extract the transformation expressed by the current band.
5400 * Then extract the transformation applied by "merge_graph"
5401 * to the cluster to which this SCC belongs.
5402 * Combine the two to obtain the complete transformation on the node.
5404 * Note that the range of the first transformation is an anonymous space,
5405 * while the domain of the second is named "cluster_X". The range
5406 * of the former therefore needs to be adjusted before the two
5407 * can be combined.
5409 static __isl_give isl_map *extract_node_transformation(isl_ctx *ctx,
5410 struct isl_sched_node *node, struct isl_clustering *c,
5411 struct isl_sched_graph *merge_graph)
5413 struct isl_sched_node *scc_node, *cluster_node;
5414 int start, n;
5415 isl_id *id;
5416 isl_space *space;
5417 isl_multi_aff *ma, *ma2;
5419 scc_node = graph_find_node(ctx, &c->scc[node->scc], node->space);
5420 start = c->scc[node->scc].band_start;
5421 n = c->scc[node->scc].n_total_row - start;
5422 ma = node_extract_partial_schedule_multi_aff(scc_node, start, n);
5423 space = cluster_space(&c->scc[node->scc], c->scc_cluster[node->scc]);
5424 cluster_node = graph_find_node(ctx, merge_graph, space);
5425 if (space && !cluster_node)
5426 isl_die(ctx, isl_error_internal, "unable to find cluster",
5427 space = isl_space_free(space));
5428 id = isl_space_get_tuple_id(space, isl_dim_set);
5429 ma = isl_multi_aff_set_tuple_id(ma, isl_dim_out, id);
5430 isl_space_free(space);
5431 n = merge_graph->n_total_row;
5432 ma2 = node_extract_partial_schedule_multi_aff(cluster_node, 0, n);
5433 ma = isl_multi_aff_pullback_multi_aff(ma2, ma);
5435 return isl_map_from_multi_aff(ma);
5438 /* Give a set of distances "set", are they bounded by a small constant
5439 * in direction "pos"?
5440 * In practice, check if they are bounded by 2 by checking that there
5441 * are no elements with a value greater than or equal to 3 or
5442 * smaller than or equal to -3.
5444 static isl_bool distance_is_bounded(__isl_keep isl_set *set, int pos)
5446 isl_bool bounded;
5447 isl_set *test;
5449 if (!set)
5450 return isl_bool_error;
5452 test = isl_set_copy(set);
5453 test = isl_set_lower_bound_si(test, isl_dim_set, pos, 3);
5454 bounded = isl_set_is_empty(test);
5455 isl_set_free(test);
5457 if (bounded < 0 || !bounded)
5458 return bounded;
5460 test = isl_set_copy(set);
5461 test = isl_set_upper_bound_si(test, isl_dim_set, pos, -3);
5462 bounded = isl_set_is_empty(test);
5463 isl_set_free(test);
5465 return bounded;
5468 /* Does the set "set" have a fixed (but possible parametric) value
5469 * at dimension "pos"?
5471 static isl_bool has_single_value(__isl_keep isl_set *set, int pos)
5473 int n;
5474 isl_bool single;
5476 if (!set)
5477 return isl_bool_error;
5478 set = isl_set_copy(set);
5479 n = isl_set_dim(set, isl_dim_set);
5480 set = isl_set_project_out(set, isl_dim_set, pos + 1, n - (pos + 1));
5481 set = isl_set_project_out(set, isl_dim_set, 0, pos);
5482 single = isl_set_is_singleton(set);
5483 isl_set_free(set);
5485 return single;
5488 /* Does "map" have a fixed (but possible parametric) value
5489 * at dimension "pos" of either its domain or its range?
5491 static isl_bool has_singular_src_or_dst(__isl_keep isl_map *map, int pos)
5493 isl_set *set;
5494 isl_bool single;
5496 set = isl_map_domain(isl_map_copy(map));
5497 single = has_single_value(set, pos);
5498 isl_set_free(set);
5500 if (single < 0 || single)
5501 return single;
5503 set = isl_map_range(isl_map_copy(map));
5504 single = has_single_value(set, pos);
5505 isl_set_free(set);
5507 return single;
5510 /* Does the edge "edge" from "graph" have bounded dependence distances
5511 * in the merged graph "merge_graph" of a selection of clusters in "c"?
5513 * Extract the complete transformations of the source and destination
5514 * nodes of the edge, apply them to the edge constraints and
5515 * compute the differences. Finally, check if these differences are bounded
5516 * in each direction.
5518 * If the dimension of the band is greater than the number of
5519 * dimensions that can be expected to be optimized by the edge
5520 * (based on its weight), then also allow the differences to be unbounded
5521 * in the remaining dimensions, but only if either the source or
5522 * the destination has a fixed value in that direction.
5523 * This allows a statement that produces values that are used by
5524 * several instances of another statement to be merged with that
5525 * other statement.
5526 * However, merging such clusters will introduce an inherently
5527 * large proximity distance inside the merged cluster, meaning
5528 * that proximity distances will no longer be optimized in
5529 * subsequent merges. These merges are therefore only allowed
5530 * after all other possible merges have been tried.
5531 * The first time such a merge is encountered, the weight of the edge
5532 * is replaced by a negative weight. The second time (i.e., after
5533 * all merges over edges with a non-negative weight have been tried),
5534 * the merge is allowed.
5536 static isl_bool has_bounded_distances(isl_ctx *ctx, struct isl_sched_edge *edge,
5537 struct isl_sched_graph *graph, struct isl_clustering *c,
5538 struct isl_sched_graph *merge_graph)
5540 int i, n, n_slack;
5541 isl_bool bounded;
5542 isl_map *map, *t;
5543 isl_set *dist;
5545 map = isl_map_copy(edge->map);
5546 t = extract_node_transformation(ctx, edge->src, c, merge_graph);
5547 map = isl_map_apply_domain(map, t);
5548 t = extract_node_transformation(ctx, edge->dst, c, merge_graph);
5549 map = isl_map_apply_range(map, t);
5550 dist = isl_map_deltas(isl_map_copy(map));
5552 bounded = isl_bool_true;
5553 n = isl_set_dim(dist, isl_dim_set);
5554 n_slack = n - edge->weight;
5555 if (edge->weight < 0)
5556 n_slack -= graph->max_weight + 1;
5557 for (i = 0; i < n; ++i) {
5558 isl_bool bounded_i, singular_i;
5560 bounded_i = distance_is_bounded(dist, i);
5561 if (bounded_i < 0)
5562 goto error;
5563 if (bounded_i)
5564 continue;
5565 if (edge->weight >= 0)
5566 bounded = isl_bool_false;
5567 n_slack--;
5568 if (n_slack < 0)
5569 break;
5570 singular_i = has_singular_src_or_dst(map, i);
5571 if (singular_i < 0)
5572 goto error;
5573 if (singular_i)
5574 continue;
5575 bounded = isl_bool_false;
5576 break;
5578 if (!bounded && i >= n && edge->weight >= 0)
5579 edge->weight -= graph->max_weight + 1;
5580 isl_map_free(map);
5581 isl_set_free(dist);
5583 return bounded;
5584 error:
5585 isl_map_free(map);
5586 isl_set_free(dist);
5587 return isl_bool_error;
5590 /* Should the clusters be merged based on the cluster schedule
5591 * in the current (and only) band of "merge_graph"?
5592 * "graph" is the original dependence graph, while "c" records
5593 * which SCCs are involved in the latest merge.
5595 * In particular, is there at least one proximity constraint
5596 * that is optimized by the merge?
5598 * A proximity constraint is considered to be optimized
5599 * if the dependence distances are small.
5601 static isl_bool ok_to_merge_proximity(isl_ctx *ctx,
5602 struct isl_sched_graph *graph, struct isl_clustering *c,
5603 struct isl_sched_graph *merge_graph)
5605 int i;
5607 for (i = 0; i < graph->n_edge; ++i) {
5608 struct isl_sched_edge *edge = &graph->edge[i];
5609 isl_bool bounded;
5611 if (!is_proximity(edge))
5612 continue;
5613 if (!c->scc_in_merge[edge->src->scc])
5614 continue;
5615 if (!c->scc_in_merge[edge->dst->scc])
5616 continue;
5617 if (c->scc_cluster[edge->dst->scc] ==
5618 c->scc_cluster[edge->src->scc])
5619 continue;
5620 bounded = has_bounded_distances(ctx, edge, graph, c,
5621 merge_graph);
5622 if (bounded < 0 || bounded)
5623 return bounded;
5626 return isl_bool_false;
5629 /* Should the clusters be merged based on the cluster schedule
5630 * in the current (and only) band of "merge_graph"?
5631 * "graph" is the original dependence graph, while "c" records
5632 * which SCCs are involved in the latest merge.
5634 * If the current band is empty, then the clusters should not be merged.
5636 * If the band depth should be maximized and the merge schedule
5637 * is incomplete (meaning that the dimension of some of the schedule
5638 * bands in the original schedule will be reduced), then the clusters
5639 * should not be merged.
5641 * If the schedule_maximize_coincidence option is set, then check that
5642 * the number of coincident schedule dimensions is not reduced.
5644 * Finally, only allow the merge if at least one proximity
5645 * constraint is optimized.
5647 static isl_bool ok_to_merge(isl_ctx *ctx, struct isl_sched_graph *graph,
5648 struct isl_clustering *c, struct isl_sched_graph *merge_graph)
5650 if (merge_graph->n_total_row == merge_graph->band_start)
5651 return isl_bool_false;
5653 if (isl_options_get_schedule_maximize_band_depth(ctx) &&
5654 merge_graph->n_total_row < merge_graph->maxvar)
5655 return isl_bool_false;
5657 if (isl_options_get_schedule_maximize_coincidence(ctx)) {
5658 isl_bool ok;
5660 ok = ok_to_merge_coincident(c, merge_graph);
5661 if (ok < 0 || !ok)
5662 return ok;
5665 return ok_to_merge_proximity(ctx, graph, c, merge_graph);
5668 /* Apply the schedule in "t_node" to the "n" rows starting at "first"
5669 * of the schedule in "node" and return the result.
5671 * That is, essentially compute
5673 * T * N(first:first+n-1)
5675 * taking into account the constant term and the parameter coefficients
5676 * in "t_node".
5678 static __isl_give isl_mat *node_transformation(isl_ctx *ctx,
5679 struct isl_sched_node *t_node, struct isl_sched_node *node,
5680 int first, int n)
5682 int i, j;
5683 isl_mat *t;
5684 int n_row, n_col, n_param, n_var;
5686 n_param = node->nparam;
5687 n_var = node->nvar;
5688 n_row = isl_mat_rows(t_node->sched);
5689 n_col = isl_mat_cols(node->sched);
5690 t = isl_mat_alloc(ctx, n_row, n_col);
5691 if (!t)
5692 return NULL;
5693 for (i = 0; i < n_row; ++i) {
5694 isl_seq_cpy(t->row[i], t_node->sched->row[i], 1 + n_param);
5695 isl_seq_clr(t->row[i] + 1 + n_param, n_var);
5696 for (j = 0; j < n; ++j)
5697 isl_seq_addmul(t->row[i],
5698 t_node->sched->row[i][1 + n_param + j],
5699 node->sched->row[first + j],
5700 1 + n_param + n_var);
5702 return t;
5705 /* Apply the cluster schedule in "t_node" to the current band
5706 * schedule of the nodes in "graph".
5708 * In particular, replace the rows starting at band_start
5709 * by the result of applying the cluster schedule in "t_node"
5710 * to the original rows.
5712 * The coincidence of the schedule is determined by the coincidence
5713 * of the cluster schedule.
5715 static isl_stat transform(isl_ctx *ctx, struct isl_sched_graph *graph,
5716 struct isl_sched_node *t_node)
5718 int i, j;
5719 int n_new;
5720 int start, n;
5722 start = graph->band_start;
5723 n = graph->n_total_row - start;
5725 n_new = isl_mat_rows(t_node->sched);
5726 for (i = 0; i < graph->n; ++i) {
5727 struct isl_sched_node *node = &graph->node[i];
5728 isl_mat *t;
5730 t = node_transformation(ctx, t_node, node, start, n);
5731 node->sched = isl_mat_drop_rows(node->sched, start, n);
5732 node->sched = isl_mat_concat(node->sched, t);
5733 node->sched_map = isl_map_free(node->sched_map);
5734 if (!node->sched)
5735 return isl_stat_error;
5736 for (j = 0; j < n_new; ++j)
5737 node->coincident[start + j] = t_node->coincident[j];
5739 graph->n_total_row -= n;
5740 graph->n_row -= n;
5741 graph->n_total_row += n_new;
5742 graph->n_row += n_new;
5744 return isl_stat_ok;
5747 /* Merge the clusters marked for merging in "c" into a single
5748 * cluster using the cluster schedule in the current band of "merge_graph".
5749 * The representative SCC for the new cluster is the SCC with
5750 * the smallest index.
5752 * The current band schedule of each SCC in the new cluster is obtained
5753 * by applying the schedule of the corresponding original cluster
5754 * to the original band schedule.
5755 * All SCCs in the new cluster have the same number of schedule rows.
5757 static isl_stat merge(isl_ctx *ctx, struct isl_clustering *c,
5758 struct isl_sched_graph *merge_graph)
5760 int i;
5761 int cluster = -1;
5762 isl_space *space;
5764 for (i = 0; i < c->n; ++i) {
5765 struct isl_sched_node *node;
5767 if (!c->scc_in_merge[i])
5768 continue;
5769 if (cluster < 0)
5770 cluster = i;
5771 space = cluster_space(&c->scc[i], c->scc_cluster[i]);
5772 if (!space)
5773 return isl_stat_error;
5774 node = graph_find_node(ctx, merge_graph, space);
5775 isl_space_free(space);
5776 if (!node)
5777 isl_die(ctx, isl_error_internal,
5778 "unable to find cluster",
5779 return isl_stat_error);
5780 if (transform(ctx, &c->scc[i], node) < 0)
5781 return isl_stat_error;
5782 c->scc_cluster[i] = cluster;
5785 return isl_stat_ok;
5788 /* Try and merge the clusters of SCCs marked in c->scc_in_merge
5789 * by scheduling the current cluster bands with respect to each other.
5791 * Construct a dependence graph with a space for each cluster and
5792 * with the coordinates of each space corresponding to the schedule
5793 * dimensions of the current band of that cluster.
5794 * Construct a cluster schedule in this cluster dependence graph and
5795 * apply it to the current cluster bands if it is applicable
5796 * according to ok_to_merge.
5798 * If the number of remaining schedule dimensions in a cluster
5799 * with a non-maximal current schedule dimension is greater than
5800 * the number of remaining schedule dimensions in clusters
5801 * with a maximal current schedule dimension, then restrict
5802 * the number of rows to be computed in the cluster schedule
5803 * to the minimal such non-maximal current schedule dimension.
5804 * Do this by adjusting merge_graph.maxvar.
5806 * Return isl_bool_true if the clusters have effectively been merged
5807 * into a single cluster.
5809 * Note that since the standard scheduling algorithm minimizes the maximal
5810 * distance over proximity constraints, the proximity constraints between
5811 * the merged clusters may not be optimized any further than what is
5812 * sufficient to bring the distances within the limits of the internal
5813 * proximity constraints inside the individual clusters.
5814 * It may therefore make sense to perform an additional translation step
5815 * to bring the clusters closer to each other, while maintaining
5816 * the linear part of the merging schedule found using the standard
5817 * scheduling algorithm.
5819 static isl_bool try_merge(isl_ctx *ctx, struct isl_sched_graph *graph,
5820 struct isl_clustering *c)
5822 struct isl_sched_graph merge_graph = { 0 };
5823 isl_bool merged;
5825 if (init_merge_graph(ctx, graph, c, &merge_graph) < 0)
5826 goto error;
5828 if (compute_maxvar(&merge_graph) < 0)
5829 goto error;
5830 if (adjust_maxvar_to_slack(ctx, &merge_graph,c) < 0)
5831 goto error;
5832 if (compute_schedule_wcc_band(ctx, &merge_graph) < 0)
5833 goto error;
5834 merged = ok_to_merge(ctx, graph, c, &merge_graph);
5835 if (merged && merge(ctx, c, &merge_graph) < 0)
5836 goto error;
5838 graph_free(ctx, &merge_graph);
5839 return merged;
5840 error:
5841 graph_free(ctx, &merge_graph);
5842 return isl_bool_error;
5845 /* Is there any edge marked "no_merge" between two SCCs that are
5846 * about to be merged (i.e., that are set in "scc_in_merge")?
5847 * "merge_edge" is the proximity edge along which the clusters of SCCs
5848 * are going to be merged.
5850 * If there is any edge between two SCCs with a negative weight,
5851 * while the weight of "merge_edge" is non-negative, then this
5852 * means that the edge was postponed. "merge_edge" should then
5853 * also be postponed since merging along the edge with negative weight should
5854 * be postponed until all edges with non-negative weight have been tried.
5855 * Replace the weight of "merge_edge" by a negative weight as well and
5856 * tell the caller not to attempt a merge.
5858 static int any_no_merge(struct isl_sched_graph *graph, int *scc_in_merge,
5859 struct isl_sched_edge *merge_edge)
5861 int i;
5863 for (i = 0; i < graph->n_edge; ++i) {
5864 struct isl_sched_edge *edge = &graph->edge[i];
5866 if (!scc_in_merge[edge->src->scc])
5867 continue;
5868 if (!scc_in_merge[edge->dst->scc])
5869 continue;
5870 if (edge->no_merge)
5871 return 1;
5872 if (merge_edge->weight >= 0 && edge->weight < 0) {
5873 merge_edge->weight -= graph->max_weight + 1;
5874 return 1;
5878 return 0;
5881 /* Merge the two clusters in "c" connected by the edge in "graph"
5882 * with index "edge" into a single cluster.
5883 * If it turns out to be impossible to merge these two clusters,
5884 * then mark the edge as "no_merge" such that it will not be
5885 * considered again.
5887 * First mark all SCCs that need to be merged. This includes the SCCs
5888 * in the two clusters, but it may also include the SCCs
5889 * of intermediate clusters.
5890 * If there is already a no_merge edge between any pair of such SCCs,
5891 * then simply mark the current edge as no_merge as well.
5892 * Likewise, if any of those edges was postponed by has_bounded_distances,
5893 * then postpone the current edge as well.
5894 * Otherwise, try and merge the clusters and mark "edge" as "no_merge"
5895 * if the clusters did not end up getting merged, unless the non-merge
5896 * is due to the fact that the edge was postponed. This postponement
5897 * can be recognized by a change in weight (from non-negative to negative).
5899 static isl_stat merge_clusters_along_edge(isl_ctx *ctx,
5900 struct isl_sched_graph *graph, int edge, struct isl_clustering *c)
5902 isl_bool merged;
5903 int edge_weight = graph->edge[edge].weight;
5905 if (mark_merge_sccs(ctx, graph, edge, c) < 0)
5906 return isl_stat_error;
5908 if (any_no_merge(graph, c->scc_in_merge, &graph->edge[edge]))
5909 merged = isl_bool_false;
5910 else
5911 merged = try_merge(ctx, graph, c);
5912 if (merged < 0)
5913 return isl_stat_error;
5914 if (!merged && edge_weight == graph->edge[edge].weight)
5915 graph->edge[edge].no_merge = 1;
5917 return isl_stat_ok;
5920 /* Does "node" belong to the cluster identified by "cluster"?
5922 static int node_cluster_exactly(struct isl_sched_node *node, int cluster)
5924 return node->cluster == cluster;
5927 /* Does "edge" connect two nodes belonging to the cluster
5928 * identified by "cluster"?
5930 static int edge_cluster_exactly(struct isl_sched_edge *edge, int cluster)
5932 return edge->src->cluster == cluster && edge->dst->cluster == cluster;
5935 /* Swap the schedule of "node1" and "node2".
5936 * Both nodes have been derived from the same node in a common parent graph.
5937 * Since the "coincident" field is shared with that node
5938 * in the parent graph, there is no need to also swap this field.
5940 static void swap_sched(struct isl_sched_node *node1,
5941 struct isl_sched_node *node2)
5943 isl_mat *sched;
5944 isl_map *sched_map;
5946 sched = node1->sched;
5947 node1->sched = node2->sched;
5948 node2->sched = sched;
5950 sched_map = node1->sched_map;
5951 node1->sched_map = node2->sched_map;
5952 node2->sched_map = sched_map;
5955 /* Copy the current band schedule from the SCCs that form the cluster
5956 * with index "pos" to the actual cluster at position "pos".
5957 * By construction, the index of the first SCC that belongs to the cluster
5958 * is also "pos".
5960 * The order of the nodes inside both the SCCs and the cluster
5961 * is assumed to be same as the order in the original "graph".
5963 * Since the SCC graphs will no longer be used after this function,
5964 * the schedules are actually swapped rather than copied.
5966 static isl_stat copy_partial(struct isl_sched_graph *graph,
5967 struct isl_clustering *c, int pos)
5969 int i, j;
5971 c->cluster[pos].n_total_row = c->scc[pos].n_total_row;
5972 c->cluster[pos].n_row = c->scc[pos].n_row;
5973 c->cluster[pos].maxvar = c->scc[pos].maxvar;
5974 j = 0;
5975 for (i = 0; i < graph->n; ++i) {
5976 int k;
5977 int s;
5979 if (graph->node[i].cluster != pos)
5980 continue;
5981 s = graph->node[i].scc;
5982 k = c->scc_node[s]++;
5983 swap_sched(&c->cluster[pos].node[j], &c->scc[s].node[k]);
5984 if (c->scc[s].maxvar > c->cluster[pos].maxvar)
5985 c->cluster[pos].maxvar = c->scc[s].maxvar;
5986 ++j;
5989 return isl_stat_ok;
5992 /* Is there a (conditional) validity dependence from node[j] to node[i],
5993 * forcing node[i] to follow node[j] or do the nodes belong to the same
5994 * cluster?
5996 static isl_bool node_follows_strong_or_same_cluster(int i, int j, void *user)
5998 struct isl_sched_graph *graph = user;
6000 if (graph->node[i].cluster == graph->node[j].cluster)
6001 return isl_bool_true;
6002 return graph_has_validity_edge(graph, &graph->node[j], &graph->node[i]);
6005 /* Extract the merged clusters of SCCs in "graph", sort them, and
6006 * store them in c->clusters. Update c->scc_cluster accordingly.
6008 * First keep track of the cluster containing the SCC to which a node
6009 * belongs in the node itself.
6010 * Then extract the clusters into c->clusters, copying the current
6011 * band schedule from the SCCs that belong to the cluster.
6012 * Do this only once per cluster.
6014 * Finally, topologically sort the clusters and update c->scc_cluster
6015 * to match the new scc numbering. While the SCCs were originally
6016 * sorted already, some SCCs that depend on some other SCCs may
6017 * have been merged with SCCs that appear before these other SCCs.
6018 * A reordering may therefore be required.
6020 static isl_stat extract_clusters(isl_ctx *ctx, struct isl_sched_graph *graph,
6021 struct isl_clustering *c)
6023 int i;
6025 for (i = 0; i < graph->n; ++i)
6026 graph->node[i].cluster = c->scc_cluster[graph->node[i].scc];
6028 for (i = 0; i < graph->scc; ++i) {
6029 if (c->scc_cluster[i] != i)
6030 continue;
6031 if (extract_sub_graph(ctx, graph, &node_cluster_exactly,
6032 &edge_cluster_exactly, i, &c->cluster[i]) < 0)
6033 return isl_stat_error;
6034 c->cluster[i].src_scc = -1;
6035 c->cluster[i].dst_scc = -1;
6036 if (copy_partial(graph, c, i) < 0)
6037 return isl_stat_error;
6040 if (detect_ccs(ctx, graph, &node_follows_strong_or_same_cluster) < 0)
6041 return isl_stat_error;
6042 for (i = 0; i < graph->n; ++i)
6043 c->scc_cluster[graph->node[i].scc] = graph->node[i].cluster;
6045 return isl_stat_ok;
6048 /* Compute weights on the proximity edges of "graph" that can
6049 * be used by find_proximity to find the most appropriate
6050 * proximity edge to use to merge two clusters in "c".
6051 * The weights are also used by has_bounded_distances to determine
6052 * whether the merge should be allowed.
6053 * Store the maximum of the computed weights in graph->max_weight.
6055 * The computed weight is a measure for the number of remaining schedule
6056 * dimensions that can still be completely aligned.
6057 * In particular, compute the number of equalities between
6058 * input dimensions and output dimensions in the proximity constraints.
6059 * The directions that are already handled by outer schedule bands
6060 * are projected out prior to determining this number.
6062 * Edges that will never be considered by find_proximity are ignored.
6064 static isl_stat compute_weights(struct isl_sched_graph *graph,
6065 struct isl_clustering *c)
6067 int i;
6069 graph->max_weight = 0;
6071 for (i = 0; i < graph->n_edge; ++i) {
6072 struct isl_sched_edge *edge = &graph->edge[i];
6073 struct isl_sched_node *src = edge->src;
6074 struct isl_sched_node *dst = edge->dst;
6075 isl_basic_map *hull;
6076 int n_in, n_out;
6078 if (!is_proximity(edge))
6079 continue;
6080 if (bad_cluster(&c->scc[edge->src->scc]) ||
6081 bad_cluster(&c->scc[edge->dst->scc]))
6082 continue;
6083 if (c->scc_cluster[edge->dst->scc] ==
6084 c->scc_cluster[edge->src->scc])
6085 continue;
6087 hull = isl_map_affine_hull(isl_map_copy(edge->map));
6088 hull = isl_basic_map_transform_dims(hull, isl_dim_in, 0,
6089 isl_mat_copy(src->ctrans));
6090 hull = isl_basic_map_transform_dims(hull, isl_dim_out, 0,
6091 isl_mat_copy(dst->ctrans));
6092 hull = isl_basic_map_project_out(hull,
6093 isl_dim_in, 0, src->rank);
6094 hull = isl_basic_map_project_out(hull,
6095 isl_dim_out, 0, dst->rank);
6096 hull = isl_basic_map_remove_divs(hull);
6097 n_in = isl_basic_map_dim(hull, isl_dim_in);
6098 n_out = isl_basic_map_dim(hull, isl_dim_out);
6099 hull = isl_basic_map_drop_constraints_not_involving_dims(hull,
6100 isl_dim_in, 0, n_in);
6101 hull = isl_basic_map_drop_constraints_not_involving_dims(hull,
6102 isl_dim_out, 0, n_out);
6103 if (!hull)
6104 return isl_stat_error;
6105 edge->weight = hull->n_eq;
6106 isl_basic_map_free(hull);
6108 if (edge->weight > graph->max_weight)
6109 graph->max_weight = edge->weight;
6112 return isl_stat_ok;
6115 /* Call compute_schedule_finish_band on each of the clusters in "c"
6116 * in their topological order. This order is determined by the scc
6117 * fields of the nodes in "graph".
6118 * Combine the results in a sequence expressing the topological order.
6120 * If there is only one cluster left, then there is no need to introduce
6121 * a sequence node. Also, in this case, the cluster necessarily contains
6122 * the SCC at position 0 in the original graph and is therefore also
6123 * stored in the first cluster of "c".
6125 static __isl_give isl_schedule_node *finish_bands_clustering(
6126 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph,
6127 struct isl_clustering *c)
6129 int i;
6130 isl_ctx *ctx;
6131 isl_union_set_list *filters;
6133 if (graph->scc == 1)
6134 return compute_schedule_finish_band(node, &c->cluster[0], 0);
6136 ctx = isl_schedule_node_get_ctx(node);
6138 filters = extract_sccs(ctx, graph);
6139 node = isl_schedule_node_insert_sequence(node, filters);
6141 for (i = 0; i < graph->scc; ++i) {
6142 int j = c->scc_cluster[i];
6143 node = isl_schedule_node_child(node, i);
6144 node = isl_schedule_node_child(node, 0);
6145 node = compute_schedule_finish_band(node, &c->cluster[j], 0);
6146 node = isl_schedule_node_parent(node);
6147 node = isl_schedule_node_parent(node);
6150 return node;
6153 /* Compute a schedule for a connected dependence graph by first considering
6154 * each strongly connected component (SCC) in the graph separately and then
6155 * incrementally combining them into clusters.
6156 * Return the updated schedule node.
6158 * Initially, each cluster consists of a single SCC, each with its
6159 * own band schedule. The algorithm then tries to merge pairs
6160 * of clusters along a proximity edge until no more suitable
6161 * proximity edges can be found. During this merging, the schedule
6162 * is maintained in the individual SCCs.
6163 * After the merging is completed, the full resulting clusters
6164 * are extracted and in finish_bands_clustering,
6165 * compute_schedule_finish_band is called on each of them to integrate
6166 * the band into "node" and to continue the computation.
6168 * compute_weights initializes the weights that are used by find_proximity.
6170 static __isl_give isl_schedule_node *compute_schedule_wcc_clustering(
6171 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph)
6173 isl_ctx *ctx;
6174 struct isl_clustering c;
6175 int i;
6177 ctx = isl_schedule_node_get_ctx(node);
6179 if (clustering_init(ctx, &c, graph) < 0)
6180 goto error;
6182 if (compute_weights(graph, &c) < 0)
6183 goto error;
6185 for (;;) {
6186 i = find_proximity(graph, &c);
6187 if (i < 0)
6188 goto error;
6189 if (i >= graph->n_edge)
6190 break;
6191 if (merge_clusters_along_edge(ctx, graph, i, &c) < 0)
6192 goto error;
6195 if (extract_clusters(ctx, graph, &c) < 0)
6196 goto error;
6198 node = finish_bands_clustering(node, graph, &c);
6200 clustering_free(ctx, &c);
6201 return node;
6202 error:
6203 clustering_free(ctx, &c);
6204 return isl_schedule_node_free(node);
6207 /* Compute a schedule for a connected dependence graph and return
6208 * the updated schedule node.
6210 * If Feautrier's algorithm is selected, we first recursively try to satisfy
6211 * as many validity dependences as possible. When all validity dependences
6212 * are satisfied we extend the schedule to a full-dimensional schedule.
6214 * Call compute_schedule_wcc_whole or compute_schedule_wcc_clustering
6215 * depending on whether the user has selected the option to try and
6216 * compute a schedule for the entire (weakly connected) component first.
6217 * If there is only a single strongly connected component (SCC), then
6218 * there is no point in trying to combine SCCs
6219 * in compute_schedule_wcc_clustering, so compute_schedule_wcc_whole
6220 * is called instead.
6222 static __isl_give isl_schedule_node *compute_schedule_wcc(
6223 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph)
6225 isl_ctx *ctx;
6227 if (!node)
6228 return NULL;
6230 ctx = isl_schedule_node_get_ctx(node);
6231 if (detect_sccs(ctx, graph) < 0)
6232 return isl_schedule_node_free(node);
6234 if (compute_maxvar(graph) < 0)
6235 return isl_schedule_node_free(node);
6237 if (need_feautrier_step(ctx, graph))
6238 return compute_schedule_wcc_feautrier(node, graph);
6240 if (graph->scc <= 1 || isl_options_get_schedule_whole_component(ctx))
6241 return compute_schedule_wcc_whole(node, graph);
6242 else
6243 return compute_schedule_wcc_clustering(node, graph);
6246 /* Compute a schedule for each group of nodes identified by node->scc
6247 * separately and then combine them in a sequence node (or as set node
6248 * if graph->weak is set) inserted at position "node" of the schedule tree.
6249 * Return the updated schedule node.
6251 * If "wcc" is set then each of the groups belongs to a single
6252 * weakly connected component in the dependence graph so that
6253 * there is no need for compute_sub_schedule to look for weakly
6254 * connected components.
6256 static __isl_give isl_schedule_node *compute_component_schedule(
6257 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph,
6258 int wcc)
6260 int component;
6261 isl_ctx *ctx;
6262 isl_union_set_list *filters;
6264 if (!node)
6265 return NULL;
6266 ctx = isl_schedule_node_get_ctx(node);
6268 filters = extract_sccs(ctx, graph);
6269 if (graph->weak)
6270 node = isl_schedule_node_insert_set(node, filters);
6271 else
6272 node = isl_schedule_node_insert_sequence(node, filters);
6274 for (component = 0; component < graph->scc; ++component) {
6275 node = isl_schedule_node_child(node, component);
6276 node = isl_schedule_node_child(node, 0);
6277 node = compute_sub_schedule(node, ctx, graph,
6278 &node_scc_exactly,
6279 &edge_scc_exactly, component, wcc);
6280 node = isl_schedule_node_parent(node);
6281 node = isl_schedule_node_parent(node);
6284 return node;
6287 /* Compute a schedule for the given dependence graph and insert it at "node".
6288 * Return the updated schedule node.
6290 * We first check if the graph is connected (through validity and conditional
6291 * validity dependences) and, if not, compute a schedule
6292 * for each component separately.
6293 * If the schedule_serialize_sccs option is set, then we check for strongly
6294 * connected components instead and compute a separate schedule for
6295 * each such strongly connected component.
6297 static __isl_give isl_schedule_node *compute_schedule(isl_schedule_node *node,
6298 struct isl_sched_graph *graph)
6300 isl_ctx *ctx;
6302 if (!node)
6303 return NULL;
6305 ctx = isl_schedule_node_get_ctx(node);
6306 if (isl_options_get_schedule_serialize_sccs(ctx)) {
6307 if (detect_sccs(ctx, graph) < 0)
6308 return isl_schedule_node_free(node);
6309 } else {
6310 if (detect_wccs(ctx, graph) < 0)
6311 return isl_schedule_node_free(node);
6314 if (graph->scc > 1)
6315 return compute_component_schedule(node, graph, 1);
6317 return compute_schedule_wcc(node, graph);
6320 /* Compute a schedule on sc->domain that respects the given schedule
6321 * constraints.
6323 * In particular, the schedule respects all the validity dependences.
6324 * If the default isl scheduling algorithm is used, it tries to minimize
6325 * the dependence distances over the proximity dependences.
6326 * If Feautrier's scheduling algorithm is used, the proximity dependence
6327 * distances are only minimized during the extension to a full-dimensional
6328 * schedule.
6330 * If there are any condition and conditional validity dependences,
6331 * then the conditional validity dependences may be violated inside
6332 * a tilable band, provided they have no adjacent non-local
6333 * condition dependences.
6335 __isl_give isl_schedule *isl_schedule_constraints_compute_schedule(
6336 __isl_take isl_schedule_constraints *sc)
6338 isl_ctx *ctx = isl_schedule_constraints_get_ctx(sc);
6339 struct isl_sched_graph graph = { 0 };
6340 isl_schedule *sched;
6341 isl_schedule_node *node;
6342 isl_union_set *domain;
6344 sc = isl_schedule_constraints_align_params(sc);
6346 domain = isl_schedule_constraints_get_domain(sc);
6347 if (isl_union_set_n_set(domain) == 0) {
6348 isl_schedule_constraints_free(sc);
6349 return isl_schedule_from_domain(domain);
6352 if (graph_init(&graph, sc) < 0)
6353 domain = isl_union_set_free(domain);
6355 node = isl_schedule_node_from_domain(domain);
6356 node = isl_schedule_node_child(node, 0);
6357 if (graph.n > 0)
6358 node = compute_schedule(node, &graph);
6359 sched = isl_schedule_node_get_schedule(node);
6360 isl_schedule_node_free(node);
6362 graph_free(ctx, &graph);
6363 isl_schedule_constraints_free(sc);
6365 return sched;
6368 /* Compute a schedule for the given union of domains that respects
6369 * all the validity dependences and minimizes
6370 * the dependence distances over the proximity dependences.
6372 * This function is kept for backward compatibility.
6374 __isl_give isl_schedule *isl_union_set_compute_schedule(
6375 __isl_take isl_union_set *domain,
6376 __isl_take isl_union_map *validity,
6377 __isl_take isl_union_map *proximity)
6379 isl_schedule_constraints *sc;
6381 sc = isl_schedule_constraints_on_domain(domain);
6382 sc = isl_schedule_constraints_set_validity(sc, validity);
6383 sc = isl_schedule_constraints_set_proximity(sc, proximity);
6385 return isl_schedule_constraints_compute_schedule(sc);