isl_ast_build_expr.c: directly include required header
[isl.git] / isl_scheduler.c
blobdb31a69567ac50e16607d5ec66a50f0992325be0
1 /*
2 * Copyright 2011 INRIA Saclay
3 * Copyright 2012-2014 Ecole Normale Superieure
4 * Copyright 2015 Sven Verdoolaege
6 * Use of this software is governed by the MIT license
8 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
9 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
10 * 91893 Orsay, France
11 * and Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
14 #include <isl_ctx_private.h>
15 #include <isl_map_private.h>
16 #include <isl_space_private.h>
17 #include <isl_aff_private.h>
18 #include <isl/hash.h>
19 #include <isl/constraint.h>
20 #include <isl/schedule.h>
21 #include <isl/schedule_node.h>
22 #include <isl_mat_private.h>
23 #include <isl_vec_private.h>
24 #include <isl/set.h>
25 #include <isl_seq.h>
26 #include <isl_tab.h>
27 #include <isl_dim_map.h>
28 #include <isl/map_to_basic_set.h>
29 #include <isl_sort.h>
30 #include <isl_options_private.h>
31 #include <isl_tarjan.h>
32 #include <isl_morph.h>
35 * The scheduling algorithm implemented in this file was inspired by
36 * Bondhugula et al., "Automatic Transformations for Communication-Minimized
37 * Parallelization and Locality Optimization in the Polyhedral Model".
40 enum isl_edge_type {
41 isl_edge_validity = 0,
42 isl_edge_first = isl_edge_validity,
43 isl_edge_coincidence,
44 isl_edge_condition,
45 isl_edge_conditional_validity,
46 isl_edge_proximity,
47 isl_edge_last = isl_edge_proximity
50 /* The constraints that need to be satisfied by a schedule on "domain".
52 * "context" specifies extra constraints on the parameters.
54 * "validity" constraints map domain elements i to domain elements
55 * that should be scheduled after i. (Hard constraint)
56 * "proximity" constraints map domain elements i to domains elements
57 * that should be scheduled as early as possible after i (or before i).
58 * (Soft constraint)
60 * "condition" and "conditional_validity" constraints map possibly "tagged"
61 * domain elements i -> s to "tagged" domain elements j -> t.
62 * The elements of the "conditional_validity" constraints, but without the
63 * tags (i.e., the elements i -> j) are treated as validity constraints,
64 * except that during the construction of a tilable band,
65 * the elements of the "conditional_validity" constraints may be violated
66 * provided that all adjacent elements of the "condition" constraints
67 * are local within the band.
68 * A dependence is local within a band if domain and range are mapped
69 * to the same schedule point by the band.
71 struct isl_schedule_constraints {
72 isl_union_set *domain;
73 isl_set *context;
75 isl_union_map *constraint[isl_edge_last + 1];
78 __isl_give isl_schedule_constraints *isl_schedule_constraints_copy(
79 __isl_keep isl_schedule_constraints *sc)
81 isl_ctx *ctx;
82 isl_schedule_constraints *sc_copy;
83 enum isl_edge_type i;
85 ctx = isl_union_set_get_ctx(sc->domain);
86 sc_copy = isl_calloc_type(ctx, struct isl_schedule_constraints);
87 if (!sc_copy)
88 return NULL;
90 sc_copy->domain = isl_union_set_copy(sc->domain);
91 sc_copy->context = isl_set_copy(sc->context);
92 if (!sc_copy->domain || !sc_copy->context)
93 return isl_schedule_constraints_free(sc_copy);
95 for (i = isl_edge_first; i <= isl_edge_last; ++i) {
96 sc_copy->constraint[i] = isl_union_map_copy(sc->constraint[i]);
97 if (!sc_copy->constraint[i])
98 return isl_schedule_constraints_free(sc_copy);
101 return sc_copy;
105 /* Construct an isl_schedule_constraints object for computing a schedule
106 * on "domain". The initial object does not impose any constraints.
108 __isl_give isl_schedule_constraints *isl_schedule_constraints_on_domain(
109 __isl_take isl_union_set *domain)
111 isl_ctx *ctx;
112 isl_space *space;
113 isl_schedule_constraints *sc;
114 isl_union_map *empty;
115 enum isl_edge_type i;
117 if (!domain)
118 return NULL;
120 ctx = isl_union_set_get_ctx(domain);
121 sc = isl_calloc_type(ctx, struct isl_schedule_constraints);
122 if (!sc)
123 goto error;
125 space = isl_union_set_get_space(domain);
126 sc->domain = domain;
127 sc->context = isl_set_universe(isl_space_copy(space));
128 empty = isl_union_map_empty(space);
129 for (i = isl_edge_first; i <= isl_edge_last; ++i) {
130 sc->constraint[i] = isl_union_map_copy(empty);
131 if (!sc->constraint[i])
132 sc->domain = isl_union_set_free(sc->domain);
134 isl_union_map_free(empty);
136 if (!sc->domain || !sc->context)
137 return isl_schedule_constraints_free(sc);
139 return sc;
140 error:
141 isl_union_set_free(domain);
142 return NULL;
145 /* Replace the context of "sc" by "context".
147 __isl_give isl_schedule_constraints *isl_schedule_constraints_set_context(
148 __isl_take isl_schedule_constraints *sc, __isl_take isl_set *context)
150 if (!sc || !context)
151 goto error;
153 isl_set_free(sc->context);
154 sc->context = context;
156 return sc;
157 error:
158 isl_schedule_constraints_free(sc);
159 isl_set_free(context);
160 return NULL;
163 /* Replace the validity constraints of "sc" by "validity".
165 __isl_give isl_schedule_constraints *isl_schedule_constraints_set_validity(
166 __isl_take isl_schedule_constraints *sc,
167 __isl_take isl_union_map *validity)
169 if (!sc || !validity)
170 goto error;
172 isl_union_map_free(sc->constraint[isl_edge_validity]);
173 sc->constraint[isl_edge_validity] = validity;
175 return sc;
176 error:
177 isl_schedule_constraints_free(sc);
178 isl_union_map_free(validity);
179 return NULL;
182 /* Replace the coincidence constraints of "sc" by "coincidence".
184 __isl_give isl_schedule_constraints *isl_schedule_constraints_set_coincidence(
185 __isl_take isl_schedule_constraints *sc,
186 __isl_take isl_union_map *coincidence)
188 if (!sc || !coincidence)
189 goto error;
191 isl_union_map_free(sc->constraint[isl_edge_coincidence]);
192 sc->constraint[isl_edge_coincidence] = coincidence;
194 return sc;
195 error:
196 isl_schedule_constraints_free(sc);
197 isl_union_map_free(coincidence);
198 return NULL;
201 /* Replace the proximity constraints of "sc" by "proximity".
203 __isl_give isl_schedule_constraints *isl_schedule_constraints_set_proximity(
204 __isl_take isl_schedule_constraints *sc,
205 __isl_take isl_union_map *proximity)
207 if (!sc || !proximity)
208 goto error;
210 isl_union_map_free(sc->constraint[isl_edge_proximity]);
211 sc->constraint[isl_edge_proximity] = proximity;
213 return sc;
214 error:
215 isl_schedule_constraints_free(sc);
216 isl_union_map_free(proximity);
217 return NULL;
220 /* Replace the conditional validity constraints of "sc" by "condition"
221 * and "validity".
223 __isl_give isl_schedule_constraints *
224 isl_schedule_constraints_set_conditional_validity(
225 __isl_take isl_schedule_constraints *sc,
226 __isl_take isl_union_map *condition,
227 __isl_take isl_union_map *validity)
229 if (!sc || !condition || !validity)
230 goto error;
232 isl_union_map_free(sc->constraint[isl_edge_condition]);
233 sc->constraint[isl_edge_condition] = condition;
234 isl_union_map_free(sc->constraint[isl_edge_conditional_validity]);
235 sc->constraint[isl_edge_conditional_validity] = validity;
237 return sc;
238 error:
239 isl_schedule_constraints_free(sc);
240 isl_union_map_free(condition);
241 isl_union_map_free(validity);
242 return NULL;
245 __isl_null isl_schedule_constraints *isl_schedule_constraints_free(
246 __isl_take isl_schedule_constraints *sc)
248 enum isl_edge_type i;
250 if (!sc)
251 return NULL;
253 isl_union_set_free(sc->domain);
254 isl_set_free(sc->context);
255 for (i = isl_edge_first; i <= isl_edge_last; ++i)
256 isl_union_map_free(sc->constraint[i]);
258 free(sc);
260 return NULL;
263 isl_ctx *isl_schedule_constraints_get_ctx(
264 __isl_keep isl_schedule_constraints *sc)
266 return sc ? isl_union_set_get_ctx(sc->domain) : NULL;
269 /* Return the validity constraints of "sc".
271 __isl_give isl_union_map *isl_schedule_constraints_get_validity(
272 __isl_keep isl_schedule_constraints *sc)
274 if (!sc)
275 return NULL;
277 return isl_union_map_copy(sc->constraint[isl_edge_validity]);
280 /* Return the coincidence constraints of "sc".
282 __isl_give isl_union_map *isl_schedule_constraints_get_coincidence(
283 __isl_keep isl_schedule_constraints *sc)
285 if (!sc)
286 return NULL;
288 return isl_union_map_copy(sc->constraint[isl_edge_coincidence]);
291 /* Return the conditional validity constraints of "sc".
293 __isl_give isl_union_map *isl_schedule_constraints_get_conditional_validity(
294 __isl_keep isl_schedule_constraints *sc)
296 if (!sc)
297 return NULL;
299 return
300 isl_union_map_copy(sc->constraint[isl_edge_conditional_validity]);
303 /* Return the conditions for the conditional validity constraints of "sc".
305 __isl_give isl_union_map *
306 isl_schedule_constraints_get_conditional_validity_condition(
307 __isl_keep isl_schedule_constraints *sc)
309 if (!sc)
310 return NULL;
312 return isl_union_map_copy(sc->constraint[isl_edge_condition]);
315 void isl_schedule_constraints_dump(__isl_keep isl_schedule_constraints *sc)
317 if (!sc)
318 return;
320 fprintf(stderr, "domain: ");
321 isl_union_set_dump(sc->domain);
322 fprintf(stderr, "context: ");
323 isl_set_dump(sc->context);
324 fprintf(stderr, "validity: ");
325 isl_union_map_dump(sc->constraint[isl_edge_validity]);
326 fprintf(stderr, "proximity: ");
327 isl_union_map_dump(sc->constraint[isl_edge_proximity]);
328 fprintf(stderr, "coincidence: ");
329 isl_union_map_dump(sc->constraint[isl_edge_coincidence]);
330 fprintf(stderr, "condition: ");
331 isl_union_map_dump(sc->constraint[isl_edge_condition]);
332 fprintf(stderr, "conditional_validity: ");
333 isl_union_map_dump(sc->constraint[isl_edge_conditional_validity]);
336 /* Align the parameters of the fields of "sc".
338 static __isl_give isl_schedule_constraints *
339 isl_schedule_constraints_align_params(__isl_take isl_schedule_constraints *sc)
341 isl_space *space;
342 enum isl_edge_type i;
344 if (!sc)
345 return NULL;
347 space = isl_union_set_get_space(sc->domain);
348 space = isl_space_align_params(space, isl_set_get_space(sc->context));
349 for (i = isl_edge_first; i <= isl_edge_last; ++i)
350 space = isl_space_align_params(space,
351 isl_union_map_get_space(sc->constraint[i]));
353 for (i = isl_edge_first; i <= isl_edge_last; ++i) {
354 sc->constraint[i] = isl_union_map_align_params(
355 sc->constraint[i], isl_space_copy(space));
356 if (!sc->constraint[i])
357 space = isl_space_free(space);
359 sc->context = isl_set_align_params(sc->context, isl_space_copy(space));
360 sc->domain = isl_union_set_align_params(sc->domain, space);
361 if (!sc->context || !sc->domain)
362 return isl_schedule_constraints_free(sc);
364 return sc;
367 /* Return the total number of isl_maps in the constraints of "sc".
369 static __isl_give int isl_schedule_constraints_n_map(
370 __isl_keep isl_schedule_constraints *sc)
372 enum isl_edge_type i;
373 int n = 0;
375 for (i = isl_edge_first; i <= isl_edge_last; ++i)
376 n += isl_union_map_n_map(sc->constraint[i]);
378 return n;
381 /* Internal information about a node that is used during the construction
382 * of a schedule.
383 * space represents the space in which the domain lives
384 * sched is a matrix representation of the schedule being constructed
385 * for this node; if compressed is set, then this schedule is
386 * defined over the compressed domain space
387 * sched_map is an isl_map representation of the same (partial) schedule
388 * sched_map may be NULL; if compressed is set, then this map
389 * is defined over the uncompressed domain space
390 * rank is the number of linearly independent rows in the linear part
391 * of sched
392 * the columns of cmap represent a change of basis for the schedule
393 * coefficients; the first rank columns span the linear part of
394 * the schedule rows
395 * cinv is the inverse of cmap.
396 * start is the first variable in the LP problem in the sequences that
397 * represents the schedule coefficients of this node
398 * nvar is the dimension of the domain
399 * nparam is the number of parameters or 0 if we are not constructing
400 * a parametric schedule
402 * If compressed is set, then hull represents the constraints
403 * that were used to derive the compression, while compress and
404 * decompress map the original space to the compressed space and
405 * vice versa.
407 * scc is the index of SCC (or WCC) this node belongs to
409 * coincident contains a boolean for each of the rows of the schedule,
410 * indicating whether the corresponding scheduling dimension satisfies
411 * the coincidence constraints in the sense that the corresponding
412 * dependence distances are zero.
414 struct isl_sched_node {
415 isl_space *space;
416 int compressed;
417 isl_set *hull;
418 isl_multi_aff *compress;
419 isl_multi_aff *decompress;
420 isl_mat *sched;
421 isl_map *sched_map;
422 int rank;
423 isl_mat *cmap;
424 isl_mat *cinv;
425 int start;
426 int nvar;
427 int nparam;
429 int scc;
431 int *coincident;
434 static int node_has_space(const void *entry, const void *val)
436 struct isl_sched_node *node = (struct isl_sched_node *)entry;
437 isl_space *dim = (isl_space *)val;
439 return isl_space_is_equal(node->space, dim);
442 static int node_scc_exactly(struct isl_sched_node *node, int scc)
444 return node->scc == scc;
447 static int node_scc_at_most(struct isl_sched_node *node, int scc)
449 return node->scc <= scc;
452 static int node_scc_at_least(struct isl_sched_node *node, int scc)
454 return node->scc >= scc;
457 /* An edge in the dependence graph. An edge may be used to
458 * ensure validity of the generated schedule, to minimize the dependence
459 * distance or both
461 * map is the dependence relation, with i -> j in the map if j depends on i
462 * tagged_condition and tagged_validity contain the union of all tagged
463 * condition or conditional validity dependence relations that
464 * specialize the dependence relation "map"; that is,
465 * if (i -> a) -> (j -> b) is an element of "tagged_condition"
466 * or "tagged_validity", then i -> j is an element of "map".
467 * If these fields are NULL, then they represent the empty relation.
468 * src is the source node
469 * dst is the sink node
470 * validity is set if the edge is used to ensure correctness
471 * coincidence is used to enforce zero dependence distances
472 * proximity is set if the edge is used to minimize dependence distances
473 * condition is set if the edge represents a condition
474 * for a conditional validity schedule constraint
475 * local can only be set for condition edges and indicates that
476 * the dependence distance over the edge should be zero
477 * conditional_validity is set if the edge is used to conditionally
478 * ensure correctness
480 * For validity edges, start and end mark the sequence of inequality
481 * constraints in the LP problem that encode the validity constraint
482 * corresponding to this edge.
484 struct isl_sched_edge {
485 isl_map *map;
486 isl_union_map *tagged_condition;
487 isl_union_map *tagged_validity;
489 struct isl_sched_node *src;
490 struct isl_sched_node *dst;
492 unsigned validity : 1;
493 unsigned coincidence : 1;
494 unsigned proximity : 1;
495 unsigned local : 1;
496 unsigned condition : 1;
497 unsigned conditional_validity : 1;
499 int start;
500 int end;
503 /* Internal information about the dependence graph used during
504 * the construction of the schedule.
506 * intra_hmap is a cache, mapping dependence relations to their dual,
507 * for dependences from a node to itself
508 * inter_hmap is a cache, mapping dependence relations to their dual,
509 * for dependences between distinct nodes
510 * if compression is involved then the key for these maps
511 * it the original, uncompressed dependence relation, while
512 * the value is the dual of the compressed dependence relation.
514 * n is the number of nodes
515 * node is the list of nodes
516 * maxvar is the maximal number of variables over all nodes
517 * max_row is the allocated number of rows in the schedule
518 * n_row is the current (maximal) number of linearly independent
519 * rows in the node schedules
520 * n_total_row is the current number of rows in the node schedules
521 * band_start is the starting row in the node schedules of the current band
522 * root is set if this graph is the original dependence graph,
523 * without any splitting
525 * sorted contains a list of node indices sorted according to the
526 * SCC to which a node belongs
528 * n_edge is the number of edges
529 * edge is the list of edges
530 * max_edge contains the maximal number of edges of each type;
531 * in particular, it contains the number of edges in the inital graph.
532 * edge_table contains pointers into the edge array, hashed on the source
533 * and sink spaces; there is one such table for each type;
534 * a given edge may be referenced from more than one table
535 * if the corresponding relation appears in more than of the
536 * sets of dependences
538 * node_table contains pointers into the node array, hashed on the space
540 * region contains a list of variable sequences that should be non-trivial
542 * lp contains the (I)LP problem used to obtain new schedule rows
544 * src_scc and dst_scc are the source and sink SCCs of an edge with
545 * conflicting constraints
547 * scc represents the number of components
548 * weak is set if the components are weakly connected
550 struct isl_sched_graph {
551 isl_map_to_basic_set *intra_hmap;
552 isl_map_to_basic_set *inter_hmap;
554 struct isl_sched_node *node;
555 int n;
556 int maxvar;
557 int max_row;
558 int n_row;
560 int *sorted;
562 int n_total_row;
563 int band_start;
565 int root;
567 struct isl_sched_edge *edge;
568 int n_edge;
569 int max_edge[isl_edge_last + 1];
570 struct isl_hash_table *edge_table[isl_edge_last + 1];
572 struct isl_hash_table *node_table;
573 struct isl_region *region;
575 isl_basic_set *lp;
577 int src_scc;
578 int dst_scc;
580 int scc;
581 int weak;
584 /* Initialize node_table based on the list of nodes.
586 static int graph_init_table(isl_ctx *ctx, struct isl_sched_graph *graph)
588 int i;
590 graph->node_table = isl_hash_table_alloc(ctx, graph->n);
591 if (!graph->node_table)
592 return -1;
594 for (i = 0; i < graph->n; ++i) {
595 struct isl_hash_table_entry *entry;
596 uint32_t hash;
598 hash = isl_space_get_hash(graph->node[i].space);
599 entry = isl_hash_table_find(ctx, graph->node_table, hash,
600 &node_has_space,
601 graph->node[i].space, 1);
602 if (!entry)
603 return -1;
604 entry->data = &graph->node[i];
607 return 0;
610 /* Return a pointer to the node that lives within the given space,
611 * or NULL if there is no such node.
613 static struct isl_sched_node *graph_find_node(isl_ctx *ctx,
614 struct isl_sched_graph *graph, __isl_keep isl_space *dim)
616 struct isl_hash_table_entry *entry;
617 uint32_t hash;
619 hash = isl_space_get_hash(dim);
620 entry = isl_hash_table_find(ctx, graph->node_table, hash,
621 &node_has_space, dim, 0);
623 return entry ? entry->data : NULL;
626 static int edge_has_src_and_dst(const void *entry, const void *val)
628 const struct isl_sched_edge *edge = entry;
629 const struct isl_sched_edge *temp = val;
631 return edge->src == temp->src && edge->dst == temp->dst;
634 /* Add the given edge to graph->edge_table[type].
636 static int graph_edge_table_add(isl_ctx *ctx, struct isl_sched_graph *graph,
637 enum isl_edge_type type, struct isl_sched_edge *edge)
639 struct isl_hash_table_entry *entry;
640 uint32_t hash;
642 hash = isl_hash_init();
643 hash = isl_hash_builtin(hash, edge->src);
644 hash = isl_hash_builtin(hash, edge->dst);
645 entry = isl_hash_table_find(ctx, graph->edge_table[type], hash,
646 &edge_has_src_and_dst, edge, 1);
647 if (!entry)
648 return -1;
649 entry->data = edge;
651 return 0;
654 /* Allocate the edge_tables based on the maximal number of edges of
655 * each type.
657 static int graph_init_edge_tables(isl_ctx *ctx, struct isl_sched_graph *graph)
659 int i;
661 for (i = 0; i <= isl_edge_last; ++i) {
662 graph->edge_table[i] = isl_hash_table_alloc(ctx,
663 graph->max_edge[i]);
664 if (!graph->edge_table[i])
665 return -1;
668 return 0;
671 /* If graph->edge_table[type] contains an edge from the given source
672 * to the given destination, then return the hash table entry of this edge.
673 * Otherwise, return NULL.
675 static struct isl_hash_table_entry *graph_find_edge_entry(
676 struct isl_sched_graph *graph,
677 enum isl_edge_type type,
678 struct isl_sched_node *src, struct isl_sched_node *dst)
680 isl_ctx *ctx = isl_space_get_ctx(src->space);
681 uint32_t hash;
682 struct isl_sched_edge temp = { .src = src, .dst = dst };
684 hash = isl_hash_init();
685 hash = isl_hash_builtin(hash, temp.src);
686 hash = isl_hash_builtin(hash, temp.dst);
687 return isl_hash_table_find(ctx, graph->edge_table[type], hash,
688 &edge_has_src_and_dst, &temp, 0);
692 /* If graph->edge_table[type] contains an edge from the given source
693 * to the given destination, then return this edge.
694 * Otherwise, return NULL.
696 static struct isl_sched_edge *graph_find_edge(struct isl_sched_graph *graph,
697 enum isl_edge_type type,
698 struct isl_sched_node *src, struct isl_sched_node *dst)
700 struct isl_hash_table_entry *entry;
702 entry = graph_find_edge_entry(graph, type, src, dst);
703 if (!entry)
704 return NULL;
706 return entry->data;
709 /* Check whether the dependence graph has an edge of the given type
710 * between the given two nodes.
712 static int graph_has_edge(struct isl_sched_graph *graph,
713 enum isl_edge_type type,
714 struct isl_sched_node *src, struct isl_sched_node *dst)
716 struct isl_sched_edge *edge;
717 int empty;
719 edge = graph_find_edge(graph, type, src, dst);
720 if (!edge)
721 return 0;
723 empty = isl_map_plain_is_empty(edge->map);
724 if (empty < 0)
725 return -1;
727 return !empty;
730 /* Look for any edge with the same src, dst and map fields as "model".
732 * Return the matching edge if one can be found.
733 * Return "model" if no matching edge is found.
734 * Return NULL on error.
736 static struct isl_sched_edge *graph_find_matching_edge(
737 struct isl_sched_graph *graph, struct isl_sched_edge *model)
739 enum isl_edge_type i;
740 struct isl_sched_edge *edge;
742 for (i = isl_edge_first; i <= isl_edge_last; ++i) {
743 int is_equal;
745 edge = graph_find_edge(graph, i, model->src, model->dst);
746 if (!edge)
747 continue;
748 is_equal = isl_map_plain_is_equal(model->map, edge->map);
749 if (is_equal < 0)
750 return NULL;
751 if (is_equal)
752 return edge;
755 return model;
758 /* Remove the given edge from all the edge_tables that refer to it.
760 static void graph_remove_edge(struct isl_sched_graph *graph,
761 struct isl_sched_edge *edge)
763 isl_ctx *ctx = isl_map_get_ctx(edge->map);
764 enum isl_edge_type i;
766 for (i = isl_edge_first; i <= isl_edge_last; ++i) {
767 struct isl_hash_table_entry *entry;
769 entry = graph_find_edge_entry(graph, i, edge->src, edge->dst);
770 if (!entry)
771 continue;
772 if (entry->data != edge)
773 continue;
774 isl_hash_table_remove(ctx, graph->edge_table[i], entry);
778 /* Check whether the dependence graph has any edge
779 * between the given two nodes.
781 static int graph_has_any_edge(struct isl_sched_graph *graph,
782 struct isl_sched_node *src, struct isl_sched_node *dst)
784 enum isl_edge_type i;
785 int r;
787 for (i = isl_edge_first; i <= isl_edge_last; ++i) {
788 r = graph_has_edge(graph, i, src, dst);
789 if (r < 0 || r)
790 return r;
793 return r;
796 /* Check whether the dependence graph has a validity edge
797 * between the given two nodes.
799 * Conditional validity edges are essentially validity edges that
800 * can be ignored if the corresponding condition edges are iteration private.
801 * Here, we are only checking for the presence of validity
802 * edges, so we need to consider the conditional validity edges too.
803 * In particular, this function is used during the detection
804 * of strongly connected components and we cannot ignore
805 * conditional validity edges during this detection.
807 static int graph_has_validity_edge(struct isl_sched_graph *graph,
808 struct isl_sched_node *src, struct isl_sched_node *dst)
810 int r;
812 r = graph_has_edge(graph, isl_edge_validity, src, dst);
813 if (r < 0 || r)
814 return r;
816 return graph_has_edge(graph, isl_edge_conditional_validity, src, dst);
819 static int graph_alloc(isl_ctx *ctx, struct isl_sched_graph *graph,
820 int n_node, int n_edge)
822 int i;
824 graph->n = n_node;
825 graph->n_edge = n_edge;
826 graph->node = isl_calloc_array(ctx, struct isl_sched_node, graph->n);
827 graph->sorted = isl_calloc_array(ctx, int, graph->n);
828 graph->region = isl_alloc_array(ctx, struct isl_region, graph->n);
829 graph->edge = isl_calloc_array(ctx,
830 struct isl_sched_edge, graph->n_edge);
832 graph->intra_hmap = isl_map_to_basic_set_alloc(ctx, 2 * n_edge);
833 graph->inter_hmap = isl_map_to_basic_set_alloc(ctx, 2 * n_edge);
835 if (!graph->node || !graph->region || (graph->n_edge && !graph->edge) ||
836 !graph->sorted)
837 return -1;
839 for(i = 0; i < graph->n; ++i)
840 graph->sorted[i] = i;
842 return 0;
845 static void graph_free(isl_ctx *ctx, struct isl_sched_graph *graph)
847 int i;
849 isl_map_to_basic_set_free(graph->intra_hmap);
850 isl_map_to_basic_set_free(graph->inter_hmap);
852 if (graph->node)
853 for (i = 0; i < graph->n; ++i) {
854 isl_space_free(graph->node[i].space);
855 isl_set_free(graph->node[i].hull);
856 isl_multi_aff_free(graph->node[i].compress);
857 isl_multi_aff_free(graph->node[i].decompress);
858 isl_mat_free(graph->node[i].sched);
859 isl_map_free(graph->node[i].sched_map);
860 isl_mat_free(graph->node[i].cmap);
861 isl_mat_free(graph->node[i].cinv);
862 if (graph->root)
863 free(graph->node[i].coincident);
865 free(graph->node);
866 free(graph->sorted);
867 if (graph->edge)
868 for (i = 0; i < graph->n_edge; ++i) {
869 isl_map_free(graph->edge[i].map);
870 isl_union_map_free(graph->edge[i].tagged_condition);
871 isl_union_map_free(graph->edge[i].tagged_validity);
873 free(graph->edge);
874 free(graph->region);
875 for (i = 0; i <= isl_edge_last; ++i)
876 isl_hash_table_free(ctx, graph->edge_table[i]);
877 isl_hash_table_free(ctx, graph->node_table);
878 isl_basic_set_free(graph->lp);
881 /* For each "set" on which this function is called, increment
882 * graph->n by one and update graph->maxvar.
884 static int init_n_maxvar(__isl_take isl_set *set, void *user)
886 struct isl_sched_graph *graph = user;
887 int nvar = isl_set_dim(set, isl_dim_set);
889 graph->n++;
890 if (nvar > graph->maxvar)
891 graph->maxvar = nvar;
893 isl_set_free(set);
895 return 0;
898 /* Add the number of basic maps in "map" to *n.
900 static int add_n_basic_map(__isl_take isl_map *map, void *user)
902 int *n = user;
904 *n += isl_map_n_basic_map(map);
905 isl_map_free(map);
907 return 0;
910 /* Compute the number of rows that should be allocated for the schedule.
911 * In particular, we need one row for each variable or one row
912 * for each basic map in the dependences.
913 * Note that it is practically impossible to exhaust both
914 * the number of dependences and the number of variables.
916 static int compute_max_row(struct isl_sched_graph *graph,
917 __isl_keep isl_schedule_constraints *sc)
919 enum isl_edge_type i;
920 int n_edge;
922 graph->n = 0;
923 graph->maxvar = 0;
924 if (isl_union_set_foreach_set(sc->domain, &init_n_maxvar, graph) < 0)
925 return -1;
926 n_edge = 0;
927 for (i = isl_edge_first; i <= isl_edge_last; ++i)
928 if (isl_union_map_foreach_map(sc->constraint[i],
929 &add_n_basic_map, &n_edge) < 0)
930 return -1;
931 graph->max_row = n_edge + graph->maxvar;
933 return 0;
936 /* Does "bset" have any defining equalities for its set variables?
938 static int has_any_defining_equality(__isl_keep isl_basic_set *bset)
940 int i, n;
942 if (!bset)
943 return -1;
945 n = isl_basic_set_dim(bset, isl_dim_set);
946 for (i = 0; i < n; ++i) {
947 int has;
949 has = isl_basic_set_has_defining_equality(bset, isl_dim_set, i,
950 NULL);
951 if (has < 0 || has)
952 return has;
955 return 0;
958 /* Add a new node to the graph representing the given space.
959 * "nvar" is the (possibly compressed) number of variables and
960 * may be smaller than then number of set variables in "space"
961 * if "compressed" is set.
962 * If "compressed" is set, then "hull" represents the constraints
963 * that were used to derive the compression, while "compress" and
964 * "decompress" map the original space to the compressed space and
965 * vice versa.
966 * If "compressed" is not set, then "hull", "compress" and "decompress"
967 * should be NULL.
969 static int add_node(struct isl_sched_graph *graph, __isl_take isl_space *space,
970 int nvar, int compressed, __isl_take isl_set *hull,
971 __isl_take isl_multi_aff *compress,
972 __isl_take isl_multi_aff *decompress)
974 int nparam;
975 isl_ctx *ctx;
976 isl_mat *sched;
977 int *coincident;
979 if (!space)
980 return -1;
982 ctx = isl_space_get_ctx(space);
983 nparam = isl_space_dim(space, isl_dim_param);
984 if (!ctx->opt->schedule_parametric)
985 nparam = 0;
986 sched = isl_mat_alloc(ctx, 0, 1 + nparam + nvar);
987 graph->node[graph->n].space = space;
988 graph->node[graph->n].nvar = nvar;
989 graph->node[graph->n].nparam = nparam;
990 graph->node[graph->n].sched = sched;
991 graph->node[graph->n].sched_map = NULL;
992 coincident = isl_calloc_array(ctx, int, graph->max_row);
993 graph->node[graph->n].coincident = coincident;
994 graph->node[graph->n].compressed = compressed;
995 graph->node[graph->n].hull = hull;
996 graph->node[graph->n].compress = compress;
997 graph->node[graph->n].decompress = decompress;
998 graph->n++;
1000 if (!space || !sched || (graph->max_row && !coincident))
1001 return -1;
1002 if (compressed && (!hull || !compress || !decompress))
1003 return -1;
1005 return 0;
1008 /* Add a new node to the graph representing the given set.
1010 * If any of the set variables is defined by an equality, then
1011 * we perform variable compression such that we can perform
1012 * the scheduling on the compressed domain.
1014 static int extract_node(__isl_take isl_set *set, void *user)
1016 int nvar;
1017 int has_equality;
1018 isl_space *space;
1019 isl_basic_set *hull;
1020 isl_set *hull_set;
1021 isl_morph *morph;
1022 isl_multi_aff *compress, *decompress;
1023 struct isl_sched_graph *graph = user;
1025 space = isl_set_get_space(set);
1026 hull = isl_set_affine_hull(set);
1027 hull = isl_basic_set_remove_divs(hull);
1028 nvar = isl_space_dim(space, isl_dim_set);
1029 has_equality = has_any_defining_equality(hull);
1031 if (has_equality < 0)
1032 goto error;
1033 if (!has_equality) {
1034 isl_basic_set_free(hull);
1035 return add_node(graph, space, nvar, 0, NULL, NULL, NULL);
1038 morph = isl_basic_set_variable_compression(hull, isl_dim_set);
1039 nvar = isl_morph_ran_dim(morph, isl_dim_set);
1040 compress = isl_morph_get_var_multi_aff(morph);
1041 morph = isl_morph_inverse(morph);
1042 decompress = isl_morph_get_var_multi_aff(morph);
1043 isl_morph_free(morph);
1045 hull_set = isl_set_from_basic_set(hull);
1046 return add_node(graph, space, nvar, 1, hull_set, compress, decompress);
1047 error:
1048 isl_basic_set_free(hull);
1049 isl_space_free(space);
1050 return -1;
1053 struct isl_extract_edge_data {
1054 enum isl_edge_type type;
1055 struct isl_sched_graph *graph;
1058 /* Merge edge2 into edge1, freeing the contents of edge2.
1059 * "type" is the type of the schedule constraint from which edge2 was
1060 * extracted.
1061 * Return 0 on success and -1 on failure.
1063 * edge1 and edge2 are assumed to have the same value for the map field.
1065 static int merge_edge(enum isl_edge_type type, struct isl_sched_edge *edge1,
1066 struct isl_sched_edge *edge2)
1068 edge1->validity |= edge2->validity;
1069 edge1->coincidence |= edge2->coincidence;
1070 edge1->proximity |= edge2->proximity;
1071 edge1->condition |= edge2->condition;
1072 edge1->conditional_validity |= edge2->conditional_validity;
1073 isl_map_free(edge2->map);
1075 if (type == isl_edge_condition) {
1076 if (!edge1->tagged_condition)
1077 edge1->tagged_condition = edge2->tagged_condition;
1078 else
1079 edge1->tagged_condition =
1080 isl_union_map_union(edge1->tagged_condition,
1081 edge2->tagged_condition);
1084 if (type == isl_edge_conditional_validity) {
1085 if (!edge1->tagged_validity)
1086 edge1->tagged_validity = edge2->tagged_validity;
1087 else
1088 edge1->tagged_validity =
1089 isl_union_map_union(edge1->tagged_validity,
1090 edge2->tagged_validity);
1093 if (type == isl_edge_condition && !edge1->tagged_condition)
1094 return -1;
1095 if (type == isl_edge_conditional_validity && !edge1->tagged_validity)
1096 return -1;
1098 return 0;
1101 /* Insert dummy tags in domain and range of "map".
1103 * In particular, if "map" is of the form
1105 * A -> B
1107 * then return
1109 * [A -> dummy_tag] -> [B -> dummy_tag]
1111 * where the dummy_tags are identical and equal to any dummy tags
1112 * introduced by any other call to this function.
1114 static __isl_give isl_map *insert_dummy_tags(__isl_take isl_map *map)
1116 static char dummy;
1117 isl_ctx *ctx;
1118 isl_id *id;
1119 isl_space *space;
1120 isl_set *domain, *range;
1122 ctx = isl_map_get_ctx(map);
1124 id = isl_id_alloc(ctx, NULL, &dummy);
1125 space = isl_space_params(isl_map_get_space(map));
1126 space = isl_space_set_from_params(space);
1127 space = isl_space_set_tuple_id(space, isl_dim_set, id);
1128 space = isl_space_map_from_set(space);
1130 domain = isl_map_wrap(map);
1131 range = isl_map_wrap(isl_map_universe(space));
1132 map = isl_map_from_domain_and_range(domain, range);
1133 map = isl_map_zip(map);
1135 return map;
1138 /* Given that at least one of "src" or "dst" is compressed, return
1139 * a map between the spaces of these nodes restricted to the affine
1140 * hull that was used in the compression.
1142 static __isl_give isl_map *extract_hull(struct isl_sched_node *src,
1143 struct isl_sched_node *dst)
1145 isl_set *dom, *ran;
1147 if (src->compressed)
1148 dom = isl_set_copy(src->hull);
1149 else
1150 dom = isl_set_universe(isl_space_copy(src->space));
1151 if (dst->compressed)
1152 ran = isl_set_copy(dst->hull);
1153 else
1154 ran = isl_set_universe(isl_space_copy(dst->space));
1156 return isl_map_from_domain_and_range(dom, ran);
1159 /* Intersect the domains of the nested relations in domain and range
1160 * of "tagged" with "map".
1162 static __isl_give isl_map *map_intersect_domains(__isl_take isl_map *tagged,
1163 __isl_keep isl_map *map)
1165 isl_set *set;
1167 tagged = isl_map_zip(tagged);
1168 set = isl_map_wrap(isl_map_copy(map));
1169 tagged = isl_map_intersect_domain(tagged, set);
1170 tagged = isl_map_zip(tagged);
1171 return tagged;
1174 /* Add a new edge to the graph based on the given map
1175 * and add it to data->graph->edge_table[data->type].
1176 * If a dependence relation of a given type happens to be identical
1177 * to one of the dependence relations of a type that was added before,
1178 * then we don't create a new edge, but instead mark the original edge
1179 * as also representing a dependence of the current type.
1181 * Edges of type isl_edge_condition or isl_edge_conditional_validity
1182 * may be specified as "tagged" dependence relations. That is, "map"
1183 * may contain elements (i -> a) -> (j -> b), where i -> j denotes
1184 * the dependence on iterations and a and b are tags.
1185 * edge->map is set to the relation containing the elements i -> j,
1186 * while edge->tagged_condition and edge->tagged_validity contain
1187 * the union of all the "map" relations
1188 * for which extract_edge is called that result in the same edge->map.
1190 * If the source or the destination node is compressed, then
1191 * intersect both "map" and "tagged" with the constraints that
1192 * were used to construct the compression.
1193 * This ensures that there are no schedule constraints defined
1194 * outside of these domains, while the scheduler no longer has
1195 * any control over those outside parts.
1197 static int extract_edge(__isl_take isl_map *map, void *user)
1199 isl_ctx *ctx = isl_map_get_ctx(map);
1200 struct isl_extract_edge_data *data = user;
1201 struct isl_sched_graph *graph = data->graph;
1202 struct isl_sched_node *src, *dst;
1203 isl_space *dim;
1204 struct isl_sched_edge *edge;
1205 isl_map *tagged = NULL;
1207 if (data->type == isl_edge_condition ||
1208 data->type == isl_edge_conditional_validity) {
1209 if (isl_map_can_zip(map)) {
1210 tagged = isl_map_copy(map);
1211 map = isl_set_unwrap(isl_map_domain(isl_map_zip(map)));
1212 } else {
1213 tagged = insert_dummy_tags(isl_map_copy(map));
1217 dim = isl_space_domain(isl_map_get_space(map));
1218 src = graph_find_node(ctx, graph, dim);
1219 isl_space_free(dim);
1220 dim = isl_space_range(isl_map_get_space(map));
1221 dst = graph_find_node(ctx, graph, dim);
1222 isl_space_free(dim);
1224 if (!src || !dst) {
1225 isl_map_free(map);
1226 isl_map_free(tagged);
1227 return 0;
1230 if (src->compressed || dst->compressed) {
1231 isl_map *hull;
1232 hull = extract_hull(src, dst);
1233 if (tagged)
1234 tagged = map_intersect_domains(tagged, hull);
1235 map = isl_map_intersect(map, hull);
1238 graph->edge[graph->n_edge].src = src;
1239 graph->edge[graph->n_edge].dst = dst;
1240 graph->edge[graph->n_edge].map = map;
1241 graph->edge[graph->n_edge].validity = 0;
1242 graph->edge[graph->n_edge].coincidence = 0;
1243 graph->edge[graph->n_edge].proximity = 0;
1244 graph->edge[graph->n_edge].condition = 0;
1245 graph->edge[graph->n_edge].local = 0;
1246 graph->edge[graph->n_edge].conditional_validity = 0;
1247 graph->edge[graph->n_edge].tagged_condition = NULL;
1248 graph->edge[graph->n_edge].tagged_validity = NULL;
1249 if (data->type == isl_edge_validity)
1250 graph->edge[graph->n_edge].validity = 1;
1251 if (data->type == isl_edge_coincidence)
1252 graph->edge[graph->n_edge].coincidence = 1;
1253 if (data->type == isl_edge_proximity)
1254 graph->edge[graph->n_edge].proximity = 1;
1255 if (data->type == isl_edge_condition) {
1256 graph->edge[graph->n_edge].condition = 1;
1257 graph->edge[graph->n_edge].tagged_condition =
1258 isl_union_map_from_map(tagged);
1260 if (data->type == isl_edge_conditional_validity) {
1261 graph->edge[graph->n_edge].conditional_validity = 1;
1262 graph->edge[graph->n_edge].tagged_validity =
1263 isl_union_map_from_map(tagged);
1266 edge = graph_find_matching_edge(graph, &graph->edge[graph->n_edge]);
1267 if (!edge) {
1268 graph->n_edge++;
1269 return -1;
1271 if (edge == &graph->edge[graph->n_edge])
1272 return graph_edge_table_add(ctx, graph, data->type,
1273 &graph->edge[graph->n_edge++]);
1275 if (merge_edge(data->type, edge, &graph->edge[graph->n_edge]) < 0)
1276 return -1;
1278 return graph_edge_table_add(ctx, graph, data->type, edge);
1281 /* Check whether there is any dependence from node[j] to node[i]
1282 * or from node[i] to node[j].
1284 static int node_follows_weak(int i, int j, void *user)
1286 int f;
1287 struct isl_sched_graph *graph = user;
1289 f = graph_has_any_edge(graph, &graph->node[j], &graph->node[i]);
1290 if (f < 0 || f)
1291 return f;
1292 return graph_has_any_edge(graph, &graph->node[i], &graph->node[j]);
1295 /* Check whether there is a (conditional) validity dependence from node[j]
1296 * to node[i], forcing node[i] to follow node[j].
1298 static int node_follows_strong(int i, int j, void *user)
1300 struct isl_sched_graph *graph = user;
1302 return graph_has_validity_edge(graph, &graph->node[j], &graph->node[i]);
1305 /* Use Tarjan's algorithm for computing the strongly connected components
1306 * in the dependence graph (only validity edges).
1307 * If weak is set, we consider the graph to be undirected and
1308 * we effectively compute the (weakly) connected components.
1309 * Additionally, we also consider other edges when weak is set.
1311 static int detect_ccs(isl_ctx *ctx, struct isl_sched_graph *graph, int weak)
1313 int i, n;
1314 struct isl_tarjan_graph *g = NULL;
1316 g = isl_tarjan_graph_init(ctx, graph->n,
1317 weak ? &node_follows_weak : &node_follows_strong, graph);
1318 if (!g)
1319 return -1;
1321 graph->weak = weak;
1322 graph->scc = 0;
1323 i = 0;
1324 n = graph->n;
1325 while (n) {
1326 while (g->order[i] != -1) {
1327 graph->node[g->order[i]].scc = graph->scc;
1328 --n;
1329 ++i;
1331 ++i;
1332 graph->scc++;
1335 isl_tarjan_graph_free(g);
1337 return 0;
1340 /* Apply Tarjan's algorithm to detect the strongly connected components
1341 * in the dependence graph.
1343 static int detect_sccs(isl_ctx *ctx, struct isl_sched_graph *graph)
1345 return detect_ccs(ctx, graph, 0);
1348 /* Apply Tarjan's algorithm to detect the (weakly) connected components
1349 * in the dependence graph.
1351 static int detect_wccs(isl_ctx *ctx, struct isl_sched_graph *graph)
1353 return detect_ccs(ctx, graph, 1);
1356 static int cmp_scc(const void *a, const void *b, void *data)
1358 struct isl_sched_graph *graph = data;
1359 const int *i1 = a;
1360 const int *i2 = b;
1362 return graph->node[*i1].scc - graph->node[*i2].scc;
1365 /* Sort the elements of graph->sorted according to the corresponding SCCs.
1367 static int sort_sccs(struct isl_sched_graph *graph)
1369 return isl_sort(graph->sorted, graph->n, sizeof(int), &cmp_scc, graph);
1372 /* Given a dependence relation R from "node" to itself,
1373 * construct the set of coefficients of valid constraints for elements
1374 * in that dependence relation.
1375 * In particular, the result contains tuples of coefficients
1376 * c_0, c_n, c_x such that
1378 * c_0 + c_n n + c_x y - c_x x >= 0 for each (x,y) in R
1380 * or, equivalently,
1382 * c_0 + c_n n + c_x d >= 0 for each d in delta R = { y - x | (x,y) in R }
1384 * We choose here to compute the dual of delta R.
1385 * Alternatively, we could have computed the dual of R, resulting
1386 * in a set of tuples c_0, c_n, c_x, c_y, and then
1387 * plugged in (c_0, c_n, c_x, -c_x).
1389 * If "node" has been compressed, then the dependence relation
1390 * is also compressed before the set of coefficients is computed.
1392 static __isl_give isl_basic_set *intra_coefficients(
1393 struct isl_sched_graph *graph, struct isl_sched_node *node,
1394 __isl_take isl_map *map)
1396 isl_set *delta;
1397 isl_map *key;
1398 isl_basic_set *coef;
1400 if (isl_map_to_basic_set_has(graph->intra_hmap, map))
1401 return isl_map_to_basic_set_get(graph->intra_hmap, map);
1403 key = isl_map_copy(map);
1404 if (node->compressed) {
1405 map = isl_map_preimage_domain_multi_aff(map,
1406 isl_multi_aff_copy(node->decompress));
1407 map = isl_map_preimage_range_multi_aff(map,
1408 isl_multi_aff_copy(node->decompress));
1410 delta = isl_set_remove_divs(isl_map_deltas(map));
1411 coef = isl_set_coefficients(delta);
1412 graph->intra_hmap = isl_map_to_basic_set_set(graph->intra_hmap, key,
1413 isl_basic_set_copy(coef));
1415 return coef;
1418 /* Given a dependence relation R, construct the set of coefficients
1419 * of valid constraints for elements in that dependence relation.
1420 * In particular, the result contains tuples of coefficients
1421 * c_0, c_n, c_x, c_y such that
1423 * c_0 + c_n n + c_x x + c_y y >= 0 for each (x,y) in R
1425 * If the source or destination nodes of "edge" have been compressed,
1426 * then the dependence relation is also compressed before
1427 * the set of coefficients is computed.
1429 static __isl_give isl_basic_set *inter_coefficients(
1430 struct isl_sched_graph *graph, struct isl_sched_edge *edge,
1431 __isl_take isl_map *map)
1433 isl_set *set;
1434 isl_map *key;
1435 isl_basic_set *coef;
1437 if (isl_map_to_basic_set_has(graph->inter_hmap, map))
1438 return isl_map_to_basic_set_get(graph->inter_hmap, map);
1440 key = isl_map_copy(map);
1441 if (edge->src->compressed)
1442 map = isl_map_preimage_domain_multi_aff(map,
1443 isl_multi_aff_copy(edge->src->decompress));
1444 if (edge->dst->compressed)
1445 map = isl_map_preimage_range_multi_aff(map,
1446 isl_multi_aff_copy(edge->dst->decompress));
1447 set = isl_map_wrap(isl_map_remove_divs(map));
1448 coef = isl_set_coefficients(set);
1449 graph->inter_hmap = isl_map_to_basic_set_set(graph->inter_hmap, key,
1450 isl_basic_set_copy(coef));
1452 return coef;
1455 /* Add constraints to graph->lp that force validity for the given
1456 * dependence from a node i to itself.
1457 * That is, add constraints that enforce
1459 * (c_i_0 + c_i_n n + c_i_x y) - (c_i_0 + c_i_n n + c_i_x x)
1460 * = c_i_x (y - x) >= 0
1462 * for each (x,y) in R.
1463 * We obtain general constraints on coefficients (c_0, c_n, c_x)
1464 * of valid constraints for (y - x) and then plug in (0, 0, c_i_x^+ - c_i_x^-),
1465 * where c_i_x = c_i_x^+ - c_i_x^-, with c_i_x^+ and c_i_x^- non-negative.
1466 * In graph->lp, the c_i_x^- appear before their c_i_x^+ counterpart.
1468 * Actually, we do not construct constraints for the c_i_x themselves,
1469 * but for the coefficients of c_i_x written as a linear combination
1470 * of the columns in node->cmap.
1472 static int add_intra_validity_constraints(struct isl_sched_graph *graph,
1473 struct isl_sched_edge *edge)
1475 unsigned total;
1476 isl_map *map = isl_map_copy(edge->map);
1477 isl_ctx *ctx = isl_map_get_ctx(map);
1478 isl_space *dim;
1479 isl_dim_map *dim_map;
1480 isl_basic_set *coef;
1481 struct isl_sched_node *node = edge->src;
1483 coef = intra_coefficients(graph, node, map);
1485 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
1487 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
1488 isl_space_dim(dim, isl_dim_set), isl_mat_copy(node->cmap));
1489 if (!coef)
1490 goto error;
1492 total = isl_basic_set_total_dim(graph->lp);
1493 dim_map = isl_dim_map_alloc(ctx, total);
1494 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 1, 2,
1495 isl_space_dim(dim, isl_dim_set), 1,
1496 node->nvar, -1);
1497 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 2, 2,
1498 isl_space_dim(dim, isl_dim_set), 1,
1499 node->nvar, 1);
1500 graph->lp = isl_basic_set_extend_constraints(graph->lp,
1501 coef->n_eq, coef->n_ineq);
1502 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
1503 coef, dim_map);
1504 isl_space_free(dim);
1506 return 0;
1507 error:
1508 isl_space_free(dim);
1509 return -1;
1512 /* Add constraints to graph->lp that force validity for the given
1513 * dependence from node i to node j.
1514 * That is, add constraints that enforce
1516 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x) >= 0
1518 * for each (x,y) in R.
1519 * We obtain general constraints on coefficients (c_0, c_n, c_x, c_y)
1520 * of valid constraints for R and then plug in
1521 * (c_j_0 - c_i_0, c_j_n^+ - c_j_n^- - (c_i_n^+ - c_i_n^-),
1522 * c_j_x^+ - c_j_x^- - (c_i_x^+ - c_i_x^-)),
1523 * where c_* = c_*^+ - c_*^-, with c_*^+ and c_*^- non-negative.
1524 * In graph->lp, the c_*^- appear before their c_*^+ counterpart.
1526 * Actually, we do not construct constraints for the c_*_x themselves,
1527 * but for the coefficients of c_*_x written as a linear combination
1528 * of the columns in node->cmap.
1530 static int add_inter_validity_constraints(struct isl_sched_graph *graph,
1531 struct isl_sched_edge *edge)
1533 unsigned total;
1534 isl_map *map = isl_map_copy(edge->map);
1535 isl_ctx *ctx = isl_map_get_ctx(map);
1536 isl_space *dim;
1537 isl_dim_map *dim_map;
1538 isl_basic_set *coef;
1539 struct isl_sched_node *src = edge->src;
1540 struct isl_sched_node *dst = edge->dst;
1542 coef = inter_coefficients(graph, edge, map);
1544 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
1546 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
1547 isl_space_dim(dim, isl_dim_set), isl_mat_copy(src->cmap));
1548 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
1549 isl_space_dim(dim, isl_dim_set) + src->nvar,
1550 isl_mat_copy(dst->cmap));
1551 if (!coef)
1552 goto error;
1554 total = isl_basic_set_total_dim(graph->lp);
1555 dim_map = isl_dim_map_alloc(ctx, total);
1557 isl_dim_map_range(dim_map, dst->start, 0, 0, 0, 1, 1);
1558 isl_dim_map_range(dim_map, dst->start + 1, 2, 1, 1, dst->nparam, -1);
1559 isl_dim_map_range(dim_map, dst->start + 2, 2, 1, 1, dst->nparam, 1);
1560 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 1, 2,
1561 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
1562 dst->nvar, -1);
1563 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 2, 2,
1564 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
1565 dst->nvar, 1);
1567 isl_dim_map_range(dim_map, src->start, 0, 0, 0, 1, -1);
1568 isl_dim_map_range(dim_map, src->start + 1, 2, 1, 1, src->nparam, 1);
1569 isl_dim_map_range(dim_map, src->start + 2, 2, 1, 1, src->nparam, -1);
1570 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 1, 2,
1571 isl_space_dim(dim, isl_dim_set), 1,
1572 src->nvar, 1);
1573 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 2, 2,
1574 isl_space_dim(dim, isl_dim_set), 1,
1575 src->nvar, -1);
1577 edge->start = graph->lp->n_ineq;
1578 graph->lp = isl_basic_set_extend_constraints(graph->lp,
1579 coef->n_eq, coef->n_ineq);
1580 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
1581 coef, dim_map);
1582 if (!graph->lp)
1583 goto error;
1584 isl_space_free(dim);
1585 edge->end = graph->lp->n_ineq;
1587 return 0;
1588 error:
1589 isl_space_free(dim);
1590 return -1;
1593 /* Add constraints to graph->lp that bound the dependence distance for the given
1594 * dependence from a node i to itself.
1595 * If s = 1, we add the constraint
1597 * c_i_x (y - x) <= m_0 + m_n n
1599 * or
1601 * -c_i_x (y - x) + m_0 + m_n n >= 0
1603 * for each (x,y) in R.
1604 * If s = -1, we add the constraint
1606 * -c_i_x (y - x) <= m_0 + m_n n
1608 * or
1610 * c_i_x (y - x) + m_0 + m_n n >= 0
1612 * for each (x,y) in R.
1613 * We obtain general constraints on coefficients (c_0, c_n, c_x)
1614 * of valid constraints for (y - x) and then plug in (m_0, m_n, -s * c_i_x),
1615 * with each coefficient (except m_0) represented as a pair of non-negative
1616 * coefficients.
1618 * Actually, we do not construct constraints for the c_i_x themselves,
1619 * but for the coefficients of c_i_x written as a linear combination
1620 * of the columns in node->cmap.
1623 * If "local" is set, then we add constraints
1625 * c_i_x (y - x) <= 0
1627 * or
1629 * -c_i_x (y - x) <= 0
1631 * instead, forcing the dependence distance to be (less than or) equal to 0.
1632 * That is, we plug in (0, 0, -s * c_i_x),
1633 * Note that dependences marked local are treated as validity constraints
1634 * by add_all_validity_constraints and therefore also have
1635 * their distances bounded by 0 from below.
1637 static int add_intra_proximity_constraints(struct isl_sched_graph *graph,
1638 struct isl_sched_edge *edge, int s, int local)
1640 unsigned total;
1641 unsigned nparam;
1642 isl_map *map = isl_map_copy(edge->map);
1643 isl_ctx *ctx = isl_map_get_ctx(map);
1644 isl_space *dim;
1645 isl_dim_map *dim_map;
1646 isl_basic_set *coef;
1647 struct isl_sched_node *node = edge->src;
1649 coef = intra_coefficients(graph, node, map);
1651 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
1653 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
1654 isl_space_dim(dim, isl_dim_set), isl_mat_copy(node->cmap));
1655 if (!coef)
1656 goto error;
1658 nparam = isl_space_dim(node->space, isl_dim_param);
1659 total = isl_basic_set_total_dim(graph->lp);
1660 dim_map = isl_dim_map_alloc(ctx, total);
1662 if (!local) {
1663 isl_dim_map_range(dim_map, 1, 0, 0, 0, 1, 1);
1664 isl_dim_map_range(dim_map, 4, 2, 1, 1, nparam, -1);
1665 isl_dim_map_range(dim_map, 5, 2, 1, 1, nparam, 1);
1667 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 1, 2,
1668 isl_space_dim(dim, isl_dim_set), 1,
1669 node->nvar, s);
1670 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 2, 2,
1671 isl_space_dim(dim, isl_dim_set), 1,
1672 node->nvar, -s);
1673 graph->lp = isl_basic_set_extend_constraints(graph->lp,
1674 coef->n_eq, coef->n_ineq);
1675 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
1676 coef, dim_map);
1677 isl_space_free(dim);
1679 return 0;
1680 error:
1681 isl_space_free(dim);
1682 return -1;
1685 /* Add constraints to graph->lp that bound the dependence distance for the given
1686 * dependence from node i to node j.
1687 * If s = 1, we add the constraint
1689 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x)
1690 * <= m_0 + m_n n
1692 * or
1694 * -(c_j_0 + c_j_n n + c_j_x y) + (c_i_0 + c_i_n n + c_i_x x) +
1695 * m_0 + m_n n >= 0
1697 * for each (x,y) in R.
1698 * If s = -1, we add the constraint
1700 * -((c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x))
1701 * <= m_0 + m_n n
1703 * or
1705 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x) +
1706 * m_0 + m_n n >= 0
1708 * for each (x,y) in R.
1709 * We obtain general constraints on coefficients (c_0, c_n, c_x, c_y)
1710 * of valid constraints for R and then plug in
1711 * (m_0 - s*c_j_0 + s*c_i_0, m_n - s*c_j_n + s*c_i_n,
1712 * -s*c_j_x+s*c_i_x)
1713 * with each coefficient (except m_0, c_j_0 and c_i_0)
1714 * represented as a pair of non-negative coefficients.
1716 * Actually, we do not construct constraints for the c_*_x themselves,
1717 * but for the coefficients of c_*_x written as a linear combination
1718 * of the columns in node->cmap.
1721 * If "local" is set, then we add constraints
1723 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x) <= 0
1725 * or
1727 * -((c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x)) <= 0
1729 * instead, forcing the dependence distance to be (less than or) equal to 0.
1730 * That is, we plug in
1731 * (-s*c_j_0 + s*c_i_0, -s*c_j_n + s*c_i_n, -s*c_j_x+s*c_i_x).
1732 * Note that dependences marked local are treated as validity constraints
1733 * by add_all_validity_constraints and therefore also have
1734 * their distances bounded by 0 from below.
1736 static int add_inter_proximity_constraints(struct isl_sched_graph *graph,
1737 struct isl_sched_edge *edge, int s, int local)
1739 unsigned total;
1740 unsigned nparam;
1741 isl_map *map = isl_map_copy(edge->map);
1742 isl_ctx *ctx = isl_map_get_ctx(map);
1743 isl_space *dim;
1744 isl_dim_map *dim_map;
1745 isl_basic_set *coef;
1746 struct isl_sched_node *src = edge->src;
1747 struct isl_sched_node *dst = edge->dst;
1749 coef = inter_coefficients(graph, edge, map);
1751 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
1753 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
1754 isl_space_dim(dim, isl_dim_set), isl_mat_copy(src->cmap));
1755 coef = isl_basic_set_transform_dims(coef, isl_dim_set,
1756 isl_space_dim(dim, isl_dim_set) + src->nvar,
1757 isl_mat_copy(dst->cmap));
1758 if (!coef)
1759 goto error;
1761 nparam = isl_space_dim(src->space, isl_dim_param);
1762 total = isl_basic_set_total_dim(graph->lp);
1763 dim_map = isl_dim_map_alloc(ctx, total);
1765 if (!local) {
1766 isl_dim_map_range(dim_map, 1, 0, 0, 0, 1, 1);
1767 isl_dim_map_range(dim_map, 4, 2, 1, 1, nparam, -1);
1768 isl_dim_map_range(dim_map, 5, 2, 1, 1, nparam, 1);
1771 isl_dim_map_range(dim_map, dst->start, 0, 0, 0, 1, -s);
1772 isl_dim_map_range(dim_map, dst->start + 1, 2, 1, 1, dst->nparam, s);
1773 isl_dim_map_range(dim_map, dst->start + 2, 2, 1, 1, dst->nparam, -s);
1774 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 1, 2,
1775 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
1776 dst->nvar, s);
1777 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 2, 2,
1778 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
1779 dst->nvar, -s);
1781 isl_dim_map_range(dim_map, src->start, 0, 0, 0, 1, s);
1782 isl_dim_map_range(dim_map, src->start + 1, 2, 1, 1, src->nparam, -s);
1783 isl_dim_map_range(dim_map, src->start + 2, 2, 1, 1, src->nparam, s);
1784 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 1, 2,
1785 isl_space_dim(dim, isl_dim_set), 1,
1786 src->nvar, -s);
1787 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 2, 2,
1788 isl_space_dim(dim, isl_dim_set), 1,
1789 src->nvar, s);
1791 graph->lp = isl_basic_set_extend_constraints(graph->lp,
1792 coef->n_eq, coef->n_ineq);
1793 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
1794 coef, dim_map);
1795 isl_space_free(dim);
1797 return 0;
1798 error:
1799 isl_space_free(dim);
1800 return -1;
1803 /* Add all validity constraints to graph->lp.
1805 * An edge that is forced to be local needs to have its dependence
1806 * distances equal to zero. We take care of bounding them by 0 from below
1807 * here. add_all_proximity_constraints takes care of bounding them by 0
1808 * from above.
1810 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
1811 * Otherwise, we ignore them.
1813 static int add_all_validity_constraints(struct isl_sched_graph *graph,
1814 int use_coincidence)
1816 int i;
1818 for (i = 0; i < graph->n_edge; ++i) {
1819 struct isl_sched_edge *edge= &graph->edge[i];
1820 int local;
1822 local = edge->local || (edge->coincidence && use_coincidence);
1823 if (!edge->validity && !local)
1824 continue;
1825 if (edge->src != edge->dst)
1826 continue;
1827 if (add_intra_validity_constraints(graph, edge) < 0)
1828 return -1;
1831 for (i = 0; i < graph->n_edge; ++i) {
1832 struct isl_sched_edge *edge = &graph->edge[i];
1833 int local;
1835 local = edge->local || (edge->coincidence && use_coincidence);
1836 if (!edge->validity && !local)
1837 continue;
1838 if (edge->src == edge->dst)
1839 continue;
1840 if (add_inter_validity_constraints(graph, edge) < 0)
1841 return -1;
1844 return 0;
1847 /* Add constraints to graph->lp that bound the dependence distance
1848 * for all dependence relations.
1849 * If a given proximity dependence is identical to a validity
1850 * dependence, then the dependence distance is already bounded
1851 * from below (by zero), so we only need to bound the distance
1852 * from above. (This includes the case of "local" dependences
1853 * which are treated as validity dependence by add_all_validity_constraints.)
1854 * Otherwise, we need to bound the distance both from above and from below.
1856 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
1857 * Otherwise, we ignore them.
1859 static int add_all_proximity_constraints(struct isl_sched_graph *graph,
1860 int use_coincidence)
1862 int i;
1864 for (i = 0; i < graph->n_edge; ++i) {
1865 struct isl_sched_edge *edge= &graph->edge[i];
1866 int local;
1868 local = edge->local || (edge->coincidence && use_coincidence);
1869 if (!edge->proximity && !local)
1870 continue;
1871 if (edge->src == edge->dst &&
1872 add_intra_proximity_constraints(graph, edge, 1, local) < 0)
1873 return -1;
1874 if (edge->src != edge->dst &&
1875 add_inter_proximity_constraints(graph, edge, 1, local) < 0)
1876 return -1;
1877 if (edge->validity || local)
1878 continue;
1879 if (edge->src == edge->dst &&
1880 add_intra_proximity_constraints(graph, edge, -1, 0) < 0)
1881 return -1;
1882 if (edge->src != edge->dst &&
1883 add_inter_proximity_constraints(graph, edge, -1, 0) < 0)
1884 return -1;
1887 return 0;
1890 /* Compute a basis for the rows in the linear part of the schedule
1891 * and extend this basis to a full basis. The remaining rows
1892 * can then be used to force linear independence from the rows
1893 * in the schedule.
1895 * In particular, given the schedule rows S, we compute
1897 * S = H Q
1898 * S U = H
1900 * with H the Hermite normal form of S. That is, all but the
1901 * first rank columns of H are zero and so each row in S is
1902 * a linear combination of the first rank rows of Q.
1903 * The matrix Q is then transposed because we will write the
1904 * coefficients of the next schedule row as a column vector s
1905 * and express this s as a linear combination s = Q c of the
1906 * computed basis.
1907 * Similarly, the matrix U is transposed such that we can
1908 * compute the coefficients c = U s from a schedule row s.
1910 static int node_update_cmap(struct isl_sched_node *node)
1912 isl_mat *H, *U, *Q;
1913 int n_row = isl_mat_rows(node->sched);
1915 H = isl_mat_sub_alloc(node->sched, 0, n_row,
1916 1 + node->nparam, node->nvar);
1918 H = isl_mat_left_hermite(H, 0, &U, &Q);
1919 isl_mat_free(node->cmap);
1920 isl_mat_free(node->cinv);
1921 node->cmap = isl_mat_transpose(Q);
1922 node->cinv = isl_mat_transpose(U);
1923 node->rank = isl_mat_initial_non_zero_cols(H);
1924 isl_mat_free(H);
1926 if (!node->cmap || !node->cinv || node->rank < 0)
1927 return -1;
1928 return 0;
1931 /* How many times should we count the constraints in "edge"?
1933 * If carry is set, then we are counting the number of
1934 * (validity or conditional validity) constraints that will be added
1935 * in setup_carry_lp and we count each edge exactly once.
1937 * Otherwise, we count as follows
1938 * validity -> 1 (>= 0)
1939 * validity+proximity -> 2 (>= 0 and upper bound)
1940 * proximity -> 2 (lower and upper bound)
1941 * local(+any) -> 2 (>= 0 and <= 0)
1943 * If an edge is only marked conditional_validity then it counts
1944 * as zero since it is only checked afterwards.
1946 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
1947 * Otherwise, we ignore them.
1949 static int edge_multiplicity(struct isl_sched_edge *edge, int carry,
1950 int use_coincidence)
1952 if (carry && !edge->validity && !edge->conditional_validity)
1953 return 0;
1954 if (carry)
1955 return 1;
1956 if (edge->proximity || edge->local)
1957 return 2;
1958 if (use_coincidence && edge->coincidence)
1959 return 2;
1960 if (edge->validity)
1961 return 1;
1962 return 0;
1965 /* Count the number of equality and inequality constraints
1966 * that will be added for the given map.
1968 * "use_coincidence" is set if we should take into account coincidence edges.
1970 static int count_map_constraints(struct isl_sched_graph *graph,
1971 struct isl_sched_edge *edge, __isl_take isl_map *map,
1972 int *n_eq, int *n_ineq, int carry, int use_coincidence)
1974 isl_basic_set *coef;
1975 int f = edge_multiplicity(edge, carry, use_coincidence);
1977 if (f == 0) {
1978 isl_map_free(map);
1979 return 0;
1982 if (edge->src == edge->dst)
1983 coef = intra_coefficients(graph, edge->src, map);
1984 else
1985 coef = inter_coefficients(graph, edge, map);
1986 if (!coef)
1987 return -1;
1988 *n_eq += f * coef->n_eq;
1989 *n_ineq += f * coef->n_ineq;
1990 isl_basic_set_free(coef);
1992 return 0;
1995 /* Count the number of equality and inequality constraints
1996 * that will be added to the main lp problem.
1997 * We count as follows
1998 * validity -> 1 (>= 0)
1999 * validity+proximity -> 2 (>= 0 and upper bound)
2000 * proximity -> 2 (lower and upper bound)
2001 * local(+any) -> 2 (>= 0 and <= 0)
2003 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
2004 * Otherwise, we ignore them.
2006 static int count_constraints(struct isl_sched_graph *graph,
2007 int *n_eq, int *n_ineq, int use_coincidence)
2009 int i;
2011 *n_eq = *n_ineq = 0;
2012 for (i = 0; i < graph->n_edge; ++i) {
2013 struct isl_sched_edge *edge= &graph->edge[i];
2014 isl_map *map = isl_map_copy(edge->map);
2016 if (count_map_constraints(graph, edge, map, n_eq, n_ineq,
2017 0, use_coincidence) < 0)
2018 return -1;
2021 return 0;
2024 /* Count the number of constraints that will be added by
2025 * add_bound_coefficient_constraints and increment *n_eq and *n_ineq
2026 * accordingly.
2028 * In practice, add_bound_coefficient_constraints only adds inequalities.
2030 static int count_bound_coefficient_constraints(isl_ctx *ctx,
2031 struct isl_sched_graph *graph, int *n_eq, int *n_ineq)
2033 int i;
2035 if (ctx->opt->schedule_max_coefficient == -1)
2036 return 0;
2038 for (i = 0; i < graph->n; ++i)
2039 *n_ineq += 2 * graph->node[i].nparam + 2 * graph->node[i].nvar;
2041 return 0;
2044 /* Add constraints that bound the values of the variable and parameter
2045 * coefficients of the schedule.
2047 * The maximal value of the coefficients is defined by the option
2048 * 'schedule_max_coefficient'.
2050 static int add_bound_coefficient_constraints(isl_ctx *ctx,
2051 struct isl_sched_graph *graph)
2053 int i, j, k;
2054 int max_coefficient;
2055 int total;
2057 max_coefficient = ctx->opt->schedule_max_coefficient;
2059 if (max_coefficient == -1)
2060 return 0;
2062 total = isl_basic_set_total_dim(graph->lp);
2064 for (i = 0; i < graph->n; ++i) {
2065 struct isl_sched_node *node = &graph->node[i];
2066 for (j = 0; j < 2 * node->nparam + 2 * node->nvar; ++j) {
2067 int dim;
2068 k = isl_basic_set_alloc_inequality(graph->lp);
2069 if (k < 0)
2070 return -1;
2071 dim = 1 + node->start + 1 + j;
2072 isl_seq_clr(graph->lp->ineq[k], 1 + total);
2073 isl_int_set_si(graph->lp->ineq[k][dim], -1);
2074 isl_int_set_si(graph->lp->ineq[k][0], max_coefficient);
2078 return 0;
2081 /* Construct an ILP problem for finding schedule coefficients
2082 * that result in non-negative, but small dependence distances
2083 * over all dependences.
2084 * In particular, the dependence distances over proximity edges
2085 * are bounded by m_0 + m_n n and we compute schedule coefficients
2086 * with small values (preferably zero) of m_n and m_0.
2088 * All variables of the ILP are non-negative. The actual coefficients
2089 * may be negative, so each coefficient is represented as the difference
2090 * of two non-negative variables. The negative part always appears
2091 * immediately before the positive part.
2092 * Other than that, the variables have the following order
2094 * - sum of positive and negative parts of m_n coefficients
2095 * - m_0
2096 * - sum of positive and negative parts of all c_n coefficients
2097 * (unconstrained when computing non-parametric schedules)
2098 * - sum of positive and negative parts of all c_x coefficients
2099 * - positive and negative parts of m_n coefficients
2100 * - for each node
2101 * - c_i_0
2102 * - positive and negative parts of c_i_n (if parametric)
2103 * - positive and negative parts of c_i_x
2105 * The c_i_x are not represented directly, but through the columns of
2106 * node->cmap. That is, the computed values are for variable t_i_x
2107 * such that c_i_x = Q t_i_x with Q equal to node->cmap.
2109 * The constraints are those from the edges plus two or three equalities
2110 * to express the sums.
2112 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
2113 * Otherwise, we ignore them.
2115 static int setup_lp(isl_ctx *ctx, struct isl_sched_graph *graph,
2116 int use_coincidence)
2118 int i, j;
2119 int k;
2120 unsigned nparam;
2121 unsigned total;
2122 isl_space *dim;
2123 int parametric;
2124 int param_pos;
2125 int n_eq, n_ineq;
2126 int max_constant_term;
2128 max_constant_term = ctx->opt->schedule_max_constant_term;
2130 parametric = ctx->opt->schedule_parametric;
2131 nparam = isl_space_dim(graph->node[0].space, isl_dim_param);
2132 param_pos = 4;
2133 total = param_pos + 2 * nparam;
2134 for (i = 0; i < graph->n; ++i) {
2135 struct isl_sched_node *node = &graph->node[graph->sorted[i]];
2136 if (node_update_cmap(node) < 0)
2137 return -1;
2138 node->start = total;
2139 total += 1 + 2 * (node->nparam + node->nvar);
2142 if (count_constraints(graph, &n_eq, &n_ineq, use_coincidence) < 0)
2143 return -1;
2144 if (count_bound_coefficient_constraints(ctx, graph, &n_eq, &n_ineq) < 0)
2145 return -1;
2147 dim = isl_space_set_alloc(ctx, 0, total);
2148 isl_basic_set_free(graph->lp);
2149 n_eq += 2 + parametric;
2150 if (max_constant_term != -1)
2151 n_ineq += graph->n;
2153 graph->lp = isl_basic_set_alloc_space(dim, 0, n_eq, n_ineq);
2155 k = isl_basic_set_alloc_equality(graph->lp);
2156 if (k < 0)
2157 return -1;
2158 isl_seq_clr(graph->lp->eq[k], 1 + total);
2159 isl_int_set_si(graph->lp->eq[k][1], -1);
2160 for (i = 0; i < 2 * nparam; ++i)
2161 isl_int_set_si(graph->lp->eq[k][1 + param_pos + i], 1);
2163 if (parametric) {
2164 k = isl_basic_set_alloc_equality(graph->lp);
2165 if (k < 0)
2166 return -1;
2167 isl_seq_clr(graph->lp->eq[k], 1 + total);
2168 isl_int_set_si(graph->lp->eq[k][3], -1);
2169 for (i = 0; i < graph->n; ++i) {
2170 int pos = 1 + graph->node[i].start + 1;
2172 for (j = 0; j < 2 * graph->node[i].nparam; ++j)
2173 isl_int_set_si(graph->lp->eq[k][pos + j], 1);
2177 k = isl_basic_set_alloc_equality(graph->lp);
2178 if (k < 0)
2179 return -1;
2180 isl_seq_clr(graph->lp->eq[k], 1 + total);
2181 isl_int_set_si(graph->lp->eq[k][4], -1);
2182 for (i = 0; i < graph->n; ++i) {
2183 struct isl_sched_node *node = &graph->node[i];
2184 int pos = 1 + node->start + 1 + 2 * node->nparam;
2186 for (j = 0; j < 2 * node->nvar; ++j)
2187 isl_int_set_si(graph->lp->eq[k][pos + j], 1);
2190 if (max_constant_term != -1)
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 -1;
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_constant_term);
2201 if (add_bound_coefficient_constraints(ctx, graph) < 0)
2202 return -1;
2203 if (add_all_validity_constraints(graph, use_coincidence) < 0)
2204 return -1;
2205 if (add_all_proximity_constraints(graph, use_coincidence) < 0)
2206 return -1;
2208 return 0;
2211 /* Analyze the conflicting constraint found by
2212 * isl_tab_basic_set_non_trivial_lexmin. If it corresponds to the validity
2213 * constraint of one of the edges between distinct nodes, living, moreover
2214 * in distinct SCCs, then record the source and sink SCC as this may
2215 * be a good place to cut between SCCs.
2217 static int check_conflict(int con, void *user)
2219 int i;
2220 struct isl_sched_graph *graph = user;
2222 if (graph->src_scc >= 0)
2223 return 0;
2225 con -= graph->lp->n_eq;
2227 if (con >= graph->lp->n_ineq)
2228 return 0;
2230 for (i = 0; i < graph->n_edge; ++i) {
2231 if (!graph->edge[i].validity)
2232 continue;
2233 if (graph->edge[i].src == graph->edge[i].dst)
2234 continue;
2235 if (graph->edge[i].src->scc == graph->edge[i].dst->scc)
2236 continue;
2237 if (graph->edge[i].start > con)
2238 continue;
2239 if (graph->edge[i].end <= con)
2240 continue;
2241 graph->src_scc = graph->edge[i].src->scc;
2242 graph->dst_scc = graph->edge[i].dst->scc;
2245 return 0;
2248 /* Check whether the next schedule row of the given node needs to be
2249 * non-trivial. Lower-dimensional domains may have some trivial rows,
2250 * but as soon as the number of remaining required non-trivial rows
2251 * is as large as the number or remaining rows to be computed,
2252 * all remaining rows need to be non-trivial.
2254 static int needs_row(struct isl_sched_graph *graph, struct isl_sched_node *node)
2256 return node->nvar - node->rank >= graph->maxvar - graph->n_row;
2259 /* Solve the ILP problem constructed in setup_lp.
2260 * For each node such that all the remaining rows of its schedule
2261 * need to be non-trivial, we construct a non-triviality region.
2262 * This region imposes that the next row is independent of previous rows.
2263 * In particular the coefficients c_i_x are represented by t_i_x
2264 * variables with c_i_x = Q t_i_x and Q a unimodular matrix such that
2265 * its first columns span the rows of the previously computed part
2266 * of the schedule. The non-triviality region enforces that at least
2267 * one of the remaining components of t_i_x is non-zero, i.e.,
2268 * that the new schedule row depends on at least one of the remaining
2269 * columns of Q.
2271 static __isl_give isl_vec *solve_lp(struct isl_sched_graph *graph)
2273 int i;
2274 isl_vec *sol;
2275 isl_basic_set *lp;
2277 for (i = 0; i < graph->n; ++i) {
2278 struct isl_sched_node *node = &graph->node[i];
2279 int skip = node->rank;
2280 graph->region[i].pos = node->start + 1 + 2*(node->nparam+skip);
2281 if (needs_row(graph, node))
2282 graph->region[i].len = 2 * (node->nvar - skip);
2283 else
2284 graph->region[i].len = 0;
2286 lp = isl_basic_set_copy(graph->lp);
2287 sol = isl_tab_basic_set_non_trivial_lexmin(lp, 2, graph->n,
2288 graph->region, &check_conflict, graph);
2289 return sol;
2292 /* Update the schedules of all nodes based on the given solution
2293 * of the LP problem.
2294 * The new row is added to the current band.
2295 * All possibly negative coefficients are encoded as a difference
2296 * of two non-negative variables, so we need to perform the subtraction
2297 * here. Moreover, if use_cmap is set, then the solution does
2298 * not refer to the actual coefficients c_i_x, but instead to variables
2299 * t_i_x such that c_i_x = Q t_i_x and Q is equal to node->cmap.
2300 * In this case, we then also need to perform this multiplication
2301 * to obtain the values of c_i_x.
2303 * If coincident is set, then the caller guarantees that the new
2304 * row satisfies the coincidence constraints.
2306 static int update_schedule(struct isl_sched_graph *graph,
2307 __isl_take isl_vec *sol, int use_cmap, int coincident)
2309 int i, j;
2310 isl_vec *csol = NULL;
2312 if (!sol)
2313 goto error;
2314 if (sol->size == 0)
2315 isl_die(sol->ctx, isl_error_internal,
2316 "no solution found", goto error);
2317 if (graph->n_total_row >= graph->max_row)
2318 isl_die(sol->ctx, isl_error_internal,
2319 "too many schedule rows", goto error);
2321 for (i = 0; i < graph->n; ++i) {
2322 struct isl_sched_node *node = &graph->node[i];
2323 int pos = node->start;
2324 int row = isl_mat_rows(node->sched);
2326 isl_vec_free(csol);
2327 csol = isl_vec_alloc(sol->ctx, node->nvar);
2328 if (!csol)
2329 goto error;
2331 isl_map_free(node->sched_map);
2332 node->sched_map = NULL;
2333 node->sched = isl_mat_add_rows(node->sched, 1);
2334 if (!node->sched)
2335 goto error;
2336 node->sched = isl_mat_set_element(node->sched, row, 0,
2337 sol->el[1 + pos]);
2338 for (j = 0; j < node->nparam + node->nvar; ++j)
2339 isl_int_sub(sol->el[1 + pos + 1 + 2 * j + 1],
2340 sol->el[1 + pos + 1 + 2 * j + 1],
2341 sol->el[1 + pos + 1 + 2 * j]);
2342 for (j = 0; j < node->nparam; ++j)
2343 node->sched = isl_mat_set_element(node->sched,
2344 row, 1 + j, sol->el[1+pos+1+2*j+1]);
2345 for (j = 0; j < node->nvar; ++j)
2346 isl_int_set(csol->el[j],
2347 sol->el[1+pos+1+2*(node->nparam+j)+1]);
2348 if (use_cmap)
2349 csol = isl_mat_vec_product(isl_mat_copy(node->cmap),
2350 csol);
2351 if (!csol)
2352 goto error;
2353 for (j = 0; j < node->nvar; ++j)
2354 node->sched = isl_mat_set_element(node->sched,
2355 row, 1 + node->nparam + j, csol->el[j]);
2356 node->coincident[graph->n_total_row] = coincident;
2358 isl_vec_free(sol);
2359 isl_vec_free(csol);
2361 graph->n_row++;
2362 graph->n_total_row++;
2364 return 0;
2365 error:
2366 isl_vec_free(sol);
2367 isl_vec_free(csol);
2368 return -1;
2371 /* Convert row "row" of node->sched into an isl_aff living in "ls"
2372 * and return this isl_aff.
2374 static __isl_give isl_aff *extract_schedule_row(__isl_take isl_local_space *ls,
2375 struct isl_sched_node *node, int row)
2377 int j;
2378 isl_int v;
2379 isl_aff *aff;
2381 isl_int_init(v);
2383 aff = isl_aff_zero_on_domain(ls);
2384 isl_mat_get_element(node->sched, row, 0, &v);
2385 aff = isl_aff_set_constant(aff, v);
2386 for (j = 0; j < node->nparam; ++j) {
2387 isl_mat_get_element(node->sched, row, 1 + j, &v);
2388 aff = isl_aff_set_coefficient(aff, isl_dim_param, j, v);
2390 for (j = 0; j < node->nvar; ++j) {
2391 isl_mat_get_element(node->sched, row, 1 + node->nparam + j, &v);
2392 aff = isl_aff_set_coefficient(aff, isl_dim_in, j, v);
2395 isl_int_clear(v);
2397 return aff;
2400 /* Convert the "n" rows starting at "first" of node->sched into a multi_aff
2401 * and return this multi_aff.
2403 * The result is defined over the uncompressed node domain.
2405 static __isl_give isl_multi_aff *node_extract_partial_schedule_multi_aff(
2406 struct isl_sched_node *node, int first, int n)
2408 int i;
2409 isl_space *space;
2410 isl_local_space *ls;
2411 isl_aff *aff;
2412 isl_multi_aff *ma;
2413 int nrow;
2415 nrow = isl_mat_rows(node->sched);
2416 if (node->compressed)
2417 space = isl_multi_aff_get_domain_space(node->decompress);
2418 else
2419 space = isl_space_copy(node->space);
2420 ls = isl_local_space_from_space(isl_space_copy(space));
2421 space = isl_space_from_domain(space);
2422 space = isl_space_add_dims(space, isl_dim_out, n);
2423 ma = isl_multi_aff_zero(space);
2425 for (i = first; i < first + n; ++i) {
2426 aff = extract_schedule_row(isl_local_space_copy(ls), node, i);
2427 ma = isl_multi_aff_set_aff(ma, i - first, aff);
2430 isl_local_space_free(ls);
2432 if (node->compressed)
2433 ma = isl_multi_aff_pullback_multi_aff(ma,
2434 isl_multi_aff_copy(node->compress));
2436 return ma;
2439 /* Convert node->sched into a multi_aff and return this multi_aff.
2441 * The result is defined over the uncompressed node domain.
2443 static __isl_give isl_multi_aff *node_extract_schedule_multi_aff(
2444 struct isl_sched_node *node)
2446 int nrow;
2448 nrow = isl_mat_rows(node->sched);
2449 return node_extract_partial_schedule_multi_aff(node, 0, nrow);
2452 /* Convert node->sched into a map and return this map.
2454 * The result is cached in node->sched_map, which needs to be released
2455 * whenever node->sched is updated.
2456 * It is defined over the uncompressed node domain.
2458 static __isl_give isl_map *node_extract_schedule(struct isl_sched_node *node)
2460 if (!node->sched_map) {
2461 isl_multi_aff *ma;
2463 ma = node_extract_schedule_multi_aff(node);
2464 node->sched_map = isl_map_from_multi_aff(ma);
2467 return isl_map_copy(node->sched_map);
2470 /* Construct a map that can be used to update a dependence relation
2471 * based on the current schedule.
2472 * That is, construct a map expressing that source and sink
2473 * are executed within the same iteration of the current schedule.
2474 * This map can then be intersected with the dependence relation.
2475 * This is not the most efficient way, but this shouldn't be a critical
2476 * operation.
2478 static __isl_give isl_map *specializer(struct isl_sched_node *src,
2479 struct isl_sched_node *dst)
2481 isl_map *src_sched, *dst_sched;
2483 src_sched = node_extract_schedule(src);
2484 dst_sched = node_extract_schedule(dst);
2485 return isl_map_apply_range(src_sched, isl_map_reverse(dst_sched));
2488 /* Intersect the domains of the nested relations in domain and range
2489 * of "umap" with "map".
2491 static __isl_give isl_union_map *intersect_domains(
2492 __isl_take isl_union_map *umap, __isl_keep isl_map *map)
2494 isl_union_set *uset;
2496 umap = isl_union_map_zip(umap);
2497 uset = isl_union_set_from_set(isl_map_wrap(isl_map_copy(map)));
2498 umap = isl_union_map_intersect_domain(umap, uset);
2499 umap = isl_union_map_zip(umap);
2500 return umap;
2503 /* Update the dependence relation of the given edge based
2504 * on the current schedule.
2505 * If the dependence is carried completely by the current schedule, then
2506 * it is removed from the edge_tables. It is kept in the list of edges
2507 * as otherwise all edge_tables would have to be recomputed.
2509 static int update_edge(struct isl_sched_graph *graph,
2510 struct isl_sched_edge *edge)
2512 int empty;
2513 isl_map *id;
2515 id = specializer(edge->src, edge->dst);
2516 edge->map = isl_map_intersect(edge->map, isl_map_copy(id));
2517 if (!edge->map)
2518 goto error;
2520 if (edge->tagged_condition) {
2521 edge->tagged_condition =
2522 intersect_domains(edge->tagged_condition, id);
2523 if (!edge->tagged_condition)
2524 goto error;
2526 if (edge->tagged_validity) {
2527 edge->tagged_validity =
2528 intersect_domains(edge->tagged_validity, id);
2529 if (!edge->tagged_validity)
2530 goto error;
2533 empty = isl_map_plain_is_empty(edge->map);
2534 if (empty < 0)
2535 goto error;
2536 if (empty)
2537 graph_remove_edge(graph, edge);
2539 isl_map_free(id);
2540 return 0;
2541 error:
2542 isl_map_free(id);
2543 return -1;
2546 /* Does the domain of "umap" intersect "uset"?
2548 static int domain_intersects(__isl_keep isl_union_map *umap,
2549 __isl_keep isl_union_set *uset)
2551 int empty;
2553 umap = isl_union_map_copy(umap);
2554 umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(uset));
2555 empty = isl_union_map_is_empty(umap);
2556 isl_union_map_free(umap);
2558 return empty < 0 ? -1 : !empty;
2561 /* Does the range of "umap" intersect "uset"?
2563 static int range_intersects(__isl_keep isl_union_map *umap,
2564 __isl_keep isl_union_set *uset)
2566 int empty;
2568 umap = isl_union_map_copy(umap);
2569 umap = isl_union_map_intersect_range(umap, isl_union_set_copy(uset));
2570 empty = isl_union_map_is_empty(umap);
2571 isl_union_map_free(umap);
2573 return empty < 0 ? -1 : !empty;
2576 /* Are the condition dependences of "edge" local with respect to
2577 * the current schedule?
2579 * That is, are domain and range of the condition dependences mapped
2580 * to the same point?
2582 * In other words, is the condition false?
2584 static int is_condition_false(struct isl_sched_edge *edge)
2586 isl_union_map *umap;
2587 isl_map *map, *sched, *test;
2588 int empty, local;
2590 empty = isl_union_map_is_empty(edge->tagged_condition);
2591 if (empty < 0 || empty)
2592 return empty;
2594 umap = isl_union_map_copy(edge->tagged_condition);
2595 umap = isl_union_map_zip(umap);
2596 umap = isl_union_set_unwrap(isl_union_map_domain(umap));
2597 map = isl_map_from_union_map(umap);
2599 sched = node_extract_schedule(edge->src);
2600 map = isl_map_apply_domain(map, sched);
2601 sched = node_extract_schedule(edge->dst);
2602 map = isl_map_apply_range(map, sched);
2604 test = isl_map_identity(isl_map_get_space(map));
2605 local = isl_map_is_subset(map, test);
2606 isl_map_free(map);
2607 isl_map_free(test);
2609 return local;
2612 /* For each conditional validity constraint that is adjacent
2613 * to a condition with domain in condition_source or range in condition_sink,
2614 * turn it into an unconditional validity constraint.
2616 static int unconditionalize_adjacent_validity(struct isl_sched_graph *graph,
2617 __isl_take isl_union_set *condition_source,
2618 __isl_take isl_union_set *condition_sink)
2620 int i;
2622 condition_source = isl_union_set_coalesce(condition_source);
2623 condition_sink = isl_union_set_coalesce(condition_sink);
2625 for (i = 0; i < graph->n_edge; ++i) {
2626 int adjacent;
2627 isl_union_map *validity;
2629 if (!graph->edge[i].conditional_validity)
2630 continue;
2631 if (graph->edge[i].validity)
2632 continue;
2634 validity = graph->edge[i].tagged_validity;
2635 adjacent = domain_intersects(validity, condition_sink);
2636 if (adjacent >= 0 && !adjacent)
2637 adjacent = range_intersects(validity, condition_source);
2638 if (adjacent < 0)
2639 goto error;
2640 if (!adjacent)
2641 continue;
2643 graph->edge[i].validity = 1;
2646 isl_union_set_free(condition_source);
2647 isl_union_set_free(condition_sink);
2648 return 0;
2649 error:
2650 isl_union_set_free(condition_source);
2651 isl_union_set_free(condition_sink);
2652 return -1;
2655 /* Update the dependence relations of all edges based on the current schedule
2656 * and enforce conditional validity constraints that are adjacent
2657 * to satisfied condition constraints.
2659 * First check if any of the condition constraints are satisfied
2660 * (i.e., not local to the outer schedule) and keep track of
2661 * their domain and range.
2662 * Then update all dependence relations (which removes the non-local
2663 * constraints).
2664 * Finally, if any condition constraints turned out to be satisfied,
2665 * then turn all adjacent conditional validity constraints into
2666 * unconditional validity constraints.
2668 static int update_edges(isl_ctx *ctx, struct isl_sched_graph *graph)
2670 int i;
2671 int any = 0;
2672 isl_union_set *source, *sink;
2674 source = isl_union_set_empty(isl_space_params_alloc(ctx, 0));
2675 sink = isl_union_set_empty(isl_space_params_alloc(ctx, 0));
2676 for (i = 0; i < graph->n_edge; ++i) {
2677 int local;
2678 isl_union_set *uset;
2679 isl_union_map *umap;
2681 if (!graph->edge[i].condition)
2682 continue;
2683 if (graph->edge[i].local)
2684 continue;
2685 local = is_condition_false(&graph->edge[i]);
2686 if (local < 0)
2687 goto error;
2688 if (local)
2689 continue;
2691 any = 1;
2693 umap = isl_union_map_copy(graph->edge[i].tagged_condition);
2694 uset = isl_union_map_domain(umap);
2695 source = isl_union_set_union(source, uset);
2697 umap = isl_union_map_copy(graph->edge[i].tagged_condition);
2698 uset = isl_union_map_range(umap);
2699 sink = isl_union_set_union(sink, uset);
2702 for (i = graph->n_edge - 1; i >= 0; --i) {
2703 if (update_edge(graph, &graph->edge[i]) < 0)
2704 goto error;
2707 if (any)
2708 return unconditionalize_adjacent_validity(graph, source, sink);
2710 isl_union_set_free(source);
2711 isl_union_set_free(sink);
2712 return 0;
2713 error:
2714 isl_union_set_free(source);
2715 isl_union_set_free(sink);
2716 return -1;
2719 static void next_band(struct isl_sched_graph *graph)
2721 graph->band_start = graph->n_total_row;
2724 /* Return the union of the universe domains of the nodes in "graph"
2725 * that satisfy "pred".
2727 static __isl_give isl_union_set *isl_sched_graph_domain(isl_ctx *ctx,
2728 struct isl_sched_graph *graph,
2729 int (*pred)(struct isl_sched_node *node, int data), int data)
2731 int i;
2732 isl_set *set;
2733 isl_union_set *dom;
2735 for (i = 0; i < graph->n; ++i)
2736 if (pred(&graph->node[i], data))
2737 break;
2739 if (i >= graph->n)
2740 isl_die(ctx, isl_error_internal,
2741 "empty component", return NULL);
2743 set = isl_set_universe(isl_space_copy(graph->node[i].space));
2744 dom = isl_union_set_from_set(set);
2746 for (i = i + 1; i < graph->n; ++i) {
2747 if (!pred(&graph->node[i], data))
2748 continue;
2749 set = isl_set_universe(isl_space_copy(graph->node[i].space));
2750 dom = isl_union_set_union(dom, isl_union_set_from_set(set));
2753 return dom;
2756 /* Return a list of unions of universe domains, where each element
2757 * in the list corresponds to an SCC (or WCC) indexed by node->scc.
2759 static __isl_give isl_union_set_list *extract_sccs(isl_ctx *ctx,
2760 struct isl_sched_graph *graph)
2762 int i;
2763 isl_union_set_list *filters;
2765 filters = isl_union_set_list_alloc(ctx, graph->scc);
2766 for (i = 0; i < graph->scc; ++i) {
2767 isl_union_set *dom;
2769 dom = isl_sched_graph_domain(ctx, graph, &node_scc_exactly, i);
2770 filters = isl_union_set_list_add(filters, dom);
2773 return filters;
2776 /* Return a list of two unions of universe domains, one for the SCCs up
2777 * to and including graph->src_scc and another for the other SCCS.
2779 static __isl_give isl_union_set_list *extract_split(isl_ctx *ctx,
2780 struct isl_sched_graph *graph)
2782 isl_union_set *dom;
2783 isl_union_set_list *filters;
2785 filters = isl_union_set_list_alloc(ctx, 2);
2786 dom = isl_sched_graph_domain(ctx, graph,
2787 &node_scc_at_most, graph->src_scc);
2788 filters = isl_union_set_list_add(filters, dom);
2789 dom = isl_sched_graph_domain(ctx, graph,
2790 &node_scc_at_least, graph->src_scc + 1);
2791 filters = isl_union_set_list_add(filters, dom);
2793 return filters;
2796 /* Topologically sort statements mapped to the same schedule iteration
2797 * and add insert a sequence node in front of "node"
2798 * corresponding to this order.
2800 static __isl_give isl_schedule_node *sort_statements(
2801 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph)
2803 isl_ctx *ctx;
2804 isl_union_set_list *filters;
2806 if (!node)
2807 return NULL;
2809 ctx = isl_schedule_node_get_ctx(node);
2810 if (graph->n < 1)
2811 isl_die(ctx, isl_error_internal,
2812 "graph should have at least one node",
2813 return isl_schedule_node_free(node));
2815 if (graph->n == 1)
2816 return node;
2818 if (update_edges(ctx, graph) < 0)
2819 return isl_schedule_node_free(node);
2821 if (graph->n_edge == 0)
2822 return node;
2824 if (detect_sccs(ctx, graph) < 0)
2825 return isl_schedule_node_free(node);
2827 filters = extract_sccs(ctx, graph);
2828 node = isl_schedule_node_insert_sequence(node, filters);
2830 return node;
2833 /* Copy nodes that satisfy node_pred from the src dependence graph
2834 * to the dst dependence graph.
2836 static int copy_nodes(struct isl_sched_graph *dst, struct isl_sched_graph *src,
2837 int (*node_pred)(struct isl_sched_node *node, int data), int data)
2839 int i;
2841 dst->n = 0;
2842 for (i = 0; i < src->n; ++i) {
2843 int j;
2845 if (!node_pred(&src->node[i], data))
2846 continue;
2848 j = dst->n;
2849 dst->node[j].space = isl_space_copy(src->node[i].space);
2850 dst->node[j].compressed = src->node[i].compressed;
2851 dst->node[j].hull = isl_set_copy(src->node[i].hull);
2852 dst->node[j].compress =
2853 isl_multi_aff_copy(src->node[i].compress);
2854 dst->node[j].decompress =
2855 isl_multi_aff_copy(src->node[i].decompress);
2856 dst->node[j].nvar = src->node[i].nvar;
2857 dst->node[j].nparam = src->node[i].nparam;
2858 dst->node[j].sched = isl_mat_copy(src->node[i].sched);
2859 dst->node[j].sched_map = isl_map_copy(src->node[i].sched_map);
2860 dst->node[j].coincident = src->node[i].coincident;
2861 dst->n++;
2863 if (!dst->node[j].space || !dst->node[j].sched)
2864 return -1;
2865 if (dst->node[j].compressed &&
2866 (!dst->node[j].hull || !dst->node[j].compress ||
2867 !dst->node[j].decompress))
2868 return -1;
2871 return 0;
2874 /* Copy non-empty edges that satisfy edge_pred from the src dependence graph
2875 * to the dst dependence graph.
2876 * If the source or destination node of the edge is not in the destination
2877 * graph, then it must be a backward proximity edge and it should simply
2878 * be ignored.
2880 static int copy_edges(isl_ctx *ctx, struct isl_sched_graph *dst,
2881 struct isl_sched_graph *src,
2882 int (*edge_pred)(struct isl_sched_edge *edge, int data), int data)
2884 int i;
2885 enum isl_edge_type t;
2887 dst->n_edge = 0;
2888 for (i = 0; i < src->n_edge; ++i) {
2889 struct isl_sched_edge *edge = &src->edge[i];
2890 isl_map *map;
2891 isl_union_map *tagged_condition;
2892 isl_union_map *tagged_validity;
2893 struct isl_sched_node *dst_src, *dst_dst;
2895 if (!edge_pred(edge, data))
2896 continue;
2898 if (isl_map_plain_is_empty(edge->map))
2899 continue;
2901 dst_src = graph_find_node(ctx, dst, edge->src->space);
2902 dst_dst = graph_find_node(ctx, dst, edge->dst->space);
2903 if (!dst_src || !dst_dst) {
2904 if (edge->validity || edge->conditional_validity)
2905 isl_die(ctx, isl_error_internal,
2906 "backward (conditional) validity edge",
2907 return -1);
2908 continue;
2911 map = isl_map_copy(edge->map);
2912 tagged_condition = isl_union_map_copy(edge->tagged_condition);
2913 tagged_validity = isl_union_map_copy(edge->tagged_validity);
2915 dst->edge[dst->n_edge].src = dst_src;
2916 dst->edge[dst->n_edge].dst = dst_dst;
2917 dst->edge[dst->n_edge].map = map;
2918 dst->edge[dst->n_edge].tagged_condition = tagged_condition;
2919 dst->edge[dst->n_edge].tagged_validity = tagged_validity;
2920 dst->edge[dst->n_edge].validity = edge->validity;
2921 dst->edge[dst->n_edge].proximity = edge->proximity;
2922 dst->edge[dst->n_edge].coincidence = edge->coincidence;
2923 dst->edge[dst->n_edge].condition = edge->condition;
2924 dst->edge[dst->n_edge].conditional_validity =
2925 edge->conditional_validity;
2926 dst->n_edge++;
2928 if (edge->tagged_condition && !tagged_condition)
2929 return -1;
2930 if (edge->tagged_validity && !tagged_validity)
2931 return -1;
2933 for (t = isl_edge_first; t <= isl_edge_last; ++t) {
2934 if (edge !=
2935 graph_find_edge(src, t, edge->src, edge->dst))
2936 continue;
2937 if (graph_edge_table_add(ctx, dst, t,
2938 &dst->edge[dst->n_edge - 1]) < 0)
2939 return -1;
2943 return 0;
2946 /* Compute the maximal number of variables over all nodes.
2947 * This is the maximal number of linearly independent schedule
2948 * rows that we need to compute.
2949 * Just in case we end up in a part of the dependence graph
2950 * with only lower-dimensional domains, we make sure we will
2951 * compute the required amount of extra linearly independent rows.
2953 static int compute_maxvar(struct isl_sched_graph *graph)
2955 int i;
2957 graph->maxvar = 0;
2958 for (i = 0; i < graph->n; ++i) {
2959 struct isl_sched_node *node = &graph->node[i];
2960 int nvar;
2962 if (node_update_cmap(node) < 0)
2963 return -1;
2964 nvar = node->nvar + graph->n_row - node->rank;
2965 if (nvar > graph->maxvar)
2966 graph->maxvar = nvar;
2969 return 0;
2972 static __isl_give isl_schedule_node *compute_schedule(isl_schedule_node *node,
2973 struct isl_sched_graph *graph);
2974 static __isl_give isl_schedule_node *compute_schedule_wcc(
2975 isl_schedule_node *node, struct isl_sched_graph *graph);
2977 /* Compute a schedule for a subgraph of "graph". In particular, for
2978 * the graph composed of nodes that satisfy node_pred and edges that
2979 * that satisfy edge_pred. The caller should precompute the number
2980 * of nodes and edges that satisfy these predicates and pass them along
2981 * as "n" and "n_edge".
2982 * If the subgraph is known to consist of a single component, then wcc should
2983 * be set and then we call compute_schedule_wcc on the constructed subgraph.
2984 * Otherwise, we call compute_schedule, which will check whether the subgraph
2985 * is connected.
2987 * The schedule is inserted at "node" and the updated schedule node
2988 * is returned.
2990 static __isl_give isl_schedule_node *compute_sub_schedule(
2991 __isl_take isl_schedule_node *node, isl_ctx *ctx,
2992 struct isl_sched_graph *graph, int n, int n_edge,
2993 int (*node_pred)(struct isl_sched_node *node, int data),
2994 int (*edge_pred)(struct isl_sched_edge *edge, int data),
2995 int data, int wcc)
2997 struct isl_sched_graph split = { 0 };
2998 int t;
3000 if (graph_alloc(ctx, &split, n, n_edge) < 0)
3001 goto error;
3002 if (copy_nodes(&split, graph, node_pred, data) < 0)
3003 goto error;
3004 if (graph_init_table(ctx, &split) < 0)
3005 goto error;
3006 for (t = 0; t <= isl_edge_last; ++t)
3007 split.max_edge[t] = graph->max_edge[t];
3008 if (graph_init_edge_tables(ctx, &split) < 0)
3009 goto error;
3010 if (copy_edges(ctx, &split, graph, edge_pred, data) < 0)
3011 goto error;
3012 split.n_row = graph->n_row;
3013 split.max_row = graph->max_row;
3014 split.n_total_row = graph->n_total_row;
3015 split.band_start = graph->band_start;
3017 if (wcc)
3018 node = compute_schedule_wcc(node, &split);
3019 else
3020 node = compute_schedule(node, &split);
3022 graph_free(ctx, &split);
3023 return node;
3024 error:
3025 graph_free(ctx, &split);
3026 return isl_schedule_node_free(node);
3029 static int edge_scc_exactly(struct isl_sched_edge *edge, int scc)
3031 return edge->src->scc == scc && edge->dst->scc == scc;
3034 static int edge_dst_scc_at_most(struct isl_sched_edge *edge, int scc)
3036 return edge->dst->scc <= scc;
3039 static int edge_src_scc_at_least(struct isl_sched_edge *edge, int scc)
3041 return edge->src->scc >= scc;
3044 /* Reset the current band by dropping all its schedule rows.
3046 static int reset_band(struct isl_sched_graph *graph)
3048 int i;
3049 int drop;
3051 drop = graph->n_total_row - graph->band_start;
3052 graph->n_total_row -= drop;
3053 graph->n_row -= drop;
3055 for (i = 0; i < graph->n; ++i) {
3056 struct isl_sched_node *node = &graph->node[i];
3058 isl_map_free(node->sched_map);
3059 node->sched_map = NULL;
3061 node->sched = isl_mat_drop_rows(node->sched,
3062 graph->band_start, drop);
3064 if (!node->sched)
3065 return -1;
3068 return 0;
3071 /* Split the current graph into two parts and compute a schedule for each
3072 * part individually. In particular, one part consists of all SCCs up
3073 * to and including graph->src_scc, while the other part contains the other
3074 * SCCS. The split is enforced by a sequence node inserted at position "node"
3075 * in the schedule tree. Return the updated schedule node.
3077 * The current band is reset. It would be possible to reuse
3078 * the previously computed rows as the first rows in the next
3079 * band, but recomputing them may result in better rows as we are looking
3080 * at a smaller part of the dependence graph.
3082 static __isl_give isl_schedule_node *compute_split_schedule(
3083 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph)
3085 int i, n, e1, e2;
3086 int orig_total_row;
3087 isl_ctx *ctx;
3088 isl_union_set_list *filters;
3090 if (!node)
3091 return NULL;
3093 if (reset_band(graph) < 0)
3094 return isl_schedule_node_free(node);
3096 n = 0;
3097 for (i = 0; i < graph->n; ++i) {
3098 struct isl_sched_node *node = &graph->node[i];
3099 int before = node->scc <= graph->src_scc;
3101 if (before)
3102 n++;
3105 e1 = e2 = 0;
3106 for (i = 0; i < graph->n_edge; ++i) {
3107 if (graph->edge[i].dst->scc <= graph->src_scc)
3108 e1++;
3109 if (graph->edge[i].src->scc > graph->src_scc)
3110 e2++;
3113 next_band(graph);
3115 ctx = isl_schedule_node_get_ctx(node);
3116 filters = extract_split(ctx, graph);
3117 node = isl_schedule_node_insert_sequence(node, filters);
3118 node = isl_schedule_node_child(node, 0);
3119 node = isl_schedule_node_child(node, 0);
3121 orig_total_row = graph->n_total_row;
3122 node = compute_sub_schedule(node, ctx, graph, n, e1,
3123 &node_scc_at_most, &edge_dst_scc_at_most,
3124 graph->src_scc, 0);
3125 node = isl_schedule_node_parent(node);
3126 node = isl_schedule_node_next_sibling(node);
3127 node = isl_schedule_node_child(node, 0);
3128 graph->n_total_row = orig_total_row;
3129 node = compute_sub_schedule(node, ctx, graph, graph->n - n, e2,
3130 &node_scc_at_least, &edge_src_scc_at_least,
3131 graph->src_scc + 1, 0);
3132 node = isl_schedule_node_parent(node);
3133 node = isl_schedule_node_parent(node);
3135 return node;
3138 /* Insert a band node at position "node" in the schedule tree corresponding
3139 * to the current band in "graph". Mark the band node permutable
3140 * if "permutable" is set.
3141 * The partial schedules and the coincidence property are extracted
3142 * from the graph nodes.
3143 * Return the updated schedule node.
3145 static __isl_give isl_schedule_node *insert_current_band(
3146 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph,
3147 int permutable)
3149 int i;
3150 int start, end, n;
3151 isl_multi_aff *ma;
3152 isl_multi_pw_aff *mpa;
3153 isl_multi_union_pw_aff *mupa;
3155 if (!node)
3156 return NULL;
3158 if (graph->n < 1)
3159 isl_die(isl_schedule_node_get_ctx(node), isl_error_internal,
3160 "graph should have at least one node",
3161 return isl_schedule_node_free(node));
3163 start = graph->band_start;
3164 end = graph->n_total_row;
3165 n = end - start;
3167 ma = node_extract_partial_schedule_multi_aff(&graph->node[0], start, n);
3168 mpa = isl_multi_pw_aff_from_multi_aff(ma);
3169 mupa = isl_multi_union_pw_aff_from_multi_pw_aff(mpa);
3171 for (i = 1; i < graph->n; ++i) {
3172 isl_multi_union_pw_aff *mupa_i;
3174 ma = node_extract_partial_schedule_multi_aff(&graph->node[i],
3175 start, n);
3176 mpa = isl_multi_pw_aff_from_multi_aff(ma);
3177 mupa_i = isl_multi_union_pw_aff_from_multi_pw_aff(mpa);
3178 mupa = isl_multi_union_pw_aff_union_add(mupa, mupa_i);
3180 node = isl_schedule_node_insert_partial_schedule(node, mupa);
3182 for (i = 0; i < n; ++i)
3183 node = isl_schedule_node_band_member_set_coincident(node, i,
3184 graph->node[0].coincident[start + i]);
3185 node = isl_schedule_node_band_set_permutable(node, permutable);
3187 return node;
3190 /* Update the dependence relations based on the current schedule,
3191 * add the current band to "node" and the continue with the computation
3192 * of the next band.
3193 * Return the updated schedule node.
3195 static __isl_give isl_schedule_node *compute_next_band(
3196 __isl_take isl_schedule_node *node,
3197 struct isl_sched_graph *graph, int permutable)
3199 isl_ctx *ctx;
3201 if (!node)
3202 return NULL;
3204 ctx = isl_schedule_node_get_ctx(node);
3205 if (update_edges(ctx, graph) < 0)
3206 return isl_schedule_node_free(node);
3207 node = insert_current_band(node, graph, permutable);
3208 next_band(graph);
3210 node = isl_schedule_node_child(node, 0);
3211 node = compute_schedule(node, graph);
3212 node = isl_schedule_node_parent(node);
3214 return node;
3217 /* Add constraints to graph->lp that force the dependence "map" (which
3218 * is part of the dependence relation of "edge")
3219 * to be respected and attempt to carry it, where the edge is one from
3220 * a node j to itself. "pos" is the sequence number of the given map.
3221 * That is, add constraints that enforce
3223 * (c_j_0 + c_j_n n + c_j_x y) - (c_j_0 + c_j_n n + c_j_x x)
3224 * = c_j_x (y - x) >= e_i
3226 * for each (x,y) in R.
3227 * We obtain general constraints on coefficients (c_0, c_n, c_x)
3228 * of valid constraints for (y - x) and then plug in (-e_i, 0, c_j_x),
3229 * with each coefficient in c_j_x represented as a pair of non-negative
3230 * coefficients.
3232 static int add_intra_constraints(struct isl_sched_graph *graph,
3233 struct isl_sched_edge *edge, __isl_take isl_map *map, int pos)
3235 unsigned total;
3236 isl_ctx *ctx = isl_map_get_ctx(map);
3237 isl_space *dim;
3238 isl_dim_map *dim_map;
3239 isl_basic_set *coef;
3240 struct isl_sched_node *node = edge->src;
3242 coef = intra_coefficients(graph, node, map);
3243 if (!coef)
3244 return -1;
3246 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
3248 total = isl_basic_set_total_dim(graph->lp);
3249 dim_map = isl_dim_map_alloc(ctx, total);
3250 isl_dim_map_range(dim_map, 3 + pos, 0, 0, 0, 1, -1);
3251 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 1, 2,
3252 isl_space_dim(dim, isl_dim_set), 1,
3253 node->nvar, -1);
3254 isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 2, 2,
3255 isl_space_dim(dim, isl_dim_set), 1,
3256 node->nvar, 1);
3257 graph->lp = isl_basic_set_extend_constraints(graph->lp,
3258 coef->n_eq, coef->n_ineq);
3259 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
3260 coef, dim_map);
3261 isl_space_free(dim);
3263 return 0;
3266 /* Add constraints to graph->lp that force the dependence "map" (which
3267 * is part of the dependence relation of "edge")
3268 * to be respected and attempt to carry it, where the edge is one from
3269 * node j to node k. "pos" is the sequence number of the given map.
3270 * That is, add constraints that enforce
3272 * (c_k_0 + c_k_n n + c_k_x y) - (c_j_0 + c_j_n n + c_j_x x) >= e_i
3274 * for each (x,y) in R.
3275 * We obtain general constraints on coefficients (c_0, c_n, c_x)
3276 * of valid constraints for R and then plug in
3277 * (-e_i + c_k_0 - c_j_0, c_k_n - c_j_n, c_k_x - c_j_x)
3278 * with each coefficient (except e_i, c_k_0 and c_j_0)
3279 * represented as a pair of non-negative coefficients.
3281 static int add_inter_constraints(struct isl_sched_graph *graph,
3282 struct isl_sched_edge *edge, __isl_take isl_map *map, int pos)
3284 unsigned total;
3285 isl_ctx *ctx = isl_map_get_ctx(map);
3286 isl_space *dim;
3287 isl_dim_map *dim_map;
3288 isl_basic_set *coef;
3289 struct isl_sched_node *src = edge->src;
3290 struct isl_sched_node *dst = edge->dst;
3292 coef = inter_coefficients(graph, edge, map);
3293 if (!coef)
3294 return -1;
3296 dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
3298 total = isl_basic_set_total_dim(graph->lp);
3299 dim_map = isl_dim_map_alloc(ctx, total);
3301 isl_dim_map_range(dim_map, 3 + pos, 0, 0, 0, 1, -1);
3303 isl_dim_map_range(dim_map, dst->start, 0, 0, 0, 1, 1);
3304 isl_dim_map_range(dim_map, dst->start + 1, 2, 1, 1, dst->nparam, -1);
3305 isl_dim_map_range(dim_map, dst->start + 2, 2, 1, 1, dst->nparam, 1);
3306 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 1, 2,
3307 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
3308 dst->nvar, -1);
3309 isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 2, 2,
3310 isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
3311 dst->nvar, 1);
3313 isl_dim_map_range(dim_map, src->start, 0, 0, 0, 1, -1);
3314 isl_dim_map_range(dim_map, src->start + 1, 2, 1, 1, src->nparam, 1);
3315 isl_dim_map_range(dim_map, src->start + 2, 2, 1, 1, src->nparam, -1);
3316 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 1, 2,
3317 isl_space_dim(dim, isl_dim_set), 1,
3318 src->nvar, 1);
3319 isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 2, 2,
3320 isl_space_dim(dim, isl_dim_set), 1,
3321 src->nvar, -1);
3323 graph->lp = isl_basic_set_extend_constraints(graph->lp,
3324 coef->n_eq, coef->n_ineq);
3325 graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
3326 coef, dim_map);
3327 isl_space_free(dim);
3329 return 0;
3332 /* Add constraints to graph->lp that force all (conditional) validity
3333 * dependences to be respected and attempt to carry them.
3335 static int add_all_constraints(struct isl_sched_graph *graph)
3337 int i, j;
3338 int pos;
3340 pos = 0;
3341 for (i = 0; i < graph->n_edge; ++i) {
3342 struct isl_sched_edge *edge= &graph->edge[i];
3344 if (!edge->validity && !edge->conditional_validity)
3345 continue;
3347 for (j = 0; j < edge->map->n; ++j) {
3348 isl_basic_map *bmap;
3349 isl_map *map;
3351 bmap = isl_basic_map_copy(edge->map->p[j]);
3352 map = isl_map_from_basic_map(bmap);
3354 if (edge->src == edge->dst &&
3355 add_intra_constraints(graph, edge, map, pos) < 0)
3356 return -1;
3357 if (edge->src != edge->dst &&
3358 add_inter_constraints(graph, edge, map, pos) < 0)
3359 return -1;
3360 ++pos;
3364 return 0;
3367 /* Count the number of equality and inequality constraints
3368 * that will be added to the carry_lp problem.
3369 * We count each edge exactly once.
3371 static int count_all_constraints(struct isl_sched_graph *graph,
3372 int *n_eq, int *n_ineq)
3374 int i, j;
3376 *n_eq = *n_ineq = 0;
3377 for (i = 0; i < graph->n_edge; ++i) {
3378 struct isl_sched_edge *edge= &graph->edge[i];
3379 for (j = 0; j < edge->map->n; ++j) {
3380 isl_basic_map *bmap;
3381 isl_map *map;
3383 bmap = isl_basic_map_copy(edge->map->p[j]);
3384 map = isl_map_from_basic_map(bmap);
3386 if (count_map_constraints(graph, edge, map,
3387 n_eq, n_ineq, 1, 0) < 0)
3388 return -1;
3392 return 0;
3395 /* Construct an LP problem for finding schedule coefficients
3396 * such that the schedule carries as many dependences as possible.
3397 * In particular, for each dependence i, we bound the dependence distance
3398 * from below by e_i, with 0 <= e_i <= 1 and then maximize the sum
3399 * of all e_i's. Dependence with e_i = 0 in the solution are simply
3400 * respected, while those with e_i > 0 (in practice e_i = 1) are carried.
3401 * Note that if the dependence relation is a union of basic maps,
3402 * then we have to consider each basic map individually as it may only
3403 * be possible to carry the dependences expressed by some of those
3404 * basic maps and not all off them.
3405 * Below, we consider each of those basic maps as a separate "edge".
3407 * All variables of the LP are non-negative. The actual coefficients
3408 * may be negative, so each coefficient is represented as the difference
3409 * of two non-negative variables. The negative part always appears
3410 * immediately before the positive part.
3411 * Other than that, the variables have the following order
3413 * - sum of (1 - e_i) over all edges
3414 * - sum of positive and negative parts of all c_n coefficients
3415 * (unconstrained when computing non-parametric schedules)
3416 * - sum of positive and negative parts of all c_x coefficients
3417 * - for each edge
3418 * - e_i
3419 * - for each node
3420 * - c_i_0
3421 * - positive and negative parts of c_i_n (if parametric)
3422 * - positive and negative parts of c_i_x
3424 * The constraints are those from the (validity) edges plus three equalities
3425 * to express the sums and n_edge inequalities to express e_i <= 1.
3427 static int setup_carry_lp(isl_ctx *ctx, struct isl_sched_graph *graph)
3429 int i, j;
3430 int k;
3431 isl_space *dim;
3432 unsigned total;
3433 int n_eq, n_ineq;
3434 int n_edge;
3436 n_edge = 0;
3437 for (i = 0; i < graph->n_edge; ++i)
3438 n_edge += graph->edge[i].map->n;
3440 total = 3 + n_edge;
3441 for (i = 0; i < graph->n; ++i) {
3442 struct isl_sched_node *node = &graph->node[graph->sorted[i]];
3443 node->start = total;
3444 total += 1 + 2 * (node->nparam + node->nvar);
3447 if (count_all_constraints(graph, &n_eq, &n_ineq) < 0)
3448 return -1;
3449 if (count_bound_coefficient_constraints(ctx, graph, &n_eq, &n_ineq) < 0)
3450 return -1;
3452 dim = isl_space_set_alloc(ctx, 0, total);
3453 isl_basic_set_free(graph->lp);
3454 n_eq += 3;
3455 n_ineq += n_edge;
3456 graph->lp = isl_basic_set_alloc_space(dim, 0, n_eq, n_ineq);
3457 graph->lp = isl_basic_set_set_rational(graph->lp);
3459 k = isl_basic_set_alloc_equality(graph->lp);
3460 if (k < 0)
3461 return -1;
3462 isl_seq_clr(graph->lp->eq[k], 1 + total);
3463 isl_int_set_si(graph->lp->eq[k][0], -n_edge);
3464 isl_int_set_si(graph->lp->eq[k][1], 1);
3465 for (i = 0; i < n_edge; ++i)
3466 isl_int_set_si(graph->lp->eq[k][4 + i], 1);
3468 k = isl_basic_set_alloc_equality(graph->lp);
3469 if (k < 0)
3470 return -1;
3471 isl_seq_clr(graph->lp->eq[k], 1 + total);
3472 isl_int_set_si(graph->lp->eq[k][2], -1);
3473 for (i = 0; i < graph->n; ++i) {
3474 int pos = 1 + graph->node[i].start + 1;
3476 for (j = 0; j < 2 * graph->node[i].nparam; ++j)
3477 isl_int_set_si(graph->lp->eq[k][pos + j], 1);
3480 k = isl_basic_set_alloc_equality(graph->lp);
3481 if (k < 0)
3482 return -1;
3483 isl_seq_clr(graph->lp->eq[k], 1 + total);
3484 isl_int_set_si(graph->lp->eq[k][3], -1);
3485 for (i = 0; i < graph->n; ++i) {
3486 struct isl_sched_node *node = &graph->node[i];
3487 int pos = 1 + node->start + 1 + 2 * node->nparam;
3489 for (j = 0; j < 2 * node->nvar; ++j)
3490 isl_int_set_si(graph->lp->eq[k][pos + j], 1);
3493 for (i = 0; i < n_edge; ++i) {
3494 k = isl_basic_set_alloc_inequality(graph->lp);
3495 if (k < 0)
3496 return -1;
3497 isl_seq_clr(graph->lp->ineq[k], 1 + total);
3498 isl_int_set_si(graph->lp->ineq[k][4 + i], -1);
3499 isl_int_set_si(graph->lp->ineq[k][0], 1);
3502 if (add_bound_coefficient_constraints(ctx, graph) < 0)
3503 return -1;
3504 if (add_all_constraints(graph) < 0)
3505 return -1;
3507 return 0;
3510 static __isl_give isl_schedule_node *compute_component_schedule(
3511 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph,
3512 int wcc);
3514 /* Comparison function for sorting the statements based on
3515 * the corresponding value in "r".
3517 static int smaller_value(const void *a, const void *b, void *data)
3519 isl_vec *r = data;
3520 const int *i1 = a;
3521 const int *i2 = b;
3523 return isl_int_cmp(r->el[*i1], r->el[*i2]);
3526 /* If the schedule_split_scaled option is set and if the linear
3527 * parts of the scheduling rows for all nodes in the graphs have
3528 * a non-trivial common divisor, then split off the remainder of the
3529 * constant term modulo this common divisor from the linear part.
3530 * Otherwise, insert a band node directly and continue with
3531 * the construction of the schedule.
3533 * If a non-trivial common divisor is found, then
3534 * the linear part is reduced and the remainder is enforced
3535 * by a sequence node with the children placed in the order
3536 * of this remainder.
3537 * In particular, we assign an scc index based on the remainder and
3538 * then rely on compute_component_schedule to insert the sequence and
3539 * to continue the schedule construction on each part.
3541 static __isl_give isl_schedule_node *split_scaled(
3542 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph)
3544 int i;
3545 int row;
3546 int scc;
3547 isl_ctx *ctx;
3548 isl_int gcd, gcd_i;
3549 isl_vec *r;
3550 int *order;
3552 if (!node)
3553 return NULL;
3555 ctx = isl_schedule_node_get_ctx(node);
3556 if (!ctx->opt->schedule_split_scaled)
3557 return compute_next_band(node, graph, 0);
3558 if (graph->n <= 1)
3559 return compute_next_band(node, graph, 0);
3561 isl_int_init(gcd);
3562 isl_int_init(gcd_i);
3564 isl_int_set_si(gcd, 0);
3566 row = isl_mat_rows(graph->node[0].sched) - 1;
3568 for (i = 0; i < graph->n; ++i) {
3569 struct isl_sched_node *node = &graph->node[i];
3570 int cols = isl_mat_cols(node->sched);
3572 isl_seq_gcd(node->sched->row[row] + 1, cols - 1, &gcd_i);
3573 isl_int_gcd(gcd, gcd, gcd_i);
3576 isl_int_clear(gcd_i);
3578 if (isl_int_cmp_si(gcd, 1) <= 0) {
3579 isl_int_clear(gcd);
3580 return compute_next_band(node, graph, 0);
3583 r = isl_vec_alloc(ctx, graph->n);
3584 order = isl_calloc_array(ctx, int, graph->n);
3585 if (!r || !order)
3586 goto error;
3588 for (i = 0; i < graph->n; ++i) {
3589 struct isl_sched_node *node = &graph->node[i];
3591 order[i] = i;
3592 isl_int_fdiv_r(r->el[i], node->sched->row[row][0], gcd);
3593 isl_int_fdiv_q(node->sched->row[row][0],
3594 node->sched->row[row][0], gcd);
3595 isl_int_mul(node->sched->row[row][0],
3596 node->sched->row[row][0], gcd);
3597 node->sched = isl_mat_scale_down_row(node->sched, row, gcd);
3598 if (!node->sched)
3599 goto error;
3602 if (isl_sort(order, graph->n, sizeof(order[0]), &smaller_value, r) < 0)
3603 goto error;
3605 scc = 0;
3606 for (i = 0; i < graph->n; ++i) {
3607 if (i > 0 && isl_int_ne(r->el[order[i - 1]], r->el[order[i]]))
3608 ++scc;
3609 graph->node[order[i]].scc = scc;
3611 graph->scc = ++scc;
3612 graph->weak = 0;
3614 isl_int_clear(gcd);
3615 isl_vec_free(r);
3616 free(order);
3618 if (update_edges(ctx, graph) < 0)
3619 return isl_schedule_node_free(node);
3620 node = insert_current_band(node, graph, 0);
3621 next_band(graph);
3623 node = isl_schedule_node_child(node, 0);
3624 node = compute_component_schedule(node, graph, 0);
3625 node = isl_schedule_node_parent(node);
3627 return node;
3628 error:
3629 isl_vec_free(r);
3630 free(order);
3631 isl_int_clear(gcd);
3632 return isl_schedule_node_free(node);
3635 /* Is the schedule row "sol" trivial on node "node"?
3636 * That is, is the solution zero on the dimensions orthogonal to
3637 * the previously found solutions?
3638 * Return 1 if the solution is trivial, 0 if it is not and -1 on error.
3640 * Each coefficient is represented as the difference between
3641 * two non-negative values in "sol". "sol" has been computed
3642 * in terms of the original iterators (i.e., without use of cmap).
3643 * We construct the schedule row s and write it as a linear
3644 * combination of (linear combinations of) previously computed schedule rows.
3645 * s = Q c or c = U s.
3646 * If the final entries of c are all zero, then the solution is trivial.
3648 static int is_trivial(struct isl_sched_node *node, __isl_keep isl_vec *sol)
3650 int i;
3651 int pos;
3652 int trivial;
3653 isl_ctx *ctx;
3654 isl_vec *node_sol;
3656 if (!sol)
3657 return -1;
3658 if (node->nvar == node->rank)
3659 return 0;
3661 ctx = isl_vec_get_ctx(sol);
3662 node_sol = isl_vec_alloc(ctx, node->nvar);
3663 if (!node_sol)
3664 return -1;
3666 pos = 1 + node->start + 1 + 2 * node->nparam;
3668 for (i = 0; i < node->nvar; ++i)
3669 isl_int_sub(node_sol->el[i],
3670 sol->el[pos + 2 * i + 1], sol->el[pos + 2 * i]);
3672 node_sol = isl_mat_vec_product(isl_mat_copy(node->cinv), node_sol);
3674 if (!node_sol)
3675 return -1;
3677 trivial = isl_seq_first_non_zero(node_sol->el + node->rank,
3678 node->nvar - node->rank) == -1;
3680 isl_vec_free(node_sol);
3682 return trivial;
3685 /* Is the schedule row "sol" trivial on any node where it should
3686 * not be trivial?
3687 * "sol" has been computed in terms of the original iterators
3688 * (i.e., without use of cmap).
3689 * Return 1 if any solution is trivial, 0 if they are not and -1 on error.
3691 static int is_any_trivial(struct isl_sched_graph *graph,
3692 __isl_keep isl_vec *sol)
3694 int i;
3696 for (i = 0; i < graph->n; ++i) {
3697 struct isl_sched_node *node = &graph->node[i];
3698 int trivial;
3700 if (!needs_row(graph, node))
3701 continue;
3702 trivial = is_trivial(node, sol);
3703 if (trivial < 0 || trivial)
3704 return trivial;
3707 return 0;
3710 /* Construct a schedule row for each node such that as many dependences
3711 * as possible are carried and then continue with the next band.
3713 * If the computed schedule row turns out to be trivial on one or
3714 * more nodes where it should not be trivial, then we throw it away
3715 * and try again on each component separately.
3717 * If there is only one component, then we accept the schedule row anyway,
3718 * but we do not consider it as a complete row and therefore do not
3719 * increment graph->n_row. Note that the ranks of the nodes that
3720 * do get a non-trivial schedule part will get updated regardless and
3721 * graph->maxvar is computed based on these ranks. The test for
3722 * whether more schedule rows are required in compute_schedule_wcc
3723 * is therefore not affected.
3725 * Insert a band corresponding to the schedule row at position "node"
3726 * of the schedule tree and continue with the construction of the schedule.
3727 * This insertion and the continued construction is performed by split_scaled
3728 * after optionally checking for non-trivial common divisors.
3730 static __isl_give isl_schedule_node *carry_dependences(
3731 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph)
3733 int i;
3734 int n_edge;
3735 int trivial;
3736 isl_ctx *ctx;
3737 isl_vec *sol;
3738 isl_basic_set *lp;
3740 if (!node)
3741 return NULL;
3743 n_edge = 0;
3744 for (i = 0; i < graph->n_edge; ++i)
3745 n_edge += graph->edge[i].map->n;
3747 ctx = isl_schedule_node_get_ctx(node);
3748 if (setup_carry_lp(ctx, graph) < 0)
3749 return isl_schedule_node_free(node);
3751 lp = isl_basic_set_copy(graph->lp);
3752 sol = isl_tab_basic_set_non_neg_lexmin(lp);
3753 if (!sol)
3754 return isl_schedule_node_free(node);
3756 if (sol->size == 0) {
3757 isl_vec_free(sol);
3758 isl_die(ctx, isl_error_internal,
3759 "error in schedule construction",
3760 return isl_schedule_node_free(node));
3763 isl_int_divexact(sol->el[1], sol->el[1], sol->el[0]);
3764 if (isl_int_cmp_si(sol->el[1], n_edge) >= 0) {
3765 isl_vec_free(sol);
3766 isl_die(ctx, isl_error_unknown,
3767 "unable to carry dependences",
3768 return isl_schedule_node_free(node));
3771 trivial = is_any_trivial(graph, sol);
3772 if (trivial < 0) {
3773 sol = isl_vec_free(sol);
3774 } else if (trivial && graph->scc > 1) {
3775 isl_vec_free(sol);
3776 return compute_component_schedule(node, graph, 1);
3779 if (update_schedule(graph, sol, 0, 0) < 0)
3780 return isl_schedule_node_free(node);
3781 if (trivial)
3782 graph->n_row--;
3784 return split_scaled(node, graph);
3787 /* Are there any (non-empty) (conditional) validity edges in the graph?
3789 static int has_validity_edges(struct isl_sched_graph *graph)
3791 int i;
3793 for (i = 0; i < graph->n_edge; ++i) {
3794 int empty;
3796 empty = isl_map_plain_is_empty(graph->edge[i].map);
3797 if (empty < 0)
3798 return -1;
3799 if (empty)
3800 continue;
3801 if (graph->edge[i].validity ||
3802 graph->edge[i].conditional_validity)
3803 return 1;
3806 return 0;
3809 /* Should we apply a Feautrier step?
3810 * That is, did the user request the Feautrier algorithm and are
3811 * there any validity dependences (left)?
3813 static int need_feautrier_step(isl_ctx *ctx, struct isl_sched_graph *graph)
3815 if (ctx->opt->schedule_algorithm != ISL_SCHEDULE_ALGORITHM_FEAUTRIER)
3816 return 0;
3818 return has_validity_edges(graph);
3821 /* Compute a schedule for a connected dependence graph using Feautrier's
3822 * multi-dimensional scheduling algorithm and return the updated schedule node.
3824 * The original algorithm is described in [1].
3825 * The main idea is to minimize the number of scheduling dimensions, by
3826 * trying to satisfy as many dependences as possible per scheduling dimension.
3828 * [1] P. Feautrier, Some Efficient Solutions to the Affine Scheduling
3829 * Problem, Part II: Multi-Dimensional Time.
3830 * In Intl. Journal of Parallel Programming, 1992.
3832 static __isl_give isl_schedule_node *compute_schedule_wcc_feautrier(
3833 isl_schedule_node *node, struct isl_sched_graph *graph)
3835 return carry_dependences(node, graph);
3838 /* Turn off the "local" bit on all (condition) edges.
3840 static void clear_local_edges(struct isl_sched_graph *graph)
3842 int i;
3844 for (i = 0; i < graph->n_edge; ++i)
3845 if (graph->edge[i].condition)
3846 graph->edge[i].local = 0;
3849 /* Does "graph" have both condition and conditional validity edges?
3851 static int need_condition_check(struct isl_sched_graph *graph)
3853 int i;
3854 int any_condition = 0;
3855 int any_conditional_validity = 0;
3857 for (i = 0; i < graph->n_edge; ++i) {
3858 if (graph->edge[i].condition)
3859 any_condition = 1;
3860 if (graph->edge[i].conditional_validity)
3861 any_conditional_validity = 1;
3864 return any_condition && any_conditional_validity;
3867 /* Does "graph" contain any coincidence edge?
3869 static int has_any_coincidence(struct isl_sched_graph *graph)
3871 int i;
3873 for (i = 0; i < graph->n_edge; ++i)
3874 if (graph->edge[i].coincidence)
3875 return 1;
3877 return 0;
3880 /* Extract the final schedule row as a map with the iteration domain
3881 * of "node" as domain.
3883 static __isl_give isl_map *final_row(struct isl_sched_node *node)
3885 isl_local_space *ls;
3886 isl_aff *aff;
3887 int row;
3889 row = isl_mat_rows(node->sched) - 1;
3890 ls = isl_local_space_from_space(isl_space_copy(node->space));
3891 aff = extract_schedule_row(ls, node, row);
3892 return isl_map_from_aff(aff);
3895 /* Is the conditional validity dependence in the edge with index "edge_index"
3896 * violated by the latest (i.e., final) row of the schedule?
3897 * That is, is i scheduled after j
3898 * for any conditional validity dependence i -> j?
3900 static int is_violated(struct isl_sched_graph *graph, int edge_index)
3902 isl_map *src_sched, *dst_sched, *map;
3903 struct isl_sched_edge *edge = &graph->edge[edge_index];
3904 int empty;
3906 src_sched = final_row(edge->src);
3907 dst_sched = final_row(edge->dst);
3908 map = isl_map_copy(edge->map);
3909 map = isl_map_apply_domain(map, src_sched);
3910 map = isl_map_apply_range(map, dst_sched);
3911 map = isl_map_order_gt(map, isl_dim_in, 0, isl_dim_out, 0);
3912 empty = isl_map_is_empty(map);
3913 isl_map_free(map);
3915 if (empty < 0)
3916 return -1;
3918 return !empty;
3921 /* Does "graph" have any satisfied condition edges that
3922 * are adjacent to the conditional validity constraint with
3923 * domain "conditional_source" and range "conditional_sink"?
3925 * A satisfied condition is one that is not local.
3926 * If a condition was forced to be local already (i.e., marked as local)
3927 * then there is no need to check if it is in fact local.
3929 * Additionally, mark all adjacent condition edges found as local.
3931 static int has_adjacent_true_conditions(struct isl_sched_graph *graph,
3932 __isl_keep isl_union_set *conditional_source,
3933 __isl_keep isl_union_set *conditional_sink)
3935 int i;
3936 int any = 0;
3938 for (i = 0; i < graph->n_edge; ++i) {
3939 int adjacent, local;
3940 isl_union_map *condition;
3942 if (!graph->edge[i].condition)
3943 continue;
3944 if (graph->edge[i].local)
3945 continue;
3947 condition = graph->edge[i].tagged_condition;
3948 adjacent = domain_intersects(condition, conditional_sink);
3949 if (adjacent >= 0 && !adjacent)
3950 adjacent = range_intersects(condition,
3951 conditional_source);
3952 if (adjacent < 0)
3953 return -1;
3954 if (!adjacent)
3955 continue;
3957 graph->edge[i].local = 1;
3959 local = is_condition_false(&graph->edge[i]);
3960 if (local < 0)
3961 return -1;
3962 if (!local)
3963 any = 1;
3966 return any;
3969 /* Are there any violated conditional validity dependences with
3970 * adjacent condition dependences that are not local with respect
3971 * to the current schedule?
3972 * That is, is the conditional validity constraint violated?
3974 * Additionally, mark all those adjacent condition dependences as local.
3975 * We also mark those adjacent condition dependences that were not marked
3976 * as local before, but just happened to be local already. This ensures
3977 * that they remain local if the schedule is recomputed.
3979 * We first collect domain and range of all violated conditional validity
3980 * dependences and then check if there are any adjacent non-local
3981 * condition dependences.
3983 static int has_violated_conditional_constraint(isl_ctx *ctx,
3984 struct isl_sched_graph *graph)
3986 int i;
3987 int any = 0;
3988 isl_union_set *source, *sink;
3990 source = isl_union_set_empty(isl_space_params_alloc(ctx, 0));
3991 sink = isl_union_set_empty(isl_space_params_alloc(ctx, 0));
3992 for (i = 0; i < graph->n_edge; ++i) {
3993 isl_union_set *uset;
3994 isl_union_map *umap;
3995 int violated;
3997 if (!graph->edge[i].conditional_validity)
3998 continue;
4000 violated = is_violated(graph, i);
4001 if (violated < 0)
4002 goto error;
4003 if (!violated)
4004 continue;
4006 any = 1;
4008 umap = isl_union_map_copy(graph->edge[i].tagged_validity);
4009 uset = isl_union_map_domain(umap);
4010 source = isl_union_set_union(source, uset);
4011 source = isl_union_set_coalesce(source);
4013 umap = isl_union_map_copy(graph->edge[i].tagged_validity);
4014 uset = isl_union_map_range(umap);
4015 sink = isl_union_set_union(sink, uset);
4016 sink = isl_union_set_coalesce(sink);
4019 if (any)
4020 any = has_adjacent_true_conditions(graph, source, sink);
4022 isl_union_set_free(source);
4023 isl_union_set_free(sink);
4024 return any;
4025 error:
4026 isl_union_set_free(source);
4027 isl_union_set_free(sink);
4028 return -1;
4031 /* Compute a schedule for a connected dependence graph and return
4032 * the updated schedule node.
4034 * We try to find a sequence of as many schedule rows as possible that result
4035 * in non-negative dependence distances (independent of the previous rows
4036 * in the sequence, i.e., such that the sequence is tilable), with as
4037 * many of the initial rows as possible satisfying the coincidence constraints.
4038 * If we can't find any more rows we either
4039 * - split between SCCs and start over (assuming we found an interesting
4040 * pair of SCCs between which to split)
4041 * - continue with the next band (assuming the current band has at least
4042 * one row)
4043 * - try to carry as many dependences as possible and continue with the next
4044 * band
4045 * In each case, we first insert a band node in the schedule tree
4046 * if any rows have been computed.
4048 * If Feautrier's algorithm is selected, we first recursively try to satisfy
4049 * as many validity dependences as possible. When all validity dependences
4050 * are satisfied we extend the schedule to a full-dimensional schedule.
4052 * If we manage to complete the schedule, we insert a band node
4053 * (if any schedule rows were computed) and we finish off by topologically
4054 * sorting the statements based on the remaining dependences.
4056 * If ctx->opt->schedule_outer_coincidence is set, then we force the
4057 * outermost dimension to satisfy the coincidence constraints. If this
4058 * turns out to be impossible, we fall back on the general scheme above
4059 * and try to carry as many dependences as possible.
4061 * If "graph" contains both condition and conditional validity dependences,
4062 * then we need to check that that the conditional schedule constraint
4063 * is satisfied, i.e., there are no violated conditional validity dependences
4064 * that are adjacent to any non-local condition dependences.
4065 * If there are, then we mark all those adjacent condition dependences
4066 * as local and recompute the current band. Those dependences that
4067 * are marked local will then be forced to be local.
4068 * The initial computation is performed with no dependences marked as local.
4069 * If we are lucky, then there will be no violated conditional validity
4070 * dependences adjacent to any non-local condition dependences.
4071 * Otherwise, we mark some additional condition dependences as local and
4072 * recompute. We continue this process until there are no violations left or
4073 * until we are no longer able to compute a schedule.
4074 * Since there are only a finite number of dependences,
4075 * there will only be a finite number of iterations.
4077 static __isl_give isl_schedule_node *compute_schedule_wcc(
4078 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph)
4080 int has_coincidence;
4081 int use_coincidence;
4082 int force_coincidence = 0;
4083 int check_conditional;
4084 isl_ctx *ctx;
4086 if (!node)
4087 return NULL;
4089 ctx = isl_schedule_node_get_ctx(node);
4090 if (detect_sccs(ctx, graph) < 0)
4091 return isl_schedule_node_free(node);
4092 if (sort_sccs(graph) < 0)
4093 return isl_schedule_node_free(node);
4095 if (compute_maxvar(graph) < 0)
4096 return isl_schedule_node_free(node);
4098 if (need_feautrier_step(ctx, graph))
4099 return compute_schedule_wcc_feautrier(node, graph);
4101 clear_local_edges(graph);
4102 check_conditional = need_condition_check(graph);
4103 has_coincidence = has_any_coincidence(graph);
4105 if (ctx->opt->schedule_outer_coincidence)
4106 force_coincidence = 1;
4108 use_coincidence = has_coincidence;
4109 while (graph->n_row < graph->maxvar) {
4110 isl_vec *sol;
4111 int violated;
4112 int coincident;
4114 graph->src_scc = -1;
4115 graph->dst_scc = -1;
4117 if (setup_lp(ctx, graph, use_coincidence) < 0)
4118 return isl_schedule_node_free(node);
4119 sol = solve_lp(graph);
4120 if (!sol)
4121 return isl_schedule_node_free(node);
4122 if (sol->size == 0) {
4123 int empty = graph->n_total_row == graph->band_start;
4125 isl_vec_free(sol);
4126 if (use_coincidence && (!force_coincidence || !empty)) {
4127 use_coincidence = 0;
4128 continue;
4130 if (!ctx->opt->schedule_maximize_band_depth && !empty)
4131 return compute_next_band(node, graph, 1);
4132 if (graph->src_scc >= 0)
4133 return compute_split_schedule(node, graph);
4134 if (!empty)
4135 return compute_next_band(node, graph, 1);
4136 return carry_dependences(node, graph);
4138 coincident = !has_coincidence || use_coincidence;
4139 if (update_schedule(graph, sol, 1, coincident) < 0)
4140 return isl_schedule_node_free(node);
4142 if (!check_conditional)
4143 continue;
4144 violated = has_violated_conditional_constraint(ctx, graph);
4145 if (violated < 0)
4146 return isl_schedule_node_free(node);
4147 if (!violated)
4148 continue;
4149 if (reset_band(graph) < 0)
4150 return isl_schedule_node_free(node);
4151 use_coincidence = has_coincidence;
4154 if (graph->n_total_row > graph->band_start) {
4155 node = insert_current_band(node, graph, 1);
4156 node = isl_schedule_node_child(node, 0);
4158 node = sort_statements(node, graph);
4159 if (graph->n_total_row > graph->band_start)
4160 node = isl_schedule_node_parent(node);
4162 return node;
4165 /* Compute a schedule for each group of nodes identified by node->scc
4166 * separately and then combine them in a sequence node (or as set node
4167 * if graph->weak is set) inserted at position "node" of the schedule tree.
4168 * Return the updated schedule node.
4170 * If "wcc" is set then each of the groups belongs to a single
4171 * weakly connected component in the dependence graph so that
4172 * there is no need for compute_sub_schedule to look for weakly
4173 * connected components.
4175 static __isl_give isl_schedule_node *compute_component_schedule(
4176 __isl_take isl_schedule_node *node, struct isl_sched_graph *graph,
4177 int wcc)
4179 int component, i;
4180 int n, n_edge;
4181 int orig_total_row;
4182 isl_ctx *ctx;
4183 isl_union_set_list *filters;
4185 if (!node)
4186 return NULL;
4187 ctx = isl_schedule_node_get_ctx(node);
4189 filters = extract_sccs(ctx, graph);
4190 if (graph->weak)
4191 node = isl_schedule_node_insert_set(node, filters);
4192 else
4193 node = isl_schedule_node_insert_sequence(node, filters);
4195 orig_total_row = graph->n_total_row;
4196 for (component = 0; component < graph->scc; ++component) {
4197 n = 0;
4198 for (i = 0; i < graph->n; ++i)
4199 if (graph->node[i].scc == component)
4200 n++;
4201 n_edge = 0;
4202 for (i = 0; i < graph->n_edge; ++i)
4203 if (graph->edge[i].src->scc == component &&
4204 graph->edge[i].dst->scc == component)
4205 n_edge++;
4207 node = isl_schedule_node_child(node, component);
4208 node = isl_schedule_node_child(node, 0);
4209 node = compute_sub_schedule(node, ctx, graph, n, n_edge,
4210 &node_scc_exactly,
4211 &edge_scc_exactly, component, wcc);
4212 node = isl_schedule_node_parent(node);
4213 node = isl_schedule_node_parent(node);
4214 graph->n_total_row = orig_total_row;
4217 return node;
4220 /* Compute a schedule for the given dependence graph and insert it at "node".
4221 * Return the updated schedule node.
4223 * We first check if the graph is connected (through validity and conditional
4224 * validity dependences) and, if not, compute a schedule
4225 * for each component separately.
4226 * If schedule_fuse is set to minimal fusion, then we check for strongly
4227 * connected components instead and compute a separate schedule for
4228 * each such strongly connected component.
4230 static __isl_give isl_schedule_node *compute_schedule(isl_schedule_node *node,
4231 struct isl_sched_graph *graph)
4233 isl_ctx *ctx;
4235 if (!node)
4236 return NULL;
4238 ctx = isl_schedule_node_get_ctx(node);
4239 if (ctx->opt->schedule_fuse == ISL_SCHEDULE_FUSE_MIN) {
4240 if (detect_sccs(ctx, graph) < 0)
4241 return isl_schedule_node_free(node);
4242 } else {
4243 if (detect_wccs(ctx, graph) < 0)
4244 return isl_schedule_node_free(node);
4247 if (graph->scc > 1)
4248 return compute_component_schedule(node, graph, 1);
4250 return compute_schedule_wcc(node, graph);
4253 /* Compute a schedule on sc->domain that respects the given schedule
4254 * constraints.
4256 * In particular, the schedule respects all the validity dependences.
4257 * If the default isl scheduling algorithm is used, it tries to minimize
4258 * the dependence distances over the proximity dependences.
4259 * If Feautrier's scheduling algorithm is used, the proximity dependence
4260 * distances are only minimized during the extension to a full-dimensional
4261 * schedule.
4263 * If there are any condition and conditional validity dependences,
4264 * then the conditional validity dependences may be violated inside
4265 * a tilable band, provided they have no adjacent non-local
4266 * condition dependences.
4268 * The context is included in the domain before the nodes of
4269 * the graphs are extracted in order to be able to exploit
4270 * any possible additional equalities.
4271 * However, the returned schedule contains the original domain
4272 * (before this intersection).
4274 __isl_give isl_schedule *isl_schedule_constraints_compute_schedule(
4275 __isl_take isl_schedule_constraints *sc)
4277 isl_ctx *ctx = isl_schedule_constraints_get_ctx(sc);
4278 struct isl_sched_graph graph = { 0 };
4279 isl_schedule *sched;
4280 isl_schedule_node *node;
4281 isl_union_set *domain;
4282 struct isl_extract_edge_data data;
4283 enum isl_edge_type i;
4284 int r;
4286 sc = isl_schedule_constraints_align_params(sc);
4287 if (!sc)
4288 return NULL;
4290 graph.n = isl_union_set_n_set(sc->domain);
4291 if (graph.n == 0) {
4292 isl_union_set *domain = isl_union_set_copy(sc->domain);
4293 sched = isl_schedule_from_domain(domain);
4294 goto done;
4296 if (graph_alloc(ctx, &graph, graph.n,
4297 isl_schedule_constraints_n_map(sc)) < 0)
4298 goto error;
4299 if (compute_max_row(&graph, sc) < 0)
4300 goto error;
4301 graph.root = 1;
4302 graph.n = 0;
4303 domain = isl_union_set_copy(sc->domain);
4304 domain = isl_union_set_intersect_params(domain,
4305 isl_set_copy(sc->context));
4306 r = isl_union_set_foreach_set(domain, &extract_node, &graph);
4307 isl_union_set_free(domain);
4308 if (r < 0)
4309 goto error;
4310 if (graph_init_table(ctx, &graph) < 0)
4311 goto error;
4312 for (i = isl_edge_first; i <= isl_edge_last; ++i)
4313 graph.max_edge[i] = isl_union_map_n_map(sc->constraint[i]);
4314 if (graph_init_edge_tables(ctx, &graph) < 0)
4315 goto error;
4316 graph.n_edge = 0;
4317 data.graph = &graph;
4318 for (i = isl_edge_first; i <= isl_edge_last; ++i) {
4319 data.type = i;
4320 if (isl_union_map_foreach_map(sc->constraint[i],
4321 &extract_edge, &data) < 0)
4322 goto error;
4325 node = isl_schedule_node_from_domain(isl_union_set_copy(sc->domain));
4326 node = isl_schedule_node_child(node, 0);
4327 node = compute_schedule(node, &graph);
4328 sched = isl_schedule_node_get_schedule(node);
4329 isl_schedule_node_free(node);
4331 done:
4332 graph_free(ctx, &graph);
4333 isl_schedule_constraints_free(sc);
4335 return sched;
4336 error:
4337 graph_free(ctx, &graph);
4338 isl_schedule_constraints_free(sc);
4339 return NULL;
4342 /* Compute a schedule for the given union of domains that respects
4343 * all the validity dependences and minimizes
4344 * the dependence distances over the proximity dependences.
4346 * This function is kept for backward compatibility.
4348 __isl_give isl_schedule *isl_union_set_compute_schedule(
4349 __isl_take isl_union_set *domain,
4350 __isl_take isl_union_map *validity,
4351 __isl_take isl_union_map *proximity)
4353 isl_schedule_constraints *sc;
4355 sc = isl_schedule_constraints_on_domain(domain);
4356 sc = isl_schedule_constraints_set_validity(sc, validity);
4357 sc = isl_schedule_constraints_set_proximity(sc, proximity);
4359 return isl_schedule_constraints_compute_schedule(sc);