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,
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>
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>
25 #include <isl/union_set.h>
28 #include <isl_dim_map.h>
29 #include <isl/map_to_basic_set.h>
31 #include <isl_options_private.h>
32 #include <isl_tarjan.h>
33 #include <isl_morph.h>
36 * The scheduling algorithm implemented in this file was inspired by
37 * Bondhugula et al., "Automatic Transformations for Communication-Minimized
38 * Parallelization and Locality Optimization in the Polyhedral Model".
42 isl_edge_validity
= 0,
43 isl_edge_first
= isl_edge_validity
,
46 isl_edge_conditional_validity
,
48 isl_edge_last
= isl_edge_proximity
51 /* The constraints that need to be satisfied by a schedule on "domain".
53 * "context" specifies extra constraints on the parameters.
55 * "validity" constraints map domain elements i to domain elements
56 * that should be scheduled after i. (Hard constraint)
57 * "proximity" constraints map domain elements i to domains elements
58 * that should be scheduled as early as possible after i (or before i).
61 * "condition" and "conditional_validity" constraints map possibly "tagged"
62 * domain elements i -> s to "tagged" domain elements j -> t.
63 * The elements of the "conditional_validity" constraints, but without the
64 * tags (i.e., the elements i -> j) are treated as validity constraints,
65 * except that during the construction of a tilable band,
66 * the elements of the "conditional_validity" constraints may be violated
67 * provided that all adjacent elements of the "condition" constraints
68 * are local within the band.
69 * A dependence is local within a band if domain and range are mapped
70 * to the same schedule point by the band.
72 struct isl_schedule_constraints
{
73 isl_union_set
*domain
;
76 isl_union_map
*constraint
[isl_edge_last
+ 1];
79 __isl_give isl_schedule_constraints
*isl_schedule_constraints_copy(
80 __isl_keep isl_schedule_constraints
*sc
)
83 isl_schedule_constraints
*sc_copy
;
86 ctx
= isl_union_set_get_ctx(sc
->domain
);
87 sc_copy
= isl_calloc_type(ctx
, struct isl_schedule_constraints
);
91 sc_copy
->domain
= isl_union_set_copy(sc
->domain
);
92 sc_copy
->context
= isl_set_copy(sc
->context
);
93 if (!sc_copy
->domain
|| !sc_copy
->context
)
94 return isl_schedule_constraints_free(sc_copy
);
96 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
) {
97 sc_copy
->constraint
[i
] = isl_union_map_copy(sc
->constraint
[i
]);
98 if (!sc_copy
->constraint
[i
])
99 return isl_schedule_constraints_free(sc_copy
);
106 /* Construct an isl_schedule_constraints object for computing a schedule
107 * on "domain". The initial object does not impose any constraints.
109 __isl_give isl_schedule_constraints
*isl_schedule_constraints_on_domain(
110 __isl_take isl_union_set
*domain
)
114 isl_schedule_constraints
*sc
;
115 isl_union_map
*empty
;
116 enum isl_edge_type i
;
121 ctx
= isl_union_set_get_ctx(domain
);
122 sc
= isl_calloc_type(ctx
, struct isl_schedule_constraints
);
126 space
= isl_union_set_get_space(domain
);
128 sc
->context
= isl_set_universe(isl_space_copy(space
));
129 empty
= isl_union_map_empty(space
);
130 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
) {
131 sc
->constraint
[i
] = isl_union_map_copy(empty
);
132 if (!sc
->constraint
[i
])
133 sc
->domain
= isl_union_set_free(sc
->domain
);
135 isl_union_map_free(empty
);
137 if (!sc
->domain
|| !sc
->context
)
138 return isl_schedule_constraints_free(sc
);
142 isl_union_set_free(domain
);
146 /* Replace the context of "sc" by "context".
148 __isl_give isl_schedule_constraints
*isl_schedule_constraints_set_context(
149 __isl_take isl_schedule_constraints
*sc
, __isl_take isl_set
*context
)
154 isl_set_free(sc
->context
);
155 sc
->context
= context
;
159 isl_schedule_constraints_free(sc
);
160 isl_set_free(context
);
164 /* Replace the validity constraints of "sc" by "validity".
166 __isl_give isl_schedule_constraints
*isl_schedule_constraints_set_validity(
167 __isl_take isl_schedule_constraints
*sc
,
168 __isl_take isl_union_map
*validity
)
170 if (!sc
|| !validity
)
173 isl_union_map_free(sc
->constraint
[isl_edge_validity
]);
174 sc
->constraint
[isl_edge_validity
] = validity
;
178 isl_schedule_constraints_free(sc
);
179 isl_union_map_free(validity
);
183 /* Replace the coincidence constraints of "sc" by "coincidence".
185 __isl_give isl_schedule_constraints
*isl_schedule_constraints_set_coincidence(
186 __isl_take isl_schedule_constraints
*sc
,
187 __isl_take isl_union_map
*coincidence
)
189 if (!sc
|| !coincidence
)
192 isl_union_map_free(sc
->constraint
[isl_edge_coincidence
]);
193 sc
->constraint
[isl_edge_coincidence
] = coincidence
;
197 isl_schedule_constraints_free(sc
);
198 isl_union_map_free(coincidence
);
202 /* Replace the proximity constraints of "sc" by "proximity".
204 __isl_give isl_schedule_constraints
*isl_schedule_constraints_set_proximity(
205 __isl_take isl_schedule_constraints
*sc
,
206 __isl_take isl_union_map
*proximity
)
208 if (!sc
|| !proximity
)
211 isl_union_map_free(sc
->constraint
[isl_edge_proximity
]);
212 sc
->constraint
[isl_edge_proximity
] = proximity
;
216 isl_schedule_constraints_free(sc
);
217 isl_union_map_free(proximity
);
221 /* Replace the conditional validity constraints of "sc" by "condition"
224 __isl_give isl_schedule_constraints
*
225 isl_schedule_constraints_set_conditional_validity(
226 __isl_take isl_schedule_constraints
*sc
,
227 __isl_take isl_union_map
*condition
,
228 __isl_take isl_union_map
*validity
)
230 if (!sc
|| !condition
|| !validity
)
233 isl_union_map_free(sc
->constraint
[isl_edge_condition
]);
234 sc
->constraint
[isl_edge_condition
] = condition
;
235 isl_union_map_free(sc
->constraint
[isl_edge_conditional_validity
]);
236 sc
->constraint
[isl_edge_conditional_validity
] = validity
;
240 isl_schedule_constraints_free(sc
);
241 isl_union_map_free(condition
);
242 isl_union_map_free(validity
);
246 __isl_null isl_schedule_constraints
*isl_schedule_constraints_free(
247 __isl_take isl_schedule_constraints
*sc
)
249 enum isl_edge_type i
;
254 isl_union_set_free(sc
->domain
);
255 isl_set_free(sc
->context
);
256 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
)
257 isl_union_map_free(sc
->constraint
[i
]);
264 isl_ctx
*isl_schedule_constraints_get_ctx(
265 __isl_keep isl_schedule_constraints
*sc
)
267 return sc
? isl_union_set_get_ctx(sc
->domain
) : NULL
;
270 /* Return the validity constraints of "sc".
272 __isl_give isl_union_map
*isl_schedule_constraints_get_validity(
273 __isl_keep isl_schedule_constraints
*sc
)
278 return isl_union_map_copy(sc
->constraint
[isl_edge_validity
]);
281 /* Return the coincidence constraints of "sc".
283 __isl_give isl_union_map
*isl_schedule_constraints_get_coincidence(
284 __isl_keep isl_schedule_constraints
*sc
)
289 return isl_union_map_copy(sc
->constraint
[isl_edge_coincidence
]);
292 /* Return the conditional validity constraints of "sc".
294 __isl_give isl_union_map
*isl_schedule_constraints_get_conditional_validity(
295 __isl_keep isl_schedule_constraints
*sc
)
301 isl_union_map_copy(sc
->constraint
[isl_edge_conditional_validity
]);
304 /* Return the conditions for the conditional validity constraints of "sc".
306 __isl_give isl_union_map
*
307 isl_schedule_constraints_get_conditional_validity_condition(
308 __isl_keep isl_schedule_constraints
*sc
)
313 return isl_union_map_copy(sc
->constraint
[isl_edge_condition
]);
316 void isl_schedule_constraints_dump(__isl_keep isl_schedule_constraints
*sc
)
321 fprintf(stderr
, "domain: ");
322 isl_union_set_dump(sc
->domain
);
323 fprintf(stderr
, "context: ");
324 isl_set_dump(sc
->context
);
325 fprintf(stderr
, "validity: ");
326 isl_union_map_dump(sc
->constraint
[isl_edge_validity
]);
327 fprintf(stderr
, "proximity: ");
328 isl_union_map_dump(sc
->constraint
[isl_edge_proximity
]);
329 fprintf(stderr
, "coincidence: ");
330 isl_union_map_dump(sc
->constraint
[isl_edge_coincidence
]);
331 fprintf(stderr
, "condition: ");
332 isl_union_map_dump(sc
->constraint
[isl_edge_condition
]);
333 fprintf(stderr
, "conditional_validity: ");
334 isl_union_map_dump(sc
->constraint
[isl_edge_conditional_validity
]);
337 /* Align the parameters of the fields of "sc".
339 static __isl_give isl_schedule_constraints
*
340 isl_schedule_constraints_align_params(__isl_take isl_schedule_constraints
*sc
)
343 enum isl_edge_type i
;
348 space
= isl_union_set_get_space(sc
->domain
);
349 space
= isl_space_align_params(space
, isl_set_get_space(sc
->context
));
350 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
)
351 space
= isl_space_align_params(space
,
352 isl_union_map_get_space(sc
->constraint
[i
]));
354 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
) {
355 sc
->constraint
[i
] = isl_union_map_align_params(
356 sc
->constraint
[i
], isl_space_copy(space
));
357 if (!sc
->constraint
[i
])
358 space
= isl_space_free(space
);
360 sc
->context
= isl_set_align_params(sc
->context
, isl_space_copy(space
));
361 sc
->domain
= isl_union_set_align_params(sc
->domain
, space
);
362 if (!sc
->context
|| !sc
->domain
)
363 return isl_schedule_constraints_free(sc
);
368 /* Return the total number of isl_maps in the constraints of "sc".
370 static __isl_give
int isl_schedule_constraints_n_map(
371 __isl_keep isl_schedule_constraints
*sc
)
373 enum isl_edge_type i
;
376 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
)
377 n
+= isl_union_map_n_map(sc
->constraint
[i
]);
382 /* Internal information about a node that is used during the construction
384 * space represents the space in which the domain lives
385 * sched is a matrix representation of the schedule being constructed
386 * for this node; if compressed is set, then this schedule is
387 * defined over the compressed domain space
388 * sched_map is an isl_map representation of the same (partial) schedule
389 * sched_map may be NULL; if compressed is set, then this map
390 * is defined over the uncompressed domain space
391 * rank is the number of linearly independent rows in the linear part
393 * the columns of cmap represent a change of basis for the schedule
394 * coefficients; the first rank columns span the linear part of
396 * cinv is the inverse of cmap.
397 * start is the first variable in the LP problem in the sequences that
398 * represents the schedule coefficients of this node
399 * nvar is the dimension of the domain
400 * nparam is the number of parameters or 0 if we are not constructing
401 * a parametric schedule
403 * If compressed is set, then hull represents the constraints
404 * that were used to derive the compression, while compress and
405 * decompress map the original space to the compressed space and
408 * scc is the index of SCC (or WCC) this node belongs to
410 * coincident contains a boolean for each of the rows of the schedule,
411 * indicating whether the corresponding scheduling dimension satisfies
412 * the coincidence constraints in the sense that the corresponding
413 * dependence distances are zero.
415 struct isl_sched_node
{
419 isl_multi_aff
*compress
;
420 isl_multi_aff
*decompress
;
435 static int node_has_space(const void *entry
, const void *val
)
437 struct isl_sched_node
*node
= (struct isl_sched_node
*)entry
;
438 isl_space
*dim
= (isl_space
*)val
;
440 return isl_space_is_equal(node
->space
, dim
);
443 static int node_scc_exactly(struct isl_sched_node
*node
, int scc
)
445 return node
->scc
== scc
;
448 static int node_scc_at_most(struct isl_sched_node
*node
, int scc
)
450 return node
->scc
<= scc
;
453 static int node_scc_at_least(struct isl_sched_node
*node
, int scc
)
455 return node
->scc
>= scc
;
458 /* An edge in the dependence graph. An edge may be used to
459 * ensure validity of the generated schedule, to minimize the dependence
462 * map is the dependence relation, with i -> j in the map if j depends on i
463 * tagged_condition and tagged_validity contain the union of all tagged
464 * condition or conditional validity dependence relations that
465 * specialize the dependence relation "map"; that is,
466 * if (i -> a) -> (j -> b) is an element of "tagged_condition"
467 * or "tagged_validity", then i -> j is an element of "map".
468 * If these fields are NULL, then they represent the empty relation.
469 * src is the source node
470 * dst is the sink node
471 * validity is set if the edge is used to ensure correctness
472 * coincidence is used to enforce zero dependence distances
473 * proximity is set if the edge is used to minimize dependence distances
474 * condition is set if the edge represents a condition
475 * for a conditional validity schedule constraint
476 * local can only be set for condition edges and indicates that
477 * the dependence distance over the edge should be zero
478 * conditional_validity is set if the edge is used to conditionally
481 * For validity edges, start and end mark the sequence of inequality
482 * constraints in the LP problem that encode the validity constraint
483 * corresponding to this edge.
485 struct isl_sched_edge
{
487 isl_union_map
*tagged_condition
;
488 isl_union_map
*tagged_validity
;
490 struct isl_sched_node
*src
;
491 struct isl_sched_node
*dst
;
493 unsigned validity
: 1;
494 unsigned coincidence
: 1;
495 unsigned proximity
: 1;
497 unsigned condition
: 1;
498 unsigned conditional_validity
: 1;
504 /* Internal information about the dependence graph used during
505 * the construction of the schedule.
507 * intra_hmap is a cache, mapping dependence relations to their dual,
508 * for dependences from a node to itself
509 * inter_hmap is a cache, mapping dependence relations to their dual,
510 * for dependences between distinct nodes
511 * if compression is involved then the key for these maps
512 * it the original, uncompressed dependence relation, while
513 * the value is the dual of the compressed dependence relation.
515 * n is the number of nodes
516 * node is the list of nodes
517 * maxvar is the maximal number of variables over all nodes
518 * max_row is the allocated number of rows in the schedule
519 * n_row is the current (maximal) number of linearly independent
520 * rows in the node schedules
521 * n_total_row is the current number of rows in the node schedules
522 * band_start is the starting row in the node schedules of the current band
523 * root is set if this graph is the original dependence graph,
524 * without any splitting
526 * sorted contains a list of node indices sorted according to the
527 * SCC to which a node belongs
529 * n_edge is the number of edges
530 * edge is the list of edges
531 * max_edge contains the maximal number of edges of each type;
532 * in particular, it contains the number of edges in the inital graph.
533 * edge_table contains pointers into the edge array, hashed on the source
534 * and sink spaces; there is one such table for each type;
535 * a given edge may be referenced from more than one table
536 * if the corresponding relation appears in more than of the
537 * sets of dependences
539 * node_table contains pointers into the node array, hashed on the space
541 * region contains a list of variable sequences that should be non-trivial
543 * lp contains the (I)LP problem used to obtain new schedule rows
545 * src_scc and dst_scc are the source and sink SCCs of an edge with
546 * conflicting constraints
548 * scc represents the number of components
549 * weak is set if the components are weakly connected
551 struct isl_sched_graph
{
552 isl_map_to_basic_set
*intra_hmap
;
553 isl_map_to_basic_set
*inter_hmap
;
555 struct isl_sched_node
*node
;
568 struct isl_sched_edge
*edge
;
570 int max_edge
[isl_edge_last
+ 1];
571 struct isl_hash_table
*edge_table
[isl_edge_last
+ 1];
573 struct isl_hash_table
*node_table
;
574 struct isl_region
*region
;
585 /* Initialize node_table based on the list of nodes.
587 static int graph_init_table(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
591 graph
->node_table
= isl_hash_table_alloc(ctx
, graph
->n
);
592 if (!graph
->node_table
)
595 for (i
= 0; i
< graph
->n
; ++i
) {
596 struct isl_hash_table_entry
*entry
;
599 hash
= isl_space_get_hash(graph
->node
[i
].space
);
600 entry
= isl_hash_table_find(ctx
, graph
->node_table
, hash
,
602 graph
->node
[i
].space
, 1);
605 entry
->data
= &graph
->node
[i
];
611 /* Return a pointer to the node that lives within the given space,
612 * or NULL if there is no such node.
614 static struct isl_sched_node
*graph_find_node(isl_ctx
*ctx
,
615 struct isl_sched_graph
*graph
, __isl_keep isl_space
*dim
)
617 struct isl_hash_table_entry
*entry
;
620 hash
= isl_space_get_hash(dim
);
621 entry
= isl_hash_table_find(ctx
, graph
->node_table
, hash
,
622 &node_has_space
, dim
, 0);
624 return entry
? entry
->data
: NULL
;
627 static int edge_has_src_and_dst(const void *entry
, const void *val
)
629 const struct isl_sched_edge
*edge
= entry
;
630 const struct isl_sched_edge
*temp
= val
;
632 return edge
->src
== temp
->src
&& edge
->dst
== temp
->dst
;
635 /* Add the given edge to graph->edge_table[type].
637 static int graph_edge_table_add(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
638 enum isl_edge_type type
, struct isl_sched_edge
*edge
)
640 struct isl_hash_table_entry
*entry
;
643 hash
= isl_hash_init();
644 hash
= isl_hash_builtin(hash
, edge
->src
);
645 hash
= isl_hash_builtin(hash
, edge
->dst
);
646 entry
= isl_hash_table_find(ctx
, graph
->edge_table
[type
], hash
,
647 &edge_has_src_and_dst
, edge
, 1);
655 /* Allocate the edge_tables based on the maximal number of edges of
658 static int graph_init_edge_tables(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
662 for (i
= 0; i
<= isl_edge_last
; ++i
) {
663 graph
->edge_table
[i
] = isl_hash_table_alloc(ctx
,
665 if (!graph
->edge_table
[i
])
672 /* If graph->edge_table[type] contains an edge from the given source
673 * to the given destination, then return the hash table entry of this edge.
674 * Otherwise, return NULL.
676 static struct isl_hash_table_entry
*graph_find_edge_entry(
677 struct isl_sched_graph
*graph
,
678 enum isl_edge_type type
,
679 struct isl_sched_node
*src
, struct isl_sched_node
*dst
)
681 isl_ctx
*ctx
= isl_space_get_ctx(src
->space
);
683 struct isl_sched_edge temp
= { .src
= src
, .dst
= dst
};
685 hash
= isl_hash_init();
686 hash
= isl_hash_builtin(hash
, temp
.src
);
687 hash
= isl_hash_builtin(hash
, temp
.dst
);
688 return isl_hash_table_find(ctx
, graph
->edge_table
[type
], hash
,
689 &edge_has_src_and_dst
, &temp
, 0);
693 /* If graph->edge_table[type] contains an edge from the given source
694 * to the given destination, then return this edge.
695 * Otherwise, return NULL.
697 static struct isl_sched_edge
*graph_find_edge(struct isl_sched_graph
*graph
,
698 enum isl_edge_type type
,
699 struct isl_sched_node
*src
, struct isl_sched_node
*dst
)
701 struct isl_hash_table_entry
*entry
;
703 entry
= graph_find_edge_entry(graph
, type
, src
, dst
);
710 /* Check whether the dependence graph has an edge of the given type
711 * between the given two nodes.
713 static int graph_has_edge(struct isl_sched_graph
*graph
,
714 enum isl_edge_type type
,
715 struct isl_sched_node
*src
, struct isl_sched_node
*dst
)
717 struct isl_sched_edge
*edge
;
720 edge
= graph_find_edge(graph
, type
, src
, dst
);
724 empty
= isl_map_plain_is_empty(edge
->map
);
731 /* Look for any edge with the same src, dst and map fields as "model".
733 * Return the matching edge if one can be found.
734 * Return "model" if no matching edge is found.
735 * Return NULL on error.
737 static struct isl_sched_edge
*graph_find_matching_edge(
738 struct isl_sched_graph
*graph
, struct isl_sched_edge
*model
)
740 enum isl_edge_type i
;
741 struct isl_sched_edge
*edge
;
743 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
) {
746 edge
= graph_find_edge(graph
, i
, model
->src
, model
->dst
);
749 is_equal
= isl_map_plain_is_equal(model
->map
, edge
->map
);
759 /* Remove the given edge from all the edge_tables that refer to it.
761 static void graph_remove_edge(struct isl_sched_graph
*graph
,
762 struct isl_sched_edge
*edge
)
764 isl_ctx
*ctx
= isl_map_get_ctx(edge
->map
);
765 enum isl_edge_type i
;
767 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
) {
768 struct isl_hash_table_entry
*entry
;
770 entry
= graph_find_edge_entry(graph
, i
, edge
->src
, edge
->dst
);
773 if (entry
->data
!= edge
)
775 isl_hash_table_remove(ctx
, graph
->edge_table
[i
], entry
);
779 /* Check whether the dependence graph has any edge
780 * between the given two nodes.
782 static int graph_has_any_edge(struct isl_sched_graph
*graph
,
783 struct isl_sched_node
*src
, struct isl_sched_node
*dst
)
785 enum isl_edge_type i
;
788 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
) {
789 r
= graph_has_edge(graph
, i
, src
, dst
);
797 /* Check whether the dependence graph has a validity edge
798 * between the given two nodes.
800 * Conditional validity edges are essentially validity edges that
801 * can be ignored if the corresponding condition edges are iteration private.
802 * Here, we are only checking for the presence of validity
803 * edges, so we need to consider the conditional validity edges too.
804 * In particular, this function is used during the detection
805 * of strongly connected components and we cannot ignore
806 * conditional validity edges during this detection.
808 static int graph_has_validity_edge(struct isl_sched_graph
*graph
,
809 struct isl_sched_node
*src
, struct isl_sched_node
*dst
)
813 r
= graph_has_edge(graph
, isl_edge_validity
, src
, dst
);
817 return graph_has_edge(graph
, isl_edge_conditional_validity
, src
, dst
);
820 static int graph_alloc(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
821 int n_node
, int n_edge
)
826 graph
->n_edge
= n_edge
;
827 graph
->node
= isl_calloc_array(ctx
, struct isl_sched_node
, graph
->n
);
828 graph
->sorted
= isl_calloc_array(ctx
, int, graph
->n
);
829 graph
->region
= isl_alloc_array(ctx
, struct isl_region
, graph
->n
);
830 graph
->edge
= isl_calloc_array(ctx
,
831 struct isl_sched_edge
, graph
->n_edge
);
833 graph
->intra_hmap
= isl_map_to_basic_set_alloc(ctx
, 2 * n_edge
);
834 graph
->inter_hmap
= isl_map_to_basic_set_alloc(ctx
, 2 * n_edge
);
836 if (!graph
->node
|| !graph
->region
|| (graph
->n_edge
&& !graph
->edge
) ||
840 for(i
= 0; i
< graph
->n
; ++i
)
841 graph
->sorted
[i
] = i
;
846 static void graph_free(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
850 isl_map_to_basic_set_free(graph
->intra_hmap
);
851 isl_map_to_basic_set_free(graph
->inter_hmap
);
854 for (i
= 0; i
< graph
->n
; ++i
) {
855 isl_space_free(graph
->node
[i
].space
);
856 isl_set_free(graph
->node
[i
].hull
);
857 isl_multi_aff_free(graph
->node
[i
].compress
);
858 isl_multi_aff_free(graph
->node
[i
].decompress
);
859 isl_mat_free(graph
->node
[i
].sched
);
860 isl_map_free(graph
->node
[i
].sched_map
);
861 isl_mat_free(graph
->node
[i
].cmap
);
862 isl_mat_free(graph
->node
[i
].cinv
);
864 free(graph
->node
[i
].coincident
);
869 for (i
= 0; i
< graph
->n_edge
; ++i
) {
870 isl_map_free(graph
->edge
[i
].map
);
871 isl_union_map_free(graph
->edge
[i
].tagged_condition
);
872 isl_union_map_free(graph
->edge
[i
].tagged_validity
);
876 for (i
= 0; i
<= isl_edge_last
; ++i
)
877 isl_hash_table_free(ctx
, graph
->edge_table
[i
]);
878 isl_hash_table_free(ctx
, graph
->node_table
);
879 isl_basic_set_free(graph
->lp
);
882 /* For each "set" on which this function is called, increment
883 * graph->n by one and update graph->maxvar.
885 static int init_n_maxvar(__isl_take isl_set
*set
, void *user
)
887 struct isl_sched_graph
*graph
= user
;
888 int nvar
= isl_set_dim(set
, isl_dim_set
);
891 if (nvar
> graph
->maxvar
)
892 graph
->maxvar
= nvar
;
899 /* Add the number of basic maps in "map" to *n.
901 static int add_n_basic_map(__isl_take isl_map
*map
, void *user
)
905 *n
+= isl_map_n_basic_map(map
);
911 /* Compute the number of rows that should be allocated for the schedule.
912 * In particular, we need one row for each variable or one row
913 * for each basic map in the dependences.
914 * Note that it is practically impossible to exhaust both
915 * the number of dependences and the number of variables.
917 static int compute_max_row(struct isl_sched_graph
*graph
,
918 __isl_keep isl_schedule_constraints
*sc
)
920 enum isl_edge_type i
;
925 if (isl_union_set_foreach_set(sc
->domain
, &init_n_maxvar
, graph
) < 0)
928 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
)
929 if (isl_union_map_foreach_map(sc
->constraint
[i
],
930 &add_n_basic_map
, &n_edge
) < 0)
932 graph
->max_row
= n_edge
+ graph
->maxvar
;
937 /* Does "bset" have any defining equalities for its set variables?
939 static int has_any_defining_equality(__isl_keep isl_basic_set
*bset
)
946 n
= isl_basic_set_dim(bset
, isl_dim_set
);
947 for (i
= 0; i
< n
; ++i
) {
950 has
= isl_basic_set_has_defining_equality(bset
, isl_dim_set
, i
,
959 /* Add a new node to the graph representing the given space.
960 * "nvar" is the (possibly compressed) number of variables and
961 * may be smaller than then number of set variables in "space"
962 * if "compressed" is set.
963 * If "compressed" is set, then "hull" represents the constraints
964 * that were used to derive the compression, while "compress" and
965 * "decompress" map the original space to the compressed space and
967 * If "compressed" is not set, then "hull", "compress" and "decompress"
970 static int add_node(struct isl_sched_graph
*graph
, __isl_take isl_space
*space
,
971 int nvar
, int compressed
, __isl_take isl_set
*hull
,
972 __isl_take isl_multi_aff
*compress
,
973 __isl_take isl_multi_aff
*decompress
)
983 ctx
= isl_space_get_ctx(space
);
984 nparam
= isl_space_dim(space
, isl_dim_param
);
985 if (!ctx
->opt
->schedule_parametric
)
987 sched
= isl_mat_alloc(ctx
, 0, 1 + nparam
+ nvar
);
988 graph
->node
[graph
->n
].space
= space
;
989 graph
->node
[graph
->n
].nvar
= nvar
;
990 graph
->node
[graph
->n
].nparam
= nparam
;
991 graph
->node
[graph
->n
].sched
= sched
;
992 graph
->node
[graph
->n
].sched_map
= NULL
;
993 coincident
= isl_calloc_array(ctx
, int, graph
->max_row
);
994 graph
->node
[graph
->n
].coincident
= coincident
;
995 graph
->node
[graph
->n
].compressed
= compressed
;
996 graph
->node
[graph
->n
].hull
= hull
;
997 graph
->node
[graph
->n
].compress
= compress
;
998 graph
->node
[graph
->n
].decompress
= decompress
;
1001 if (!space
|| !sched
|| (graph
->max_row
&& !coincident
))
1003 if (compressed
&& (!hull
|| !compress
|| !decompress
))
1009 /* Add a new node to the graph representing the given set.
1011 * If any of the set variables is defined by an equality, then
1012 * we perform variable compression such that we can perform
1013 * the scheduling on the compressed domain.
1015 static int extract_node(__isl_take isl_set
*set
, void *user
)
1020 isl_basic_set
*hull
;
1023 isl_multi_aff
*compress
, *decompress
;
1024 struct isl_sched_graph
*graph
= user
;
1026 space
= isl_set_get_space(set
);
1027 hull
= isl_set_affine_hull(set
);
1028 hull
= isl_basic_set_remove_divs(hull
);
1029 nvar
= isl_space_dim(space
, isl_dim_set
);
1030 has_equality
= has_any_defining_equality(hull
);
1032 if (has_equality
< 0)
1034 if (!has_equality
) {
1035 isl_basic_set_free(hull
);
1036 return add_node(graph
, space
, nvar
, 0, NULL
, NULL
, NULL
);
1039 morph
= isl_basic_set_variable_compression(hull
, isl_dim_set
);
1040 nvar
= isl_morph_ran_dim(morph
, isl_dim_set
);
1041 compress
= isl_morph_get_var_multi_aff(morph
);
1042 morph
= isl_morph_inverse(morph
);
1043 decompress
= isl_morph_get_var_multi_aff(morph
);
1044 isl_morph_free(morph
);
1046 hull_set
= isl_set_from_basic_set(hull
);
1047 return add_node(graph
, space
, nvar
, 1, hull_set
, compress
, decompress
);
1049 isl_basic_set_free(hull
);
1050 isl_space_free(space
);
1054 struct isl_extract_edge_data
{
1055 enum isl_edge_type type
;
1056 struct isl_sched_graph
*graph
;
1059 /* Merge edge2 into edge1, freeing the contents of edge2.
1060 * "type" is the type of the schedule constraint from which edge2 was
1062 * Return 0 on success and -1 on failure.
1064 * edge1 and edge2 are assumed to have the same value for the map field.
1066 static int merge_edge(enum isl_edge_type type
, struct isl_sched_edge
*edge1
,
1067 struct isl_sched_edge
*edge2
)
1069 edge1
->validity
|= edge2
->validity
;
1070 edge1
->coincidence
|= edge2
->coincidence
;
1071 edge1
->proximity
|= edge2
->proximity
;
1072 edge1
->condition
|= edge2
->condition
;
1073 edge1
->conditional_validity
|= edge2
->conditional_validity
;
1074 isl_map_free(edge2
->map
);
1076 if (type
== isl_edge_condition
) {
1077 if (!edge1
->tagged_condition
)
1078 edge1
->tagged_condition
= edge2
->tagged_condition
;
1080 edge1
->tagged_condition
=
1081 isl_union_map_union(edge1
->tagged_condition
,
1082 edge2
->tagged_condition
);
1085 if (type
== isl_edge_conditional_validity
) {
1086 if (!edge1
->tagged_validity
)
1087 edge1
->tagged_validity
= edge2
->tagged_validity
;
1089 edge1
->tagged_validity
=
1090 isl_union_map_union(edge1
->tagged_validity
,
1091 edge2
->tagged_validity
);
1094 if (type
== isl_edge_condition
&& !edge1
->tagged_condition
)
1096 if (type
== isl_edge_conditional_validity
&& !edge1
->tagged_validity
)
1102 /* Insert dummy tags in domain and range of "map".
1104 * In particular, if "map" is of the form
1110 * [A -> dummy_tag] -> [B -> dummy_tag]
1112 * where the dummy_tags are identical and equal to any dummy tags
1113 * introduced by any other call to this function.
1115 static __isl_give isl_map
*insert_dummy_tags(__isl_take isl_map
*map
)
1121 isl_set
*domain
, *range
;
1123 ctx
= isl_map_get_ctx(map
);
1125 id
= isl_id_alloc(ctx
, NULL
, &dummy
);
1126 space
= isl_space_params(isl_map_get_space(map
));
1127 space
= isl_space_set_from_params(space
);
1128 space
= isl_space_set_tuple_id(space
, isl_dim_set
, id
);
1129 space
= isl_space_map_from_set(space
);
1131 domain
= isl_map_wrap(map
);
1132 range
= isl_map_wrap(isl_map_universe(space
));
1133 map
= isl_map_from_domain_and_range(domain
, range
);
1134 map
= isl_map_zip(map
);
1139 /* Given that at least one of "src" or "dst" is compressed, return
1140 * a map between the spaces of these nodes restricted to the affine
1141 * hull that was used in the compression.
1143 static __isl_give isl_map
*extract_hull(struct isl_sched_node
*src
,
1144 struct isl_sched_node
*dst
)
1148 if (src
->compressed
)
1149 dom
= isl_set_copy(src
->hull
);
1151 dom
= isl_set_universe(isl_space_copy(src
->space
));
1152 if (dst
->compressed
)
1153 ran
= isl_set_copy(dst
->hull
);
1155 ran
= isl_set_universe(isl_space_copy(dst
->space
));
1157 return isl_map_from_domain_and_range(dom
, ran
);
1160 /* Intersect the domains of the nested relations in domain and range
1161 * of "tagged" with "map".
1163 static __isl_give isl_map
*map_intersect_domains(__isl_take isl_map
*tagged
,
1164 __isl_keep isl_map
*map
)
1168 tagged
= isl_map_zip(tagged
);
1169 set
= isl_map_wrap(isl_map_copy(map
));
1170 tagged
= isl_map_intersect_domain(tagged
, set
);
1171 tagged
= isl_map_zip(tagged
);
1175 /* Add a new edge to the graph based on the given map
1176 * and add it to data->graph->edge_table[data->type].
1177 * If a dependence relation of a given type happens to be identical
1178 * to one of the dependence relations of a type that was added before,
1179 * then we don't create a new edge, but instead mark the original edge
1180 * as also representing a dependence of the current type.
1182 * Edges of type isl_edge_condition or isl_edge_conditional_validity
1183 * may be specified as "tagged" dependence relations. That is, "map"
1184 * may contain elements (i -> a) -> (j -> b), where i -> j denotes
1185 * the dependence on iterations and a and b are tags.
1186 * edge->map is set to the relation containing the elements i -> j,
1187 * while edge->tagged_condition and edge->tagged_validity contain
1188 * the union of all the "map" relations
1189 * for which extract_edge is called that result in the same edge->map.
1191 * If the source or the destination node is compressed, then
1192 * intersect both "map" and "tagged" with the constraints that
1193 * were used to construct the compression.
1194 * This ensures that there are no schedule constraints defined
1195 * outside of these domains, while the scheduler no longer has
1196 * any control over those outside parts.
1198 static int extract_edge(__isl_take isl_map
*map
, void *user
)
1200 isl_ctx
*ctx
= isl_map_get_ctx(map
);
1201 struct isl_extract_edge_data
*data
= user
;
1202 struct isl_sched_graph
*graph
= data
->graph
;
1203 struct isl_sched_node
*src
, *dst
;
1205 struct isl_sched_edge
*edge
;
1206 isl_map
*tagged
= NULL
;
1208 if (data
->type
== isl_edge_condition
||
1209 data
->type
== isl_edge_conditional_validity
) {
1210 if (isl_map_can_zip(map
)) {
1211 tagged
= isl_map_copy(map
);
1212 map
= isl_set_unwrap(isl_map_domain(isl_map_zip(map
)));
1214 tagged
= insert_dummy_tags(isl_map_copy(map
));
1218 dim
= isl_space_domain(isl_map_get_space(map
));
1219 src
= graph_find_node(ctx
, graph
, dim
);
1220 isl_space_free(dim
);
1221 dim
= isl_space_range(isl_map_get_space(map
));
1222 dst
= graph_find_node(ctx
, graph
, dim
);
1223 isl_space_free(dim
);
1227 isl_map_free(tagged
);
1231 if (src
->compressed
|| dst
->compressed
) {
1233 hull
= extract_hull(src
, dst
);
1235 tagged
= map_intersect_domains(tagged
, hull
);
1236 map
= isl_map_intersect(map
, hull
);
1239 graph
->edge
[graph
->n_edge
].src
= src
;
1240 graph
->edge
[graph
->n_edge
].dst
= dst
;
1241 graph
->edge
[graph
->n_edge
].map
= map
;
1242 graph
->edge
[graph
->n_edge
].validity
= 0;
1243 graph
->edge
[graph
->n_edge
].coincidence
= 0;
1244 graph
->edge
[graph
->n_edge
].proximity
= 0;
1245 graph
->edge
[graph
->n_edge
].condition
= 0;
1246 graph
->edge
[graph
->n_edge
].local
= 0;
1247 graph
->edge
[graph
->n_edge
].conditional_validity
= 0;
1248 graph
->edge
[graph
->n_edge
].tagged_condition
= NULL
;
1249 graph
->edge
[graph
->n_edge
].tagged_validity
= NULL
;
1250 if (data
->type
== isl_edge_validity
)
1251 graph
->edge
[graph
->n_edge
].validity
= 1;
1252 if (data
->type
== isl_edge_coincidence
)
1253 graph
->edge
[graph
->n_edge
].coincidence
= 1;
1254 if (data
->type
== isl_edge_proximity
)
1255 graph
->edge
[graph
->n_edge
].proximity
= 1;
1256 if (data
->type
== isl_edge_condition
) {
1257 graph
->edge
[graph
->n_edge
].condition
= 1;
1258 graph
->edge
[graph
->n_edge
].tagged_condition
=
1259 isl_union_map_from_map(tagged
);
1261 if (data
->type
== isl_edge_conditional_validity
) {
1262 graph
->edge
[graph
->n_edge
].conditional_validity
= 1;
1263 graph
->edge
[graph
->n_edge
].tagged_validity
=
1264 isl_union_map_from_map(tagged
);
1267 edge
= graph_find_matching_edge(graph
, &graph
->edge
[graph
->n_edge
]);
1272 if (edge
== &graph
->edge
[graph
->n_edge
])
1273 return graph_edge_table_add(ctx
, graph
, data
->type
,
1274 &graph
->edge
[graph
->n_edge
++]);
1276 if (merge_edge(data
->type
, edge
, &graph
->edge
[graph
->n_edge
]) < 0)
1279 return graph_edge_table_add(ctx
, graph
, data
->type
, edge
);
1282 /* Check whether there is any dependence from node[j] to node[i]
1283 * or from node[i] to node[j].
1285 static int node_follows_weak(int i
, int j
, void *user
)
1288 struct isl_sched_graph
*graph
= user
;
1290 f
= graph_has_any_edge(graph
, &graph
->node
[j
], &graph
->node
[i
]);
1293 return graph_has_any_edge(graph
, &graph
->node
[i
], &graph
->node
[j
]);
1296 /* Check whether there is a (conditional) validity dependence from node[j]
1297 * to node[i], forcing node[i] to follow node[j].
1299 static int node_follows_strong(int i
, int j
, void *user
)
1301 struct isl_sched_graph
*graph
= user
;
1303 return graph_has_validity_edge(graph
, &graph
->node
[j
], &graph
->node
[i
]);
1306 /* Use Tarjan's algorithm for computing the strongly connected components
1307 * in the dependence graph (only validity edges).
1308 * If weak is set, we consider the graph to be undirected and
1309 * we effectively compute the (weakly) connected components.
1310 * Additionally, we also consider other edges when weak is set.
1312 static int detect_ccs(isl_ctx
*ctx
, struct isl_sched_graph
*graph
, int weak
)
1315 struct isl_tarjan_graph
*g
= NULL
;
1317 g
= isl_tarjan_graph_init(ctx
, graph
->n
,
1318 weak
? &node_follows_weak
: &node_follows_strong
, graph
);
1327 while (g
->order
[i
] != -1) {
1328 graph
->node
[g
->order
[i
]].scc
= graph
->scc
;
1336 isl_tarjan_graph_free(g
);
1341 /* Apply Tarjan's algorithm to detect the strongly connected components
1342 * in the dependence graph.
1344 static int detect_sccs(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
1346 return detect_ccs(ctx
, graph
, 0);
1349 /* Apply Tarjan's algorithm to detect the (weakly) connected components
1350 * in the dependence graph.
1352 static int detect_wccs(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
1354 return detect_ccs(ctx
, graph
, 1);
1357 static int cmp_scc(const void *a
, const void *b
, void *data
)
1359 struct isl_sched_graph
*graph
= data
;
1363 return graph
->node
[*i1
].scc
- graph
->node
[*i2
].scc
;
1366 /* Sort the elements of graph->sorted according to the corresponding SCCs.
1368 static int sort_sccs(struct isl_sched_graph
*graph
)
1370 return isl_sort(graph
->sorted
, graph
->n
, sizeof(int), &cmp_scc
, graph
);
1373 /* Given a dependence relation R from "node" to itself,
1374 * construct the set of coefficients of valid constraints for elements
1375 * in that dependence relation.
1376 * In particular, the result contains tuples of coefficients
1377 * c_0, c_n, c_x such that
1379 * c_0 + c_n n + c_x y - c_x x >= 0 for each (x,y) in R
1383 * c_0 + c_n n + c_x d >= 0 for each d in delta R = { y - x | (x,y) in R }
1385 * We choose here to compute the dual of delta R.
1386 * Alternatively, we could have computed the dual of R, resulting
1387 * in a set of tuples c_0, c_n, c_x, c_y, and then
1388 * plugged in (c_0, c_n, c_x, -c_x).
1390 * If "node" has been compressed, then the dependence relation
1391 * is also compressed before the set of coefficients is computed.
1393 static __isl_give isl_basic_set
*intra_coefficients(
1394 struct isl_sched_graph
*graph
, struct isl_sched_node
*node
,
1395 __isl_take isl_map
*map
)
1399 isl_basic_set
*coef
;
1401 if (isl_map_to_basic_set_has(graph
->intra_hmap
, map
))
1402 return isl_map_to_basic_set_get(graph
->intra_hmap
, map
);
1404 key
= isl_map_copy(map
);
1405 if (node
->compressed
) {
1406 map
= isl_map_preimage_domain_multi_aff(map
,
1407 isl_multi_aff_copy(node
->decompress
));
1408 map
= isl_map_preimage_range_multi_aff(map
,
1409 isl_multi_aff_copy(node
->decompress
));
1411 delta
= isl_set_remove_divs(isl_map_deltas(map
));
1412 coef
= isl_set_coefficients(delta
);
1413 graph
->intra_hmap
= isl_map_to_basic_set_set(graph
->intra_hmap
, key
,
1414 isl_basic_set_copy(coef
));
1419 /* Given a dependence relation R, construct the set of coefficients
1420 * of valid constraints for elements in that dependence relation.
1421 * In particular, the result contains tuples of coefficients
1422 * c_0, c_n, c_x, c_y such that
1424 * c_0 + c_n n + c_x x + c_y y >= 0 for each (x,y) in R
1426 * If the source or destination nodes of "edge" have been compressed,
1427 * then the dependence relation is also compressed before
1428 * the set of coefficients is computed.
1430 static __isl_give isl_basic_set
*inter_coefficients(
1431 struct isl_sched_graph
*graph
, struct isl_sched_edge
*edge
,
1432 __isl_take isl_map
*map
)
1436 isl_basic_set
*coef
;
1438 if (isl_map_to_basic_set_has(graph
->inter_hmap
, map
))
1439 return isl_map_to_basic_set_get(graph
->inter_hmap
, map
);
1441 key
= isl_map_copy(map
);
1442 if (edge
->src
->compressed
)
1443 map
= isl_map_preimage_domain_multi_aff(map
,
1444 isl_multi_aff_copy(edge
->src
->decompress
));
1445 if (edge
->dst
->compressed
)
1446 map
= isl_map_preimage_range_multi_aff(map
,
1447 isl_multi_aff_copy(edge
->dst
->decompress
));
1448 set
= isl_map_wrap(isl_map_remove_divs(map
));
1449 coef
= isl_set_coefficients(set
);
1450 graph
->inter_hmap
= isl_map_to_basic_set_set(graph
->inter_hmap
, key
,
1451 isl_basic_set_copy(coef
));
1456 /* Add constraints to graph->lp that force validity for the given
1457 * dependence from a node i to itself.
1458 * That is, add constraints that enforce
1460 * (c_i_0 + c_i_n n + c_i_x y) - (c_i_0 + c_i_n n + c_i_x x)
1461 * = c_i_x (y - x) >= 0
1463 * for each (x,y) in R.
1464 * We obtain general constraints on coefficients (c_0, c_n, c_x)
1465 * of valid constraints for (y - x) and then plug in (0, 0, c_i_x^+ - c_i_x^-),
1466 * where c_i_x = c_i_x^+ - c_i_x^-, with c_i_x^+ and c_i_x^- non-negative.
1467 * In graph->lp, the c_i_x^- appear before their c_i_x^+ counterpart.
1469 * Actually, we do not construct constraints for the c_i_x themselves,
1470 * but for the coefficients of c_i_x written as a linear combination
1471 * of the columns in node->cmap.
1473 static int add_intra_validity_constraints(struct isl_sched_graph
*graph
,
1474 struct isl_sched_edge
*edge
)
1477 isl_map
*map
= isl_map_copy(edge
->map
);
1478 isl_ctx
*ctx
= isl_map_get_ctx(map
);
1480 isl_dim_map
*dim_map
;
1481 isl_basic_set
*coef
;
1482 struct isl_sched_node
*node
= edge
->src
;
1484 coef
= intra_coefficients(graph
, node
, map
);
1486 dim
= isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef
)));
1488 coef
= isl_basic_set_transform_dims(coef
, isl_dim_set
,
1489 isl_space_dim(dim
, isl_dim_set
), isl_mat_copy(node
->cmap
));
1493 total
= isl_basic_set_total_dim(graph
->lp
);
1494 dim_map
= isl_dim_map_alloc(ctx
, total
);
1495 isl_dim_map_range(dim_map
, node
->start
+ 2 * node
->nparam
+ 1, 2,
1496 isl_space_dim(dim
, isl_dim_set
), 1,
1498 isl_dim_map_range(dim_map
, node
->start
+ 2 * node
->nparam
+ 2, 2,
1499 isl_space_dim(dim
, isl_dim_set
), 1,
1501 graph
->lp
= isl_basic_set_extend_constraints(graph
->lp
,
1502 coef
->n_eq
, coef
->n_ineq
);
1503 graph
->lp
= isl_basic_set_add_constraints_dim_map(graph
->lp
,
1505 isl_space_free(dim
);
1509 isl_space_free(dim
);
1513 /* Add constraints to graph->lp that force validity for the given
1514 * dependence from node i to node j.
1515 * That is, add constraints that enforce
1517 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x) >= 0
1519 * for each (x,y) in R.
1520 * We obtain general constraints on coefficients (c_0, c_n, c_x, c_y)
1521 * of valid constraints for R and then plug in
1522 * (c_j_0 - c_i_0, c_j_n^+ - c_j_n^- - (c_i_n^+ - c_i_n^-),
1523 * c_j_x^+ - c_j_x^- - (c_i_x^+ - c_i_x^-)),
1524 * where c_* = c_*^+ - c_*^-, with c_*^+ and c_*^- non-negative.
1525 * In graph->lp, the c_*^- appear before their c_*^+ counterpart.
1527 * Actually, we do not construct constraints for the c_*_x themselves,
1528 * but for the coefficients of c_*_x written as a linear combination
1529 * of the columns in node->cmap.
1531 static int add_inter_validity_constraints(struct isl_sched_graph
*graph
,
1532 struct isl_sched_edge
*edge
)
1535 isl_map
*map
= isl_map_copy(edge
->map
);
1536 isl_ctx
*ctx
= isl_map_get_ctx(map
);
1538 isl_dim_map
*dim_map
;
1539 isl_basic_set
*coef
;
1540 struct isl_sched_node
*src
= edge
->src
;
1541 struct isl_sched_node
*dst
= edge
->dst
;
1543 coef
= inter_coefficients(graph
, edge
, map
);
1545 dim
= isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef
)));
1547 coef
= isl_basic_set_transform_dims(coef
, isl_dim_set
,
1548 isl_space_dim(dim
, isl_dim_set
), isl_mat_copy(src
->cmap
));
1549 coef
= isl_basic_set_transform_dims(coef
, isl_dim_set
,
1550 isl_space_dim(dim
, isl_dim_set
) + src
->nvar
,
1551 isl_mat_copy(dst
->cmap
));
1555 total
= isl_basic_set_total_dim(graph
->lp
);
1556 dim_map
= isl_dim_map_alloc(ctx
, total
);
1558 isl_dim_map_range(dim_map
, dst
->start
, 0, 0, 0, 1, 1);
1559 isl_dim_map_range(dim_map
, dst
->start
+ 1, 2, 1, 1, dst
->nparam
, -1);
1560 isl_dim_map_range(dim_map
, dst
->start
+ 2, 2, 1, 1, dst
->nparam
, 1);
1561 isl_dim_map_range(dim_map
, dst
->start
+ 2 * dst
->nparam
+ 1, 2,
1562 isl_space_dim(dim
, isl_dim_set
) + src
->nvar
, 1,
1564 isl_dim_map_range(dim_map
, dst
->start
+ 2 * dst
->nparam
+ 2, 2,
1565 isl_space_dim(dim
, isl_dim_set
) + src
->nvar
, 1,
1568 isl_dim_map_range(dim_map
, src
->start
, 0, 0, 0, 1, -1);
1569 isl_dim_map_range(dim_map
, src
->start
+ 1, 2, 1, 1, src
->nparam
, 1);
1570 isl_dim_map_range(dim_map
, src
->start
+ 2, 2, 1, 1, src
->nparam
, -1);
1571 isl_dim_map_range(dim_map
, src
->start
+ 2 * src
->nparam
+ 1, 2,
1572 isl_space_dim(dim
, isl_dim_set
), 1,
1574 isl_dim_map_range(dim_map
, src
->start
+ 2 * src
->nparam
+ 2, 2,
1575 isl_space_dim(dim
, isl_dim_set
), 1,
1578 edge
->start
= graph
->lp
->n_ineq
;
1579 graph
->lp
= isl_basic_set_extend_constraints(graph
->lp
,
1580 coef
->n_eq
, coef
->n_ineq
);
1581 graph
->lp
= isl_basic_set_add_constraints_dim_map(graph
->lp
,
1585 isl_space_free(dim
);
1586 edge
->end
= graph
->lp
->n_ineq
;
1590 isl_space_free(dim
);
1594 /* Add constraints to graph->lp that bound the dependence distance for the given
1595 * dependence from a node i to itself.
1596 * If s = 1, we add the constraint
1598 * c_i_x (y - x) <= m_0 + m_n n
1602 * -c_i_x (y - x) + m_0 + m_n n >= 0
1604 * for each (x,y) in R.
1605 * If s = -1, we add the constraint
1607 * -c_i_x (y - x) <= m_0 + m_n n
1611 * c_i_x (y - x) + m_0 + m_n n >= 0
1613 * for each (x,y) in R.
1614 * We obtain general constraints on coefficients (c_0, c_n, c_x)
1615 * of valid constraints for (y - x) and then plug in (m_0, m_n, -s * c_i_x),
1616 * with each coefficient (except m_0) represented as a pair of non-negative
1619 * Actually, we do not construct constraints for the c_i_x themselves,
1620 * but for the coefficients of c_i_x written as a linear combination
1621 * of the columns in node->cmap.
1624 * If "local" is set, then we add constraints
1626 * c_i_x (y - x) <= 0
1630 * -c_i_x (y - x) <= 0
1632 * instead, forcing the dependence distance to be (less than or) equal to 0.
1633 * That is, we plug in (0, 0, -s * c_i_x),
1634 * Note that dependences marked local are treated as validity constraints
1635 * by add_all_validity_constraints and therefore also have
1636 * their distances bounded by 0 from below.
1638 static int add_intra_proximity_constraints(struct isl_sched_graph
*graph
,
1639 struct isl_sched_edge
*edge
, int s
, int local
)
1643 isl_map
*map
= isl_map_copy(edge
->map
);
1644 isl_ctx
*ctx
= isl_map_get_ctx(map
);
1646 isl_dim_map
*dim_map
;
1647 isl_basic_set
*coef
;
1648 struct isl_sched_node
*node
= edge
->src
;
1650 coef
= intra_coefficients(graph
, node
, map
);
1652 dim
= isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef
)));
1654 coef
= isl_basic_set_transform_dims(coef
, isl_dim_set
,
1655 isl_space_dim(dim
, isl_dim_set
), isl_mat_copy(node
->cmap
));
1659 nparam
= isl_space_dim(node
->space
, isl_dim_param
);
1660 total
= isl_basic_set_total_dim(graph
->lp
);
1661 dim_map
= isl_dim_map_alloc(ctx
, total
);
1664 isl_dim_map_range(dim_map
, 1, 0, 0, 0, 1, 1);
1665 isl_dim_map_range(dim_map
, 4, 2, 1, 1, nparam
, -1);
1666 isl_dim_map_range(dim_map
, 5, 2, 1, 1, nparam
, 1);
1668 isl_dim_map_range(dim_map
, node
->start
+ 2 * node
->nparam
+ 1, 2,
1669 isl_space_dim(dim
, isl_dim_set
), 1,
1671 isl_dim_map_range(dim_map
, node
->start
+ 2 * node
->nparam
+ 2, 2,
1672 isl_space_dim(dim
, isl_dim_set
), 1,
1674 graph
->lp
= isl_basic_set_extend_constraints(graph
->lp
,
1675 coef
->n_eq
, coef
->n_ineq
);
1676 graph
->lp
= isl_basic_set_add_constraints_dim_map(graph
->lp
,
1678 isl_space_free(dim
);
1682 isl_space_free(dim
);
1686 /* Add constraints to graph->lp that bound the dependence distance for the given
1687 * dependence from node i to node j.
1688 * If s = 1, we add the constraint
1690 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x)
1695 * -(c_j_0 + c_j_n n + c_j_x y) + (c_i_0 + c_i_n n + c_i_x x) +
1698 * for each (x,y) in R.
1699 * If s = -1, we add the constraint
1701 * -((c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x))
1706 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x) +
1709 * for each (x,y) in R.
1710 * We obtain general constraints on coefficients (c_0, c_n, c_x, c_y)
1711 * of valid constraints for R and then plug in
1712 * (m_0 - s*c_j_0 + s*c_i_0, m_n - s*c_j_n + s*c_i_n,
1714 * with each coefficient (except m_0, c_j_0 and c_i_0)
1715 * represented as a pair of non-negative coefficients.
1717 * Actually, we do not construct constraints for the c_*_x themselves,
1718 * but for the coefficients of c_*_x written as a linear combination
1719 * of the columns in node->cmap.
1722 * If "local" is set, then we add constraints
1724 * (c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x) <= 0
1728 * -((c_j_0 + c_j_n n + c_j_x y) - (c_i_0 + c_i_n n + c_i_x x)) <= 0
1730 * instead, forcing the dependence distance to be (less than or) equal to 0.
1731 * That is, we plug in
1732 * (-s*c_j_0 + s*c_i_0, -s*c_j_n + s*c_i_n, -s*c_j_x+s*c_i_x).
1733 * Note that dependences marked local are treated as validity constraints
1734 * by add_all_validity_constraints and therefore also have
1735 * their distances bounded by 0 from below.
1737 static int add_inter_proximity_constraints(struct isl_sched_graph
*graph
,
1738 struct isl_sched_edge
*edge
, int s
, int local
)
1742 isl_map
*map
= isl_map_copy(edge
->map
);
1743 isl_ctx
*ctx
= isl_map_get_ctx(map
);
1745 isl_dim_map
*dim_map
;
1746 isl_basic_set
*coef
;
1747 struct isl_sched_node
*src
= edge
->src
;
1748 struct isl_sched_node
*dst
= edge
->dst
;
1750 coef
= inter_coefficients(graph
, edge
, map
);
1752 dim
= isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef
)));
1754 coef
= isl_basic_set_transform_dims(coef
, isl_dim_set
,
1755 isl_space_dim(dim
, isl_dim_set
), isl_mat_copy(src
->cmap
));
1756 coef
= isl_basic_set_transform_dims(coef
, isl_dim_set
,
1757 isl_space_dim(dim
, isl_dim_set
) + src
->nvar
,
1758 isl_mat_copy(dst
->cmap
));
1762 nparam
= isl_space_dim(src
->space
, isl_dim_param
);
1763 total
= isl_basic_set_total_dim(graph
->lp
);
1764 dim_map
= isl_dim_map_alloc(ctx
, total
);
1767 isl_dim_map_range(dim_map
, 1, 0, 0, 0, 1, 1);
1768 isl_dim_map_range(dim_map
, 4, 2, 1, 1, nparam
, -1);
1769 isl_dim_map_range(dim_map
, 5, 2, 1, 1, nparam
, 1);
1772 isl_dim_map_range(dim_map
, dst
->start
, 0, 0, 0, 1, -s
);
1773 isl_dim_map_range(dim_map
, dst
->start
+ 1, 2, 1, 1, dst
->nparam
, s
);
1774 isl_dim_map_range(dim_map
, dst
->start
+ 2, 2, 1, 1, dst
->nparam
, -s
);
1775 isl_dim_map_range(dim_map
, dst
->start
+ 2 * dst
->nparam
+ 1, 2,
1776 isl_space_dim(dim
, isl_dim_set
) + src
->nvar
, 1,
1778 isl_dim_map_range(dim_map
, dst
->start
+ 2 * dst
->nparam
+ 2, 2,
1779 isl_space_dim(dim
, isl_dim_set
) + src
->nvar
, 1,
1782 isl_dim_map_range(dim_map
, src
->start
, 0, 0, 0, 1, s
);
1783 isl_dim_map_range(dim_map
, src
->start
+ 1, 2, 1, 1, src
->nparam
, -s
);
1784 isl_dim_map_range(dim_map
, src
->start
+ 2, 2, 1, 1, src
->nparam
, s
);
1785 isl_dim_map_range(dim_map
, src
->start
+ 2 * src
->nparam
+ 1, 2,
1786 isl_space_dim(dim
, isl_dim_set
), 1,
1788 isl_dim_map_range(dim_map
, src
->start
+ 2 * src
->nparam
+ 2, 2,
1789 isl_space_dim(dim
, isl_dim_set
), 1,
1792 graph
->lp
= isl_basic_set_extend_constraints(graph
->lp
,
1793 coef
->n_eq
, coef
->n_ineq
);
1794 graph
->lp
= isl_basic_set_add_constraints_dim_map(graph
->lp
,
1796 isl_space_free(dim
);
1800 isl_space_free(dim
);
1804 /* Add all validity constraints to graph->lp.
1806 * An edge that is forced to be local needs to have its dependence
1807 * distances equal to zero. We take care of bounding them by 0 from below
1808 * here. add_all_proximity_constraints takes care of bounding them by 0
1811 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
1812 * Otherwise, we ignore them.
1814 static int add_all_validity_constraints(struct isl_sched_graph
*graph
,
1815 int use_coincidence
)
1819 for (i
= 0; i
< graph
->n_edge
; ++i
) {
1820 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
1823 local
= edge
->local
|| (edge
->coincidence
&& use_coincidence
);
1824 if (!edge
->validity
&& !local
)
1826 if (edge
->src
!= edge
->dst
)
1828 if (add_intra_validity_constraints(graph
, edge
) < 0)
1832 for (i
= 0; i
< graph
->n_edge
; ++i
) {
1833 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
1836 local
= edge
->local
|| (edge
->coincidence
&& use_coincidence
);
1837 if (!edge
->validity
&& !local
)
1839 if (edge
->src
== edge
->dst
)
1841 if (add_inter_validity_constraints(graph
, edge
) < 0)
1848 /* Add constraints to graph->lp that bound the dependence distance
1849 * for all dependence relations.
1850 * If a given proximity dependence is identical to a validity
1851 * dependence, then the dependence distance is already bounded
1852 * from below (by zero), so we only need to bound the distance
1853 * from above. (This includes the case of "local" dependences
1854 * which are treated as validity dependence by add_all_validity_constraints.)
1855 * Otherwise, we need to bound the distance both from above and from below.
1857 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
1858 * Otherwise, we ignore them.
1860 static int add_all_proximity_constraints(struct isl_sched_graph
*graph
,
1861 int use_coincidence
)
1865 for (i
= 0; i
< graph
->n_edge
; ++i
) {
1866 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
1869 local
= edge
->local
|| (edge
->coincidence
&& use_coincidence
);
1870 if (!edge
->proximity
&& !local
)
1872 if (edge
->src
== edge
->dst
&&
1873 add_intra_proximity_constraints(graph
, edge
, 1, local
) < 0)
1875 if (edge
->src
!= edge
->dst
&&
1876 add_inter_proximity_constraints(graph
, edge
, 1, local
) < 0)
1878 if (edge
->validity
|| local
)
1880 if (edge
->src
== edge
->dst
&&
1881 add_intra_proximity_constraints(graph
, edge
, -1, 0) < 0)
1883 if (edge
->src
!= edge
->dst
&&
1884 add_inter_proximity_constraints(graph
, edge
, -1, 0) < 0)
1891 /* Compute a basis for the rows in the linear part of the schedule
1892 * and extend this basis to a full basis. The remaining rows
1893 * can then be used to force linear independence from the rows
1896 * In particular, given the schedule rows S, we compute
1901 * with H the Hermite normal form of S. That is, all but the
1902 * first rank columns of H are zero and so each row in S is
1903 * a linear combination of the first rank rows of Q.
1904 * The matrix Q is then transposed because we will write the
1905 * coefficients of the next schedule row as a column vector s
1906 * and express this s as a linear combination s = Q c of the
1908 * Similarly, the matrix U is transposed such that we can
1909 * compute the coefficients c = U s from a schedule row s.
1911 static int node_update_cmap(struct isl_sched_node
*node
)
1914 int n_row
= isl_mat_rows(node
->sched
);
1916 H
= isl_mat_sub_alloc(node
->sched
, 0, n_row
,
1917 1 + node
->nparam
, node
->nvar
);
1919 H
= isl_mat_left_hermite(H
, 0, &U
, &Q
);
1920 isl_mat_free(node
->cmap
);
1921 isl_mat_free(node
->cinv
);
1922 node
->cmap
= isl_mat_transpose(Q
);
1923 node
->cinv
= isl_mat_transpose(U
);
1924 node
->rank
= isl_mat_initial_non_zero_cols(H
);
1927 if (!node
->cmap
|| !node
->cinv
|| node
->rank
< 0)
1932 /* How many times should we count the constraints in "edge"?
1934 * If carry is set, then we are counting the number of
1935 * (validity or conditional validity) constraints that will be added
1936 * in setup_carry_lp and we count each edge exactly once.
1938 * Otherwise, we count as follows
1939 * validity -> 1 (>= 0)
1940 * validity+proximity -> 2 (>= 0 and upper bound)
1941 * proximity -> 2 (lower and upper bound)
1942 * local(+any) -> 2 (>= 0 and <= 0)
1944 * If an edge is only marked conditional_validity then it counts
1945 * as zero since it is only checked afterwards.
1947 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
1948 * Otherwise, we ignore them.
1950 static int edge_multiplicity(struct isl_sched_edge
*edge
, int carry
,
1951 int use_coincidence
)
1953 if (carry
&& !edge
->validity
&& !edge
->conditional_validity
)
1957 if (edge
->proximity
|| edge
->local
)
1959 if (use_coincidence
&& edge
->coincidence
)
1966 /* Count the number of equality and inequality constraints
1967 * that will be added for the given map.
1969 * "use_coincidence" is set if we should take into account coincidence edges.
1971 static int count_map_constraints(struct isl_sched_graph
*graph
,
1972 struct isl_sched_edge
*edge
, __isl_take isl_map
*map
,
1973 int *n_eq
, int *n_ineq
, int carry
, int use_coincidence
)
1975 isl_basic_set
*coef
;
1976 int f
= edge_multiplicity(edge
, carry
, use_coincidence
);
1983 if (edge
->src
== edge
->dst
)
1984 coef
= intra_coefficients(graph
, edge
->src
, map
);
1986 coef
= inter_coefficients(graph
, edge
, map
);
1989 *n_eq
+= f
* coef
->n_eq
;
1990 *n_ineq
+= f
* coef
->n_ineq
;
1991 isl_basic_set_free(coef
);
1996 /* Count the number of equality and inequality constraints
1997 * that will be added to the main lp problem.
1998 * We count as follows
1999 * validity -> 1 (>= 0)
2000 * validity+proximity -> 2 (>= 0 and upper bound)
2001 * proximity -> 2 (lower and upper bound)
2002 * local(+any) -> 2 (>= 0 and <= 0)
2004 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
2005 * Otherwise, we ignore them.
2007 static int count_constraints(struct isl_sched_graph
*graph
,
2008 int *n_eq
, int *n_ineq
, int use_coincidence
)
2012 *n_eq
= *n_ineq
= 0;
2013 for (i
= 0; i
< graph
->n_edge
; ++i
) {
2014 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
2015 isl_map
*map
= isl_map_copy(edge
->map
);
2017 if (count_map_constraints(graph
, edge
, map
, n_eq
, n_ineq
,
2018 0, use_coincidence
) < 0)
2025 /* Count the number of constraints that will be added by
2026 * add_bound_coefficient_constraints and increment *n_eq and *n_ineq
2029 * In practice, add_bound_coefficient_constraints only adds inequalities.
2031 static int count_bound_coefficient_constraints(isl_ctx
*ctx
,
2032 struct isl_sched_graph
*graph
, int *n_eq
, int *n_ineq
)
2036 if (ctx
->opt
->schedule_max_coefficient
== -1)
2039 for (i
= 0; i
< graph
->n
; ++i
)
2040 *n_ineq
+= 2 * graph
->node
[i
].nparam
+ 2 * graph
->node
[i
].nvar
;
2045 /* Add constraints that bound the values of the variable and parameter
2046 * coefficients of the schedule.
2048 * The maximal value of the coefficients is defined by the option
2049 * 'schedule_max_coefficient'.
2051 static int add_bound_coefficient_constraints(isl_ctx
*ctx
,
2052 struct isl_sched_graph
*graph
)
2055 int max_coefficient
;
2058 max_coefficient
= ctx
->opt
->schedule_max_coefficient
;
2060 if (max_coefficient
== -1)
2063 total
= isl_basic_set_total_dim(graph
->lp
);
2065 for (i
= 0; i
< graph
->n
; ++i
) {
2066 struct isl_sched_node
*node
= &graph
->node
[i
];
2067 for (j
= 0; j
< 2 * node
->nparam
+ 2 * node
->nvar
; ++j
) {
2069 k
= isl_basic_set_alloc_inequality(graph
->lp
);
2072 dim
= 1 + node
->start
+ 1 + j
;
2073 isl_seq_clr(graph
->lp
->ineq
[k
], 1 + total
);
2074 isl_int_set_si(graph
->lp
->ineq
[k
][dim
], -1);
2075 isl_int_set_si(graph
->lp
->ineq
[k
][0], max_coefficient
);
2082 /* Construct an ILP problem for finding schedule coefficients
2083 * that result in non-negative, but small dependence distances
2084 * over all dependences.
2085 * In particular, the dependence distances over proximity edges
2086 * are bounded by m_0 + m_n n and we compute schedule coefficients
2087 * with small values (preferably zero) of m_n and m_0.
2089 * All variables of the ILP are non-negative. The actual coefficients
2090 * may be negative, so each coefficient is represented as the difference
2091 * of two non-negative variables. The negative part always appears
2092 * immediately before the positive part.
2093 * Other than that, the variables have the following order
2095 * - sum of positive and negative parts of m_n coefficients
2097 * - sum of positive and negative parts of all c_n coefficients
2098 * (unconstrained when computing non-parametric schedules)
2099 * - sum of positive and negative parts of all c_x coefficients
2100 * - positive and negative parts of m_n coefficients
2103 * - positive and negative parts of c_i_n (if parametric)
2104 * - positive and negative parts of c_i_x
2106 * The c_i_x are not represented directly, but through the columns of
2107 * node->cmap. That is, the computed values are for variable t_i_x
2108 * such that c_i_x = Q t_i_x with Q equal to node->cmap.
2110 * The constraints are those from the edges plus two or three equalities
2111 * to express the sums.
2113 * If "use_coincidence" is set, then we treat coincidence edges as local edges.
2114 * Otherwise, we ignore them.
2116 static int setup_lp(isl_ctx
*ctx
, struct isl_sched_graph
*graph
,
2117 int use_coincidence
)
2127 int max_constant_term
;
2129 max_constant_term
= ctx
->opt
->schedule_max_constant_term
;
2131 parametric
= ctx
->opt
->schedule_parametric
;
2132 nparam
= isl_space_dim(graph
->node
[0].space
, isl_dim_param
);
2134 total
= param_pos
+ 2 * nparam
;
2135 for (i
= 0; i
< graph
->n
; ++i
) {
2136 struct isl_sched_node
*node
= &graph
->node
[graph
->sorted
[i
]];
2137 if (node_update_cmap(node
) < 0)
2139 node
->start
= total
;
2140 total
+= 1 + 2 * (node
->nparam
+ node
->nvar
);
2143 if (count_constraints(graph
, &n_eq
, &n_ineq
, use_coincidence
) < 0)
2145 if (count_bound_coefficient_constraints(ctx
, graph
, &n_eq
, &n_ineq
) < 0)
2148 dim
= isl_space_set_alloc(ctx
, 0, total
);
2149 isl_basic_set_free(graph
->lp
);
2150 n_eq
+= 2 + parametric
;
2151 if (max_constant_term
!= -1)
2154 graph
->lp
= isl_basic_set_alloc_space(dim
, 0, n_eq
, n_ineq
);
2156 k
= isl_basic_set_alloc_equality(graph
->lp
);
2159 isl_seq_clr(graph
->lp
->eq
[k
], 1 + total
);
2160 isl_int_set_si(graph
->lp
->eq
[k
][1], -1);
2161 for (i
= 0; i
< 2 * nparam
; ++i
)
2162 isl_int_set_si(graph
->lp
->eq
[k
][1 + param_pos
+ i
], 1);
2165 k
= isl_basic_set_alloc_equality(graph
->lp
);
2168 isl_seq_clr(graph
->lp
->eq
[k
], 1 + total
);
2169 isl_int_set_si(graph
->lp
->eq
[k
][3], -1);
2170 for (i
= 0; i
< graph
->n
; ++i
) {
2171 int pos
= 1 + graph
->node
[i
].start
+ 1;
2173 for (j
= 0; j
< 2 * graph
->node
[i
].nparam
; ++j
)
2174 isl_int_set_si(graph
->lp
->eq
[k
][pos
+ j
], 1);
2178 k
= isl_basic_set_alloc_equality(graph
->lp
);
2181 isl_seq_clr(graph
->lp
->eq
[k
], 1 + total
);
2182 isl_int_set_si(graph
->lp
->eq
[k
][4], -1);
2183 for (i
= 0; i
< graph
->n
; ++i
) {
2184 struct isl_sched_node
*node
= &graph
->node
[i
];
2185 int pos
= 1 + node
->start
+ 1 + 2 * node
->nparam
;
2187 for (j
= 0; j
< 2 * node
->nvar
; ++j
)
2188 isl_int_set_si(graph
->lp
->eq
[k
][pos
+ j
], 1);
2191 if (max_constant_term
!= -1)
2192 for (i
= 0; i
< graph
->n
; ++i
) {
2193 struct isl_sched_node
*node
= &graph
->node
[i
];
2194 k
= isl_basic_set_alloc_inequality(graph
->lp
);
2197 isl_seq_clr(graph
->lp
->ineq
[k
], 1 + total
);
2198 isl_int_set_si(graph
->lp
->ineq
[k
][1 + node
->start
], -1);
2199 isl_int_set_si(graph
->lp
->ineq
[k
][0], max_constant_term
);
2202 if (add_bound_coefficient_constraints(ctx
, graph
) < 0)
2204 if (add_all_validity_constraints(graph
, use_coincidence
) < 0)
2206 if (add_all_proximity_constraints(graph
, use_coincidence
) < 0)
2212 /* Analyze the conflicting constraint found by
2213 * isl_tab_basic_set_non_trivial_lexmin. If it corresponds to the validity
2214 * constraint of one of the edges between distinct nodes, living, moreover
2215 * in distinct SCCs, then record the source and sink SCC as this may
2216 * be a good place to cut between SCCs.
2218 static int check_conflict(int con
, void *user
)
2221 struct isl_sched_graph
*graph
= user
;
2223 if (graph
->src_scc
>= 0)
2226 con
-= graph
->lp
->n_eq
;
2228 if (con
>= graph
->lp
->n_ineq
)
2231 for (i
= 0; i
< graph
->n_edge
; ++i
) {
2232 if (!graph
->edge
[i
].validity
)
2234 if (graph
->edge
[i
].src
== graph
->edge
[i
].dst
)
2236 if (graph
->edge
[i
].src
->scc
== graph
->edge
[i
].dst
->scc
)
2238 if (graph
->edge
[i
].start
> con
)
2240 if (graph
->edge
[i
].end
<= con
)
2242 graph
->src_scc
= graph
->edge
[i
].src
->scc
;
2243 graph
->dst_scc
= graph
->edge
[i
].dst
->scc
;
2249 /* Check whether the next schedule row of the given node needs to be
2250 * non-trivial. Lower-dimensional domains may have some trivial rows,
2251 * but as soon as the number of remaining required non-trivial rows
2252 * is as large as the number or remaining rows to be computed,
2253 * all remaining rows need to be non-trivial.
2255 static int needs_row(struct isl_sched_graph
*graph
, struct isl_sched_node
*node
)
2257 return node
->nvar
- node
->rank
>= graph
->maxvar
- graph
->n_row
;
2260 /* Solve the ILP problem constructed in setup_lp.
2261 * For each node such that all the remaining rows of its schedule
2262 * need to be non-trivial, we construct a non-triviality region.
2263 * This region imposes that the next row is independent of previous rows.
2264 * In particular the coefficients c_i_x are represented by t_i_x
2265 * variables with c_i_x = Q t_i_x and Q a unimodular matrix such that
2266 * its first columns span the rows of the previously computed part
2267 * of the schedule. The non-triviality region enforces that at least
2268 * one of the remaining components of t_i_x is non-zero, i.e.,
2269 * that the new schedule row depends on at least one of the remaining
2272 static __isl_give isl_vec
*solve_lp(struct isl_sched_graph
*graph
)
2278 for (i
= 0; i
< graph
->n
; ++i
) {
2279 struct isl_sched_node
*node
= &graph
->node
[i
];
2280 int skip
= node
->rank
;
2281 graph
->region
[i
].pos
= node
->start
+ 1 + 2*(node
->nparam
+skip
);
2282 if (needs_row(graph
, node
))
2283 graph
->region
[i
].len
= 2 * (node
->nvar
- skip
);
2285 graph
->region
[i
].len
= 0;
2287 lp
= isl_basic_set_copy(graph
->lp
);
2288 sol
= isl_tab_basic_set_non_trivial_lexmin(lp
, 2, graph
->n
,
2289 graph
->region
, &check_conflict
, graph
);
2293 /* Update the schedules of all nodes based on the given solution
2294 * of the LP problem.
2295 * The new row is added to the current band.
2296 * All possibly negative coefficients are encoded as a difference
2297 * of two non-negative variables, so we need to perform the subtraction
2298 * here. Moreover, if use_cmap is set, then the solution does
2299 * not refer to the actual coefficients c_i_x, but instead to variables
2300 * t_i_x such that c_i_x = Q t_i_x and Q is equal to node->cmap.
2301 * In this case, we then also need to perform this multiplication
2302 * to obtain the values of c_i_x.
2304 * If coincident is set, then the caller guarantees that the new
2305 * row satisfies the coincidence constraints.
2307 static int update_schedule(struct isl_sched_graph
*graph
,
2308 __isl_take isl_vec
*sol
, int use_cmap
, int coincident
)
2311 isl_vec
*csol
= NULL
;
2316 isl_die(sol
->ctx
, isl_error_internal
,
2317 "no solution found", goto error
);
2318 if (graph
->n_total_row
>= graph
->max_row
)
2319 isl_die(sol
->ctx
, isl_error_internal
,
2320 "too many schedule rows", goto error
);
2322 for (i
= 0; i
< graph
->n
; ++i
) {
2323 struct isl_sched_node
*node
= &graph
->node
[i
];
2324 int pos
= node
->start
;
2325 int row
= isl_mat_rows(node
->sched
);
2328 csol
= isl_vec_alloc(sol
->ctx
, node
->nvar
);
2332 isl_map_free(node
->sched_map
);
2333 node
->sched_map
= NULL
;
2334 node
->sched
= isl_mat_add_rows(node
->sched
, 1);
2337 node
->sched
= isl_mat_set_element(node
->sched
, row
, 0,
2339 for (j
= 0; j
< node
->nparam
+ node
->nvar
; ++j
)
2340 isl_int_sub(sol
->el
[1 + pos
+ 1 + 2 * j
+ 1],
2341 sol
->el
[1 + pos
+ 1 + 2 * j
+ 1],
2342 sol
->el
[1 + pos
+ 1 + 2 * j
]);
2343 for (j
= 0; j
< node
->nparam
; ++j
)
2344 node
->sched
= isl_mat_set_element(node
->sched
,
2345 row
, 1 + j
, sol
->el
[1+pos
+1+2*j
+1]);
2346 for (j
= 0; j
< node
->nvar
; ++j
)
2347 isl_int_set(csol
->el
[j
],
2348 sol
->el
[1+pos
+1+2*(node
->nparam
+j
)+1]);
2350 csol
= isl_mat_vec_product(isl_mat_copy(node
->cmap
),
2354 for (j
= 0; j
< node
->nvar
; ++j
)
2355 node
->sched
= isl_mat_set_element(node
->sched
,
2356 row
, 1 + node
->nparam
+ j
, csol
->el
[j
]);
2357 node
->coincident
[graph
->n_total_row
] = coincident
;
2363 graph
->n_total_row
++;
2372 /* Convert row "row" of node->sched into an isl_aff living in "ls"
2373 * and return this isl_aff.
2375 static __isl_give isl_aff
*extract_schedule_row(__isl_take isl_local_space
*ls
,
2376 struct isl_sched_node
*node
, int row
)
2384 aff
= isl_aff_zero_on_domain(ls
);
2385 isl_mat_get_element(node
->sched
, row
, 0, &v
);
2386 aff
= isl_aff_set_constant(aff
, v
);
2387 for (j
= 0; j
< node
->nparam
; ++j
) {
2388 isl_mat_get_element(node
->sched
, row
, 1 + j
, &v
);
2389 aff
= isl_aff_set_coefficient(aff
, isl_dim_param
, j
, v
);
2391 for (j
= 0; j
< node
->nvar
; ++j
) {
2392 isl_mat_get_element(node
->sched
, row
, 1 + node
->nparam
+ j
, &v
);
2393 aff
= isl_aff_set_coefficient(aff
, isl_dim_in
, j
, v
);
2401 /* Convert the "n" rows starting at "first" of node->sched into a multi_aff
2402 * and return this multi_aff.
2404 * The result is defined over the uncompressed node domain.
2406 static __isl_give isl_multi_aff
*node_extract_partial_schedule_multi_aff(
2407 struct isl_sched_node
*node
, int first
, int n
)
2411 isl_local_space
*ls
;
2416 nrow
= isl_mat_rows(node
->sched
);
2417 if (node
->compressed
)
2418 space
= isl_multi_aff_get_domain_space(node
->decompress
);
2420 space
= isl_space_copy(node
->space
);
2421 ls
= isl_local_space_from_space(isl_space_copy(space
));
2422 space
= isl_space_from_domain(space
);
2423 space
= isl_space_add_dims(space
, isl_dim_out
, n
);
2424 ma
= isl_multi_aff_zero(space
);
2426 for (i
= first
; i
< first
+ n
; ++i
) {
2427 aff
= extract_schedule_row(isl_local_space_copy(ls
), node
, i
);
2428 ma
= isl_multi_aff_set_aff(ma
, i
- first
, aff
);
2431 isl_local_space_free(ls
);
2433 if (node
->compressed
)
2434 ma
= isl_multi_aff_pullback_multi_aff(ma
,
2435 isl_multi_aff_copy(node
->compress
));
2440 /* Convert node->sched into a multi_aff and return this multi_aff.
2442 * The result is defined over the uncompressed node domain.
2444 static __isl_give isl_multi_aff
*node_extract_schedule_multi_aff(
2445 struct isl_sched_node
*node
)
2449 nrow
= isl_mat_rows(node
->sched
);
2450 return node_extract_partial_schedule_multi_aff(node
, 0, nrow
);
2453 /* Convert node->sched into a map and return this map.
2455 * The result is cached in node->sched_map, which needs to be released
2456 * whenever node->sched is updated.
2457 * It is defined over the uncompressed node domain.
2459 static __isl_give isl_map
*node_extract_schedule(struct isl_sched_node
*node
)
2461 if (!node
->sched_map
) {
2464 ma
= node_extract_schedule_multi_aff(node
);
2465 node
->sched_map
= isl_map_from_multi_aff(ma
);
2468 return isl_map_copy(node
->sched_map
);
2471 /* Construct a map that can be used to update a dependence relation
2472 * based on the current schedule.
2473 * That is, construct a map expressing that source and sink
2474 * are executed within the same iteration of the current schedule.
2475 * This map can then be intersected with the dependence relation.
2476 * This is not the most efficient way, but this shouldn't be a critical
2479 static __isl_give isl_map
*specializer(struct isl_sched_node
*src
,
2480 struct isl_sched_node
*dst
)
2482 isl_map
*src_sched
, *dst_sched
;
2484 src_sched
= node_extract_schedule(src
);
2485 dst_sched
= node_extract_schedule(dst
);
2486 return isl_map_apply_range(src_sched
, isl_map_reverse(dst_sched
));
2489 /* Intersect the domains of the nested relations in domain and range
2490 * of "umap" with "map".
2492 static __isl_give isl_union_map
*intersect_domains(
2493 __isl_take isl_union_map
*umap
, __isl_keep isl_map
*map
)
2495 isl_union_set
*uset
;
2497 umap
= isl_union_map_zip(umap
);
2498 uset
= isl_union_set_from_set(isl_map_wrap(isl_map_copy(map
)));
2499 umap
= isl_union_map_intersect_domain(umap
, uset
);
2500 umap
= isl_union_map_zip(umap
);
2504 /* Update the dependence relation of the given edge based
2505 * on the current schedule.
2506 * If the dependence is carried completely by the current schedule, then
2507 * it is removed from the edge_tables. It is kept in the list of edges
2508 * as otherwise all edge_tables would have to be recomputed.
2510 static int update_edge(struct isl_sched_graph
*graph
,
2511 struct isl_sched_edge
*edge
)
2516 id
= specializer(edge
->src
, edge
->dst
);
2517 edge
->map
= isl_map_intersect(edge
->map
, isl_map_copy(id
));
2521 if (edge
->tagged_condition
) {
2522 edge
->tagged_condition
=
2523 intersect_domains(edge
->tagged_condition
, id
);
2524 if (!edge
->tagged_condition
)
2527 if (edge
->tagged_validity
) {
2528 edge
->tagged_validity
=
2529 intersect_domains(edge
->tagged_validity
, id
);
2530 if (!edge
->tagged_validity
)
2534 empty
= isl_map_plain_is_empty(edge
->map
);
2538 graph_remove_edge(graph
, edge
);
2547 /* Does the domain of "umap" intersect "uset"?
2549 static int domain_intersects(__isl_keep isl_union_map
*umap
,
2550 __isl_keep isl_union_set
*uset
)
2554 umap
= isl_union_map_copy(umap
);
2555 umap
= isl_union_map_intersect_domain(umap
, isl_union_set_copy(uset
));
2556 empty
= isl_union_map_is_empty(umap
);
2557 isl_union_map_free(umap
);
2559 return empty
< 0 ? -1 : !empty
;
2562 /* Does the range of "umap" intersect "uset"?
2564 static int range_intersects(__isl_keep isl_union_map
*umap
,
2565 __isl_keep isl_union_set
*uset
)
2569 umap
= isl_union_map_copy(umap
);
2570 umap
= isl_union_map_intersect_range(umap
, isl_union_set_copy(uset
));
2571 empty
= isl_union_map_is_empty(umap
);
2572 isl_union_map_free(umap
);
2574 return empty
< 0 ? -1 : !empty
;
2577 /* Are the condition dependences of "edge" local with respect to
2578 * the current schedule?
2580 * That is, are domain and range of the condition dependences mapped
2581 * to the same point?
2583 * In other words, is the condition false?
2585 static int is_condition_false(struct isl_sched_edge
*edge
)
2587 isl_union_map
*umap
;
2588 isl_map
*map
, *sched
, *test
;
2591 empty
= isl_union_map_is_empty(edge
->tagged_condition
);
2592 if (empty
< 0 || empty
)
2595 umap
= isl_union_map_copy(edge
->tagged_condition
);
2596 umap
= isl_union_map_zip(umap
);
2597 umap
= isl_union_set_unwrap(isl_union_map_domain(umap
));
2598 map
= isl_map_from_union_map(umap
);
2600 sched
= node_extract_schedule(edge
->src
);
2601 map
= isl_map_apply_domain(map
, sched
);
2602 sched
= node_extract_schedule(edge
->dst
);
2603 map
= isl_map_apply_range(map
, sched
);
2605 test
= isl_map_identity(isl_map_get_space(map
));
2606 local
= isl_map_is_subset(map
, test
);
2613 /* For each conditional validity constraint that is adjacent
2614 * to a condition with domain in condition_source or range in condition_sink,
2615 * turn it into an unconditional validity constraint.
2617 static int unconditionalize_adjacent_validity(struct isl_sched_graph
*graph
,
2618 __isl_take isl_union_set
*condition_source
,
2619 __isl_take isl_union_set
*condition_sink
)
2623 condition_source
= isl_union_set_coalesce(condition_source
);
2624 condition_sink
= isl_union_set_coalesce(condition_sink
);
2626 for (i
= 0; i
< graph
->n_edge
; ++i
) {
2628 isl_union_map
*validity
;
2630 if (!graph
->edge
[i
].conditional_validity
)
2632 if (graph
->edge
[i
].validity
)
2635 validity
= graph
->edge
[i
].tagged_validity
;
2636 adjacent
= domain_intersects(validity
, condition_sink
);
2637 if (adjacent
>= 0 && !adjacent
)
2638 adjacent
= range_intersects(validity
, condition_source
);
2644 graph
->edge
[i
].validity
= 1;
2647 isl_union_set_free(condition_source
);
2648 isl_union_set_free(condition_sink
);
2651 isl_union_set_free(condition_source
);
2652 isl_union_set_free(condition_sink
);
2656 /* Update the dependence relations of all edges based on the current schedule
2657 * and enforce conditional validity constraints that are adjacent
2658 * to satisfied condition constraints.
2660 * First check if any of the condition constraints are satisfied
2661 * (i.e., not local to the outer schedule) and keep track of
2662 * their domain and range.
2663 * Then update all dependence relations (which removes the non-local
2665 * Finally, if any condition constraints turned out to be satisfied,
2666 * then turn all adjacent conditional validity constraints into
2667 * unconditional validity constraints.
2669 static int update_edges(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
2673 isl_union_set
*source
, *sink
;
2675 source
= isl_union_set_empty(isl_space_params_alloc(ctx
, 0));
2676 sink
= isl_union_set_empty(isl_space_params_alloc(ctx
, 0));
2677 for (i
= 0; i
< graph
->n_edge
; ++i
) {
2679 isl_union_set
*uset
;
2680 isl_union_map
*umap
;
2682 if (!graph
->edge
[i
].condition
)
2684 if (graph
->edge
[i
].local
)
2686 local
= is_condition_false(&graph
->edge
[i
]);
2694 umap
= isl_union_map_copy(graph
->edge
[i
].tagged_condition
);
2695 uset
= isl_union_map_domain(umap
);
2696 source
= isl_union_set_union(source
, uset
);
2698 umap
= isl_union_map_copy(graph
->edge
[i
].tagged_condition
);
2699 uset
= isl_union_map_range(umap
);
2700 sink
= isl_union_set_union(sink
, uset
);
2703 for (i
= graph
->n_edge
- 1; i
>= 0; --i
) {
2704 if (update_edge(graph
, &graph
->edge
[i
]) < 0)
2709 return unconditionalize_adjacent_validity(graph
, source
, sink
);
2711 isl_union_set_free(source
);
2712 isl_union_set_free(sink
);
2715 isl_union_set_free(source
);
2716 isl_union_set_free(sink
);
2720 static void next_band(struct isl_sched_graph
*graph
)
2722 graph
->band_start
= graph
->n_total_row
;
2725 /* Return the union of the universe domains of the nodes in "graph"
2726 * that satisfy "pred".
2728 static __isl_give isl_union_set
*isl_sched_graph_domain(isl_ctx
*ctx
,
2729 struct isl_sched_graph
*graph
,
2730 int (*pred
)(struct isl_sched_node
*node
, int data
), int data
)
2736 for (i
= 0; i
< graph
->n
; ++i
)
2737 if (pred(&graph
->node
[i
], data
))
2741 isl_die(ctx
, isl_error_internal
,
2742 "empty component", return NULL
);
2744 set
= isl_set_universe(isl_space_copy(graph
->node
[i
].space
));
2745 dom
= isl_union_set_from_set(set
);
2747 for (i
= i
+ 1; i
< graph
->n
; ++i
) {
2748 if (!pred(&graph
->node
[i
], data
))
2750 set
= isl_set_universe(isl_space_copy(graph
->node
[i
].space
));
2751 dom
= isl_union_set_union(dom
, isl_union_set_from_set(set
));
2757 /* Return a list of unions of universe domains, where each element
2758 * in the list corresponds to an SCC (or WCC) indexed by node->scc.
2760 static __isl_give isl_union_set_list
*extract_sccs(isl_ctx
*ctx
,
2761 struct isl_sched_graph
*graph
)
2764 isl_union_set_list
*filters
;
2766 filters
= isl_union_set_list_alloc(ctx
, graph
->scc
);
2767 for (i
= 0; i
< graph
->scc
; ++i
) {
2770 dom
= isl_sched_graph_domain(ctx
, graph
, &node_scc_exactly
, i
);
2771 filters
= isl_union_set_list_add(filters
, dom
);
2777 /* Return a list of two unions of universe domains, one for the SCCs up
2778 * to and including graph->src_scc and another for the other SCCS.
2780 static __isl_give isl_union_set_list
*extract_split(isl_ctx
*ctx
,
2781 struct isl_sched_graph
*graph
)
2784 isl_union_set_list
*filters
;
2786 filters
= isl_union_set_list_alloc(ctx
, 2);
2787 dom
= isl_sched_graph_domain(ctx
, graph
,
2788 &node_scc_at_most
, graph
->src_scc
);
2789 filters
= isl_union_set_list_add(filters
, dom
);
2790 dom
= isl_sched_graph_domain(ctx
, graph
,
2791 &node_scc_at_least
, graph
->src_scc
+ 1);
2792 filters
= isl_union_set_list_add(filters
, dom
);
2797 /* Topologically sort statements mapped to the same schedule iteration
2798 * and add insert a sequence node in front of "node"
2799 * corresponding to this order.
2801 static __isl_give isl_schedule_node
*sort_statements(
2802 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
)
2805 isl_union_set_list
*filters
;
2810 ctx
= isl_schedule_node_get_ctx(node
);
2812 isl_die(ctx
, isl_error_internal
,
2813 "graph should have at least one node",
2814 return isl_schedule_node_free(node
));
2819 if (update_edges(ctx
, graph
) < 0)
2820 return isl_schedule_node_free(node
);
2822 if (graph
->n_edge
== 0)
2825 if (detect_sccs(ctx
, graph
) < 0)
2826 return isl_schedule_node_free(node
);
2828 filters
= extract_sccs(ctx
, graph
);
2829 node
= isl_schedule_node_insert_sequence(node
, filters
);
2834 /* Copy nodes that satisfy node_pred from the src dependence graph
2835 * to the dst dependence graph.
2837 static int copy_nodes(struct isl_sched_graph
*dst
, struct isl_sched_graph
*src
,
2838 int (*node_pred
)(struct isl_sched_node
*node
, int data
), int data
)
2843 for (i
= 0; i
< src
->n
; ++i
) {
2846 if (!node_pred(&src
->node
[i
], data
))
2850 dst
->node
[j
].space
= isl_space_copy(src
->node
[i
].space
);
2851 dst
->node
[j
].compressed
= src
->node
[i
].compressed
;
2852 dst
->node
[j
].hull
= isl_set_copy(src
->node
[i
].hull
);
2853 dst
->node
[j
].compress
=
2854 isl_multi_aff_copy(src
->node
[i
].compress
);
2855 dst
->node
[j
].decompress
=
2856 isl_multi_aff_copy(src
->node
[i
].decompress
);
2857 dst
->node
[j
].nvar
= src
->node
[i
].nvar
;
2858 dst
->node
[j
].nparam
= src
->node
[i
].nparam
;
2859 dst
->node
[j
].sched
= isl_mat_copy(src
->node
[i
].sched
);
2860 dst
->node
[j
].sched_map
= isl_map_copy(src
->node
[i
].sched_map
);
2861 dst
->node
[j
].coincident
= src
->node
[i
].coincident
;
2864 if (!dst
->node
[j
].space
|| !dst
->node
[j
].sched
)
2866 if (dst
->node
[j
].compressed
&&
2867 (!dst
->node
[j
].hull
|| !dst
->node
[j
].compress
||
2868 !dst
->node
[j
].decompress
))
2875 /* Copy non-empty edges that satisfy edge_pred from the src dependence graph
2876 * to the dst dependence graph.
2877 * If the source or destination node of the edge is not in the destination
2878 * graph, then it must be a backward proximity edge and it should simply
2881 static int copy_edges(isl_ctx
*ctx
, struct isl_sched_graph
*dst
,
2882 struct isl_sched_graph
*src
,
2883 int (*edge_pred
)(struct isl_sched_edge
*edge
, int data
), int data
)
2886 enum isl_edge_type t
;
2889 for (i
= 0; i
< src
->n_edge
; ++i
) {
2890 struct isl_sched_edge
*edge
= &src
->edge
[i
];
2892 isl_union_map
*tagged_condition
;
2893 isl_union_map
*tagged_validity
;
2894 struct isl_sched_node
*dst_src
, *dst_dst
;
2896 if (!edge_pred(edge
, data
))
2899 if (isl_map_plain_is_empty(edge
->map
))
2902 dst_src
= graph_find_node(ctx
, dst
, edge
->src
->space
);
2903 dst_dst
= graph_find_node(ctx
, dst
, edge
->dst
->space
);
2904 if (!dst_src
|| !dst_dst
) {
2905 if (edge
->validity
|| edge
->conditional_validity
)
2906 isl_die(ctx
, isl_error_internal
,
2907 "backward (conditional) validity edge",
2912 map
= isl_map_copy(edge
->map
);
2913 tagged_condition
= isl_union_map_copy(edge
->tagged_condition
);
2914 tagged_validity
= isl_union_map_copy(edge
->tagged_validity
);
2916 dst
->edge
[dst
->n_edge
].src
= dst_src
;
2917 dst
->edge
[dst
->n_edge
].dst
= dst_dst
;
2918 dst
->edge
[dst
->n_edge
].map
= map
;
2919 dst
->edge
[dst
->n_edge
].tagged_condition
= tagged_condition
;
2920 dst
->edge
[dst
->n_edge
].tagged_validity
= tagged_validity
;
2921 dst
->edge
[dst
->n_edge
].validity
= edge
->validity
;
2922 dst
->edge
[dst
->n_edge
].proximity
= edge
->proximity
;
2923 dst
->edge
[dst
->n_edge
].coincidence
= edge
->coincidence
;
2924 dst
->edge
[dst
->n_edge
].condition
= edge
->condition
;
2925 dst
->edge
[dst
->n_edge
].conditional_validity
=
2926 edge
->conditional_validity
;
2929 if (edge
->tagged_condition
&& !tagged_condition
)
2931 if (edge
->tagged_validity
&& !tagged_validity
)
2934 for (t
= isl_edge_first
; t
<= isl_edge_last
; ++t
) {
2936 graph_find_edge(src
, t
, edge
->src
, edge
->dst
))
2938 if (graph_edge_table_add(ctx
, dst
, t
,
2939 &dst
->edge
[dst
->n_edge
- 1]) < 0)
2947 /* Compute the maximal number of variables over all nodes.
2948 * This is the maximal number of linearly independent schedule
2949 * rows that we need to compute.
2950 * Just in case we end up in a part of the dependence graph
2951 * with only lower-dimensional domains, we make sure we will
2952 * compute the required amount of extra linearly independent rows.
2954 static int compute_maxvar(struct isl_sched_graph
*graph
)
2959 for (i
= 0; i
< graph
->n
; ++i
) {
2960 struct isl_sched_node
*node
= &graph
->node
[i
];
2963 if (node_update_cmap(node
) < 0)
2965 nvar
= node
->nvar
+ graph
->n_row
- node
->rank
;
2966 if (nvar
> graph
->maxvar
)
2967 graph
->maxvar
= nvar
;
2973 static __isl_give isl_schedule_node
*compute_schedule(isl_schedule_node
*node
,
2974 struct isl_sched_graph
*graph
);
2975 static __isl_give isl_schedule_node
*compute_schedule_wcc(
2976 isl_schedule_node
*node
, struct isl_sched_graph
*graph
);
2978 /* Compute a schedule for a subgraph of "graph". In particular, for
2979 * the graph composed of nodes that satisfy node_pred and edges that
2980 * that satisfy edge_pred. The caller should precompute the number
2981 * of nodes and edges that satisfy these predicates and pass them along
2982 * as "n" and "n_edge".
2983 * If the subgraph is known to consist of a single component, then wcc should
2984 * be set and then we call compute_schedule_wcc on the constructed subgraph.
2985 * Otherwise, we call compute_schedule, which will check whether the subgraph
2988 * The schedule is inserted at "node" and the updated schedule node
2991 static __isl_give isl_schedule_node
*compute_sub_schedule(
2992 __isl_take isl_schedule_node
*node
, isl_ctx
*ctx
,
2993 struct isl_sched_graph
*graph
, int n
, int n_edge
,
2994 int (*node_pred
)(struct isl_sched_node
*node
, int data
),
2995 int (*edge_pred
)(struct isl_sched_edge
*edge
, int data
),
2998 struct isl_sched_graph split
= { 0 };
3001 if (graph_alloc(ctx
, &split
, n
, n_edge
) < 0)
3003 if (copy_nodes(&split
, graph
, node_pred
, data
) < 0)
3005 if (graph_init_table(ctx
, &split
) < 0)
3007 for (t
= 0; t
<= isl_edge_last
; ++t
)
3008 split
.max_edge
[t
] = graph
->max_edge
[t
];
3009 if (graph_init_edge_tables(ctx
, &split
) < 0)
3011 if (copy_edges(ctx
, &split
, graph
, edge_pred
, data
) < 0)
3013 split
.n_row
= graph
->n_row
;
3014 split
.max_row
= graph
->max_row
;
3015 split
.n_total_row
= graph
->n_total_row
;
3016 split
.band_start
= graph
->band_start
;
3019 node
= compute_schedule_wcc(node
, &split
);
3021 node
= compute_schedule(node
, &split
);
3023 graph_free(ctx
, &split
);
3026 graph_free(ctx
, &split
);
3027 return isl_schedule_node_free(node
);
3030 static int edge_scc_exactly(struct isl_sched_edge
*edge
, int scc
)
3032 return edge
->src
->scc
== scc
&& edge
->dst
->scc
== scc
;
3035 static int edge_dst_scc_at_most(struct isl_sched_edge
*edge
, int scc
)
3037 return edge
->dst
->scc
<= scc
;
3040 static int edge_src_scc_at_least(struct isl_sched_edge
*edge
, int scc
)
3042 return edge
->src
->scc
>= scc
;
3045 /* Reset the current band by dropping all its schedule rows.
3047 static int reset_band(struct isl_sched_graph
*graph
)
3052 drop
= graph
->n_total_row
- graph
->band_start
;
3053 graph
->n_total_row
-= drop
;
3054 graph
->n_row
-= drop
;
3056 for (i
= 0; i
< graph
->n
; ++i
) {
3057 struct isl_sched_node
*node
= &graph
->node
[i
];
3059 isl_map_free(node
->sched_map
);
3060 node
->sched_map
= NULL
;
3062 node
->sched
= isl_mat_drop_rows(node
->sched
,
3063 graph
->band_start
, drop
);
3072 /* Split the current graph into two parts and compute a schedule for each
3073 * part individually. In particular, one part consists of all SCCs up
3074 * to and including graph->src_scc, while the other part contains the other
3075 * SCCS. The split is enforced by a sequence node inserted at position "node"
3076 * in the schedule tree. Return the updated schedule node.
3078 * The current band is reset. It would be possible to reuse
3079 * the previously computed rows as the first rows in the next
3080 * band, but recomputing them may result in better rows as we are looking
3081 * at a smaller part of the dependence graph.
3083 static __isl_give isl_schedule_node
*compute_split_schedule(
3084 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
)
3089 isl_union_set_list
*filters
;
3094 if (reset_band(graph
) < 0)
3095 return isl_schedule_node_free(node
);
3098 for (i
= 0; i
< graph
->n
; ++i
) {
3099 struct isl_sched_node
*node
= &graph
->node
[i
];
3100 int before
= node
->scc
<= graph
->src_scc
;
3107 for (i
= 0; i
< graph
->n_edge
; ++i
) {
3108 if (graph
->edge
[i
].dst
->scc
<= graph
->src_scc
)
3110 if (graph
->edge
[i
].src
->scc
> graph
->src_scc
)
3116 ctx
= isl_schedule_node_get_ctx(node
);
3117 filters
= extract_split(ctx
, graph
);
3118 node
= isl_schedule_node_insert_sequence(node
, filters
);
3119 node
= isl_schedule_node_child(node
, 0);
3120 node
= isl_schedule_node_child(node
, 0);
3122 orig_total_row
= graph
->n_total_row
;
3123 node
= compute_sub_schedule(node
, ctx
, graph
, n
, e1
,
3124 &node_scc_at_most
, &edge_dst_scc_at_most
,
3126 node
= isl_schedule_node_parent(node
);
3127 node
= isl_schedule_node_next_sibling(node
);
3128 node
= isl_schedule_node_child(node
, 0);
3129 graph
->n_total_row
= orig_total_row
;
3130 node
= compute_sub_schedule(node
, ctx
, graph
, graph
->n
- n
, e2
,
3131 &node_scc_at_least
, &edge_src_scc_at_least
,
3132 graph
->src_scc
+ 1, 0);
3133 node
= isl_schedule_node_parent(node
);
3134 node
= isl_schedule_node_parent(node
);
3139 /* Insert a band node at position "node" in the schedule tree corresponding
3140 * to the current band in "graph". Mark the band node permutable
3141 * if "permutable" is set.
3142 * The partial schedules and the coincidence property are extracted
3143 * from the graph nodes.
3144 * Return the updated schedule node.
3146 static __isl_give isl_schedule_node
*insert_current_band(
3147 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
,
3153 isl_multi_pw_aff
*mpa
;
3154 isl_multi_union_pw_aff
*mupa
;
3160 isl_die(isl_schedule_node_get_ctx(node
), isl_error_internal
,
3161 "graph should have at least one node",
3162 return isl_schedule_node_free(node
));
3164 start
= graph
->band_start
;
3165 end
= graph
->n_total_row
;
3168 ma
= node_extract_partial_schedule_multi_aff(&graph
->node
[0], start
, n
);
3169 mpa
= isl_multi_pw_aff_from_multi_aff(ma
);
3170 mupa
= isl_multi_union_pw_aff_from_multi_pw_aff(mpa
);
3172 for (i
= 1; i
< graph
->n
; ++i
) {
3173 isl_multi_union_pw_aff
*mupa_i
;
3175 ma
= node_extract_partial_schedule_multi_aff(&graph
->node
[i
],
3177 mpa
= isl_multi_pw_aff_from_multi_aff(ma
);
3178 mupa_i
= isl_multi_union_pw_aff_from_multi_pw_aff(mpa
);
3179 mupa
= isl_multi_union_pw_aff_union_add(mupa
, mupa_i
);
3181 node
= isl_schedule_node_insert_partial_schedule(node
, mupa
);
3183 for (i
= 0; i
< n
; ++i
)
3184 node
= isl_schedule_node_band_member_set_coincident(node
, i
,
3185 graph
->node
[0].coincident
[start
+ i
]);
3186 node
= isl_schedule_node_band_set_permutable(node
, permutable
);
3191 /* Update the dependence relations based on the current schedule,
3192 * add the current band to "node" and the continue with the computation
3194 * Return the updated schedule node.
3196 static __isl_give isl_schedule_node
*compute_next_band(
3197 __isl_take isl_schedule_node
*node
,
3198 struct isl_sched_graph
*graph
, int permutable
)
3205 ctx
= isl_schedule_node_get_ctx(node
);
3206 if (update_edges(ctx
, graph
) < 0)
3207 return isl_schedule_node_free(node
);
3208 node
= insert_current_band(node
, graph
, permutable
);
3211 node
= isl_schedule_node_child(node
, 0);
3212 node
= compute_schedule(node
, graph
);
3213 node
= isl_schedule_node_parent(node
);
3218 /* Add constraints to graph->lp that force the dependence "map" (which
3219 * is part of the dependence relation of "edge")
3220 * to be respected and attempt to carry it, where the edge is one from
3221 * a node j to itself. "pos" is the sequence number of the given map.
3222 * That is, add constraints that enforce
3224 * (c_j_0 + c_j_n n + c_j_x y) - (c_j_0 + c_j_n n + c_j_x x)
3225 * = c_j_x (y - x) >= e_i
3227 * for each (x,y) in R.
3228 * We obtain general constraints on coefficients (c_0, c_n, c_x)
3229 * of valid constraints for (y - x) and then plug in (-e_i, 0, c_j_x),
3230 * with each coefficient in c_j_x represented as a pair of non-negative
3233 static int add_intra_constraints(struct isl_sched_graph
*graph
,
3234 struct isl_sched_edge
*edge
, __isl_take isl_map
*map
, int pos
)
3237 isl_ctx
*ctx
= isl_map_get_ctx(map
);
3239 isl_dim_map
*dim_map
;
3240 isl_basic_set
*coef
;
3241 struct isl_sched_node
*node
= edge
->src
;
3243 coef
= intra_coefficients(graph
, node
, map
);
3247 dim
= isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef
)));
3249 total
= isl_basic_set_total_dim(graph
->lp
);
3250 dim_map
= isl_dim_map_alloc(ctx
, total
);
3251 isl_dim_map_range(dim_map
, 3 + pos
, 0, 0, 0, 1, -1);
3252 isl_dim_map_range(dim_map
, node
->start
+ 2 * node
->nparam
+ 1, 2,
3253 isl_space_dim(dim
, isl_dim_set
), 1,
3255 isl_dim_map_range(dim_map
, node
->start
+ 2 * node
->nparam
+ 2, 2,
3256 isl_space_dim(dim
, isl_dim_set
), 1,
3258 graph
->lp
= isl_basic_set_extend_constraints(graph
->lp
,
3259 coef
->n_eq
, coef
->n_ineq
);
3260 graph
->lp
= isl_basic_set_add_constraints_dim_map(graph
->lp
,
3262 isl_space_free(dim
);
3267 /* Add constraints to graph->lp that force the dependence "map" (which
3268 * is part of the dependence relation of "edge")
3269 * to be respected and attempt to carry it, where the edge is one from
3270 * node j to node k. "pos" is the sequence number of the given map.
3271 * That is, add constraints that enforce
3273 * (c_k_0 + c_k_n n + c_k_x y) - (c_j_0 + c_j_n n + c_j_x x) >= e_i
3275 * for each (x,y) in R.
3276 * We obtain general constraints on coefficients (c_0, c_n, c_x)
3277 * of valid constraints for R and then plug in
3278 * (-e_i + c_k_0 - c_j_0, c_k_n - c_j_n, c_k_x - c_j_x)
3279 * with each coefficient (except e_i, c_k_0 and c_j_0)
3280 * represented as a pair of non-negative coefficients.
3282 static int add_inter_constraints(struct isl_sched_graph
*graph
,
3283 struct isl_sched_edge
*edge
, __isl_take isl_map
*map
, int pos
)
3286 isl_ctx
*ctx
= isl_map_get_ctx(map
);
3288 isl_dim_map
*dim_map
;
3289 isl_basic_set
*coef
;
3290 struct isl_sched_node
*src
= edge
->src
;
3291 struct isl_sched_node
*dst
= edge
->dst
;
3293 coef
= inter_coefficients(graph
, edge
, map
);
3297 dim
= isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef
)));
3299 total
= isl_basic_set_total_dim(graph
->lp
);
3300 dim_map
= isl_dim_map_alloc(ctx
, total
);
3302 isl_dim_map_range(dim_map
, 3 + pos
, 0, 0, 0, 1, -1);
3304 isl_dim_map_range(dim_map
, dst
->start
, 0, 0, 0, 1, 1);
3305 isl_dim_map_range(dim_map
, dst
->start
+ 1, 2, 1, 1, dst
->nparam
, -1);
3306 isl_dim_map_range(dim_map
, dst
->start
+ 2, 2, 1, 1, dst
->nparam
, 1);
3307 isl_dim_map_range(dim_map
, dst
->start
+ 2 * dst
->nparam
+ 1, 2,
3308 isl_space_dim(dim
, isl_dim_set
) + src
->nvar
, 1,
3310 isl_dim_map_range(dim_map
, dst
->start
+ 2 * dst
->nparam
+ 2, 2,
3311 isl_space_dim(dim
, isl_dim_set
) + src
->nvar
, 1,
3314 isl_dim_map_range(dim_map
, src
->start
, 0, 0, 0, 1, -1);
3315 isl_dim_map_range(dim_map
, src
->start
+ 1, 2, 1, 1, src
->nparam
, 1);
3316 isl_dim_map_range(dim_map
, src
->start
+ 2, 2, 1, 1, src
->nparam
, -1);
3317 isl_dim_map_range(dim_map
, src
->start
+ 2 * src
->nparam
+ 1, 2,
3318 isl_space_dim(dim
, isl_dim_set
), 1,
3320 isl_dim_map_range(dim_map
, src
->start
+ 2 * src
->nparam
+ 2, 2,
3321 isl_space_dim(dim
, isl_dim_set
), 1,
3324 graph
->lp
= isl_basic_set_extend_constraints(graph
->lp
,
3325 coef
->n_eq
, coef
->n_ineq
);
3326 graph
->lp
= isl_basic_set_add_constraints_dim_map(graph
->lp
,
3328 isl_space_free(dim
);
3333 /* Add constraints to graph->lp that force all (conditional) validity
3334 * dependences to be respected and attempt to carry them.
3336 static int add_all_constraints(struct isl_sched_graph
*graph
)
3342 for (i
= 0; i
< graph
->n_edge
; ++i
) {
3343 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
3345 if (!edge
->validity
&& !edge
->conditional_validity
)
3348 for (j
= 0; j
< edge
->map
->n
; ++j
) {
3349 isl_basic_map
*bmap
;
3352 bmap
= isl_basic_map_copy(edge
->map
->p
[j
]);
3353 map
= isl_map_from_basic_map(bmap
);
3355 if (edge
->src
== edge
->dst
&&
3356 add_intra_constraints(graph
, edge
, map
, pos
) < 0)
3358 if (edge
->src
!= edge
->dst
&&
3359 add_inter_constraints(graph
, edge
, map
, pos
) < 0)
3368 /* Count the number of equality and inequality constraints
3369 * that will be added to the carry_lp problem.
3370 * We count each edge exactly once.
3372 static int count_all_constraints(struct isl_sched_graph
*graph
,
3373 int *n_eq
, int *n_ineq
)
3377 *n_eq
= *n_ineq
= 0;
3378 for (i
= 0; i
< graph
->n_edge
; ++i
) {
3379 struct isl_sched_edge
*edge
= &graph
->edge
[i
];
3380 for (j
= 0; j
< edge
->map
->n
; ++j
) {
3381 isl_basic_map
*bmap
;
3384 bmap
= isl_basic_map_copy(edge
->map
->p
[j
]);
3385 map
= isl_map_from_basic_map(bmap
);
3387 if (count_map_constraints(graph
, edge
, map
,
3388 n_eq
, n_ineq
, 1, 0) < 0)
3396 /* Construct an LP problem for finding schedule coefficients
3397 * such that the schedule carries as many dependences as possible.
3398 * In particular, for each dependence i, we bound the dependence distance
3399 * from below by e_i, with 0 <= e_i <= 1 and then maximize the sum
3400 * of all e_i's. Dependence with e_i = 0 in the solution are simply
3401 * respected, while those with e_i > 0 (in practice e_i = 1) are carried.
3402 * Note that if the dependence relation is a union of basic maps,
3403 * then we have to consider each basic map individually as it may only
3404 * be possible to carry the dependences expressed by some of those
3405 * basic maps and not all off them.
3406 * Below, we consider each of those basic maps as a separate "edge".
3408 * All variables of the LP are non-negative. The actual coefficients
3409 * may be negative, so each coefficient is represented as the difference
3410 * of two non-negative variables. The negative part always appears
3411 * immediately before the positive part.
3412 * Other than that, the variables have the following order
3414 * - sum of (1 - e_i) over all edges
3415 * - sum of positive and negative parts of all c_n coefficients
3416 * (unconstrained when computing non-parametric schedules)
3417 * - sum of positive and negative parts of all c_x coefficients
3422 * - positive and negative parts of c_i_n (if parametric)
3423 * - positive and negative parts of c_i_x
3425 * The constraints are those from the (validity) edges plus three equalities
3426 * to express the sums and n_edge inequalities to express e_i <= 1.
3428 static int setup_carry_lp(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
3438 for (i
= 0; i
< graph
->n_edge
; ++i
)
3439 n_edge
+= graph
->edge
[i
].map
->n
;
3442 for (i
= 0; i
< graph
->n
; ++i
) {
3443 struct isl_sched_node
*node
= &graph
->node
[graph
->sorted
[i
]];
3444 node
->start
= total
;
3445 total
+= 1 + 2 * (node
->nparam
+ node
->nvar
);
3448 if (count_all_constraints(graph
, &n_eq
, &n_ineq
) < 0)
3450 if (count_bound_coefficient_constraints(ctx
, graph
, &n_eq
, &n_ineq
) < 0)
3453 dim
= isl_space_set_alloc(ctx
, 0, total
);
3454 isl_basic_set_free(graph
->lp
);
3457 graph
->lp
= isl_basic_set_alloc_space(dim
, 0, n_eq
, n_ineq
);
3458 graph
->lp
= isl_basic_set_set_rational(graph
->lp
);
3460 k
= isl_basic_set_alloc_equality(graph
->lp
);
3463 isl_seq_clr(graph
->lp
->eq
[k
], 1 + total
);
3464 isl_int_set_si(graph
->lp
->eq
[k
][0], -n_edge
);
3465 isl_int_set_si(graph
->lp
->eq
[k
][1], 1);
3466 for (i
= 0; i
< n_edge
; ++i
)
3467 isl_int_set_si(graph
->lp
->eq
[k
][4 + i
], 1);
3469 k
= isl_basic_set_alloc_equality(graph
->lp
);
3472 isl_seq_clr(graph
->lp
->eq
[k
], 1 + total
);
3473 isl_int_set_si(graph
->lp
->eq
[k
][2], -1);
3474 for (i
= 0; i
< graph
->n
; ++i
) {
3475 int pos
= 1 + graph
->node
[i
].start
+ 1;
3477 for (j
= 0; j
< 2 * graph
->node
[i
].nparam
; ++j
)
3478 isl_int_set_si(graph
->lp
->eq
[k
][pos
+ j
], 1);
3481 k
= isl_basic_set_alloc_equality(graph
->lp
);
3484 isl_seq_clr(graph
->lp
->eq
[k
], 1 + total
);
3485 isl_int_set_si(graph
->lp
->eq
[k
][3], -1);
3486 for (i
= 0; i
< graph
->n
; ++i
) {
3487 struct isl_sched_node
*node
= &graph
->node
[i
];
3488 int pos
= 1 + node
->start
+ 1 + 2 * node
->nparam
;
3490 for (j
= 0; j
< 2 * node
->nvar
; ++j
)
3491 isl_int_set_si(graph
->lp
->eq
[k
][pos
+ j
], 1);
3494 for (i
= 0; i
< n_edge
; ++i
) {
3495 k
= isl_basic_set_alloc_inequality(graph
->lp
);
3498 isl_seq_clr(graph
->lp
->ineq
[k
], 1 + total
);
3499 isl_int_set_si(graph
->lp
->ineq
[k
][4 + i
], -1);
3500 isl_int_set_si(graph
->lp
->ineq
[k
][0], 1);
3503 if (add_bound_coefficient_constraints(ctx
, graph
) < 0)
3505 if (add_all_constraints(graph
) < 0)
3511 static __isl_give isl_schedule_node
*compute_component_schedule(
3512 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
,
3515 /* Comparison function for sorting the statements based on
3516 * the corresponding value in "r".
3518 static int smaller_value(const void *a
, const void *b
, void *data
)
3524 return isl_int_cmp(r
->el
[*i1
], r
->el
[*i2
]);
3527 /* If the schedule_split_scaled option is set and if the linear
3528 * parts of the scheduling rows for all nodes in the graphs have
3529 * a non-trivial common divisor, then split off the remainder of the
3530 * constant term modulo this common divisor from the linear part.
3531 * Otherwise, insert a band node directly and continue with
3532 * the construction of the schedule.
3534 * If a non-trivial common divisor is found, then
3535 * the linear part is reduced and the remainder is enforced
3536 * by a sequence node with the children placed in the order
3537 * of this remainder.
3538 * In particular, we assign an scc index based on the remainder and
3539 * then rely on compute_component_schedule to insert the sequence and
3540 * to continue the schedule construction on each part.
3542 static __isl_give isl_schedule_node
*split_scaled(
3543 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
)
3556 ctx
= isl_schedule_node_get_ctx(node
);
3557 if (!ctx
->opt
->schedule_split_scaled
)
3558 return compute_next_band(node
, graph
, 0);
3560 return compute_next_band(node
, graph
, 0);
3563 isl_int_init(gcd_i
);
3565 isl_int_set_si(gcd
, 0);
3567 row
= isl_mat_rows(graph
->node
[0].sched
) - 1;
3569 for (i
= 0; i
< graph
->n
; ++i
) {
3570 struct isl_sched_node
*node
= &graph
->node
[i
];
3571 int cols
= isl_mat_cols(node
->sched
);
3573 isl_seq_gcd(node
->sched
->row
[row
] + 1, cols
- 1, &gcd_i
);
3574 isl_int_gcd(gcd
, gcd
, gcd_i
);
3577 isl_int_clear(gcd_i
);
3579 if (isl_int_cmp_si(gcd
, 1) <= 0) {
3581 return compute_next_band(node
, graph
, 0);
3584 r
= isl_vec_alloc(ctx
, graph
->n
);
3585 order
= isl_calloc_array(ctx
, int, graph
->n
);
3589 for (i
= 0; i
< graph
->n
; ++i
) {
3590 struct isl_sched_node
*node
= &graph
->node
[i
];
3593 isl_int_fdiv_r(r
->el
[i
], node
->sched
->row
[row
][0], gcd
);
3594 isl_int_fdiv_q(node
->sched
->row
[row
][0],
3595 node
->sched
->row
[row
][0], gcd
);
3596 isl_int_mul(node
->sched
->row
[row
][0],
3597 node
->sched
->row
[row
][0], gcd
);
3598 node
->sched
= isl_mat_scale_down_row(node
->sched
, row
, gcd
);
3603 if (isl_sort(order
, graph
->n
, sizeof(order
[0]), &smaller_value
, r
) < 0)
3607 for (i
= 0; i
< graph
->n
; ++i
) {
3608 if (i
> 0 && isl_int_ne(r
->el
[order
[i
- 1]], r
->el
[order
[i
]]))
3610 graph
->node
[order
[i
]].scc
= scc
;
3619 if (update_edges(ctx
, graph
) < 0)
3620 return isl_schedule_node_free(node
);
3621 node
= insert_current_band(node
, graph
, 0);
3624 node
= isl_schedule_node_child(node
, 0);
3625 node
= compute_component_schedule(node
, graph
, 0);
3626 node
= isl_schedule_node_parent(node
);
3633 return isl_schedule_node_free(node
);
3636 /* Is the schedule row "sol" trivial on node "node"?
3637 * That is, is the solution zero on the dimensions orthogonal to
3638 * the previously found solutions?
3639 * Return 1 if the solution is trivial, 0 if it is not and -1 on error.
3641 * Each coefficient is represented as the difference between
3642 * two non-negative values in "sol". "sol" has been computed
3643 * in terms of the original iterators (i.e., without use of cmap).
3644 * We construct the schedule row s and write it as a linear
3645 * combination of (linear combinations of) previously computed schedule rows.
3646 * s = Q c or c = U s.
3647 * If the final entries of c are all zero, then the solution is trivial.
3649 static int is_trivial(struct isl_sched_node
*node
, __isl_keep isl_vec
*sol
)
3659 if (node
->nvar
== node
->rank
)
3662 ctx
= isl_vec_get_ctx(sol
);
3663 node_sol
= isl_vec_alloc(ctx
, node
->nvar
);
3667 pos
= 1 + node
->start
+ 1 + 2 * node
->nparam
;
3669 for (i
= 0; i
< node
->nvar
; ++i
)
3670 isl_int_sub(node_sol
->el
[i
],
3671 sol
->el
[pos
+ 2 * i
+ 1], sol
->el
[pos
+ 2 * i
]);
3673 node_sol
= isl_mat_vec_product(isl_mat_copy(node
->cinv
), node_sol
);
3678 trivial
= isl_seq_first_non_zero(node_sol
->el
+ node
->rank
,
3679 node
->nvar
- node
->rank
) == -1;
3681 isl_vec_free(node_sol
);
3686 /* Is the schedule row "sol" trivial on any node where it should
3688 * "sol" has been computed in terms of the original iterators
3689 * (i.e., without use of cmap).
3690 * Return 1 if any solution is trivial, 0 if they are not and -1 on error.
3692 static int is_any_trivial(struct isl_sched_graph
*graph
,
3693 __isl_keep isl_vec
*sol
)
3697 for (i
= 0; i
< graph
->n
; ++i
) {
3698 struct isl_sched_node
*node
= &graph
->node
[i
];
3701 if (!needs_row(graph
, node
))
3703 trivial
= is_trivial(node
, sol
);
3704 if (trivial
< 0 || trivial
)
3711 /* Construct a schedule row for each node such that as many dependences
3712 * as possible are carried and then continue with the next band.
3714 * If the computed schedule row turns out to be trivial on one or
3715 * more nodes where it should not be trivial, then we throw it away
3716 * and try again on each component separately.
3718 * If there is only one component, then we accept the schedule row anyway,
3719 * but we do not consider it as a complete row and therefore do not
3720 * increment graph->n_row. Note that the ranks of the nodes that
3721 * do get a non-trivial schedule part will get updated regardless and
3722 * graph->maxvar is computed based on these ranks. The test for
3723 * whether more schedule rows are required in compute_schedule_wcc
3724 * is therefore not affected.
3726 * Insert a band corresponding to the schedule row at position "node"
3727 * of the schedule tree and continue with the construction of the schedule.
3728 * This insertion and the continued construction is performed by split_scaled
3729 * after optionally checking for non-trivial common divisors.
3731 static __isl_give isl_schedule_node
*carry_dependences(
3732 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
)
3745 for (i
= 0; i
< graph
->n_edge
; ++i
)
3746 n_edge
+= graph
->edge
[i
].map
->n
;
3748 ctx
= isl_schedule_node_get_ctx(node
);
3749 if (setup_carry_lp(ctx
, graph
) < 0)
3750 return isl_schedule_node_free(node
);
3752 lp
= isl_basic_set_copy(graph
->lp
);
3753 sol
= isl_tab_basic_set_non_neg_lexmin(lp
);
3755 return isl_schedule_node_free(node
);
3757 if (sol
->size
== 0) {
3759 isl_die(ctx
, isl_error_internal
,
3760 "error in schedule construction",
3761 return isl_schedule_node_free(node
));
3764 isl_int_divexact(sol
->el
[1], sol
->el
[1], sol
->el
[0]);
3765 if (isl_int_cmp_si(sol
->el
[1], n_edge
) >= 0) {
3767 isl_die(ctx
, isl_error_unknown
,
3768 "unable to carry dependences",
3769 return isl_schedule_node_free(node
));
3772 trivial
= is_any_trivial(graph
, sol
);
3774 sol
= isl_vec_free(sol
);
3775 } else if (trivial
&& graph
->scc
> 1) {
3777 return compute_component_schedule(node
, graph
, 1);
3780 if (update_schedule(graph
, sol
, 0, 0) < 0)
3781 return isl_schedule_node_free(node
);
3785 return split_scaled(node
, graph
);
3788 /* Are there any (non-empty) (conditional) validity edges in the graph?
3790 static int has_validity_edges(struct isl_sched_graph
*graph
)
3794 for (i
= 0; i
< graph
->n_edge
; ++i
) {
3797 empty
= isl_map_plain_is_empty(graph
->edge
[i
].map
);
3802 if (graph
->edge
[i
].validity
||
3803 graph
->edge
[i
].conditional_validity
)
3810 /* Should we apply a Feautrier step?
3811 * That is, did the user request the Feautrier algorithm and are
3812 * there any validity dependences (left)?
3814 static int need_feautrier_step(isl_ctx
*ctx
, struct isl_sched_graph
*graph
)
3816 if (ctx
->opt
->schedule_algorithm
!= ISL_SCHEDULE_ALGORITHM_FEAUTRIER
)
3819 return has_validity_edges(graph
);
3822 /* Compute a schedule for a connected dependence graph using Feautrier's
3823 * multi-dimensional scheduling algorithm and return the updated schedule node.
3825 * The original algorithm is described in [1].
3826 * The main idea is to minimize the number of scheduling dimensions, by
3827 * trying to satisfy as many dependences as possible per scheduling dimension.
3829 * [1] P. Feautrier, Some Efficient Solutions to the Affine Scheduling
3830 * Problem, Part II: Multi-Dimensional Time.
3831 * In Intl. Journal of Parallel Programming, 1992.
3833 static __isl_give isl_schedule_node
*compute_schedule_wcc_feautrier(
3834 isl_schedule_node
*node
, struct isl_sched_graph
*graph
)
3836 return carry_dependences(node
, graph
);
3839 /* Turn off the "local" bit on all (condition) edges.
3841 static void clear_local_edges(struct isl_sched_graph
*graph
)
3845 for (i
= 0; i
< graph
->n_edge
; ++i
)
3846 if (graph
->edge
[i
].condition
)
3847 graph
->edge
[i
].local
= 0;
3850 /* Does "graph" have both condition and conditional validity edges?
3852 static int need_condition_check(struct isl_sched_graph
*graph
)
3855 int any_condition
= 0;
3856 int any_conditional_validity
= 0;
3858 for (i
= 0; i
< graph
->n_edge
; ++i
) {
3859 if (graph
->edge
[i
].condition
)
3861 if (graph
->edge
[i
].conditional_validity
)
3862 any_conditional_validity
= 1;
3865 return any_condition
&& any_conditional_validity
;
3868 /* Does "graph" contain any coincidence edge?
3870 static int has_any_coincidence(struct isl_sched_graph
*graph
)
3874 for (i
= 0; i
< graph
->n_edge
; ++i
)
3875 if (graph
->edge
[i
].coincidence
)
3881 /* Extract the final schedule row as a map with the iteration domain
3882 * of "node" as domain.
3884 static __isl_give isl_map
*final_row(struct isl_sched_node
*node
)
3886 isl_local_space
*ls
;
3890 row
= isl_mat_rows(node
->sched
) - 1;
3891 ls
= isl_local_space_from_space(isl_space_copy(node
->space
));
3892 aff
= extract_schedule_row(ls
, node
, row
);
3893 return isl_map_from_aff(aff
);
3896 /* Is the conditional validity dependence in the edge with index "edge_index"
3897 * violated by the latest (i.e., final) row of the schedule?
3898 * That is, is i scheduled after j
3899 * for any conditional validity dependence i -> j?
3901 static int is_violated(struct isl_sched_graph
*graph
, int edge_index
)
3903 isl_map
*src_sched
, *dst_sched
, *map
;
3904 struct isl_sched_edge
*edge
= &graph
->edge
[edge_index
];
3907 src_sched
= final_row(edge
->src
);
3908 dst_sched
= final_row(edge
->dst
);
3909 map
= isl_map_copy(edge
->map
);
3910 map
= isl_map_apply_domain(map
, src_sched
);
3911 map
= isl_map_apply_range(map
, dst_sched
);
3912 map
= isl_map_order_gt(map
, isl_dim_in
, 0, isl_dim_out
, 0);
3913 empty
= isl_map_is_empty(map
);
3922 /* Does "graph" have any satisfied condition edges that
3923 * are adjacent to the conditional validity constraint with
3924 * domain "conditional_source" and range "conditional_sink"?
3926 * A satisfied condition is one that is not local.
3927 * If a condition was forced to be local already (i.e., marked as local)
3928 * then there is no need to check if it is in fact local.
3930 * Additionally, mark all adjacent condition edges found as local.
3932 static int has_adjacent_true_conditions(struct isl_sched_graph
*graph
,
3933 __isl_keep isl_union_set
*conditional_source
,
3934 __isl_keep isl_union_set
*conditional_sink
)
3939 for (i
= 0; i
< graph
->n_edge
; ++i
) {
3940 int adjacent
, local
;
3941 isl_union_map
*condition
;
3943 if (!graph
->edge
[i
].condition
)
3945 if (graph
->edge
[i
].local
)
3948 condition
= graph
->edge
[i
].tagged_condition
;
3949 adjacent
= domain_intersects(condition
, conditional_sink
);
3950 if (adjacent
>= 0 && !adjacent
)
3951 adjacent
= range_intersects(condition
,
3952 conditional_source
);
3958 graph
->edge
[i
].local
= 1;
3960 local
= is_condition_false(&graph
->edge
[i
]);
3970 /* Are there any violated conditional validity dependences with
3971 * adjacent condition dependences that are not local with respect
3972 * to the current schedule?
3973 * That is, is the conditional validity constraint violated?
3975 * Additionally, mark all those adjacent condition dependences as local.
3976 * We also mark those adjacent condition dependences that were not marked
3977 * as local before, but just happened to be local already. This ensures
3978 * that they remain local if the schedule is recomputed.
3980 * We first collect domain and range of all violated conditional validity
3981 * dependences and then check if there are any adjacent non-local
3982 * condition dependences.
3984 static int has_violated_conditional_constraint(isl_ctx
*ctx
,
3985 struct isl_sched_graph
*graph
)
3989 isl_union_set
*source
, *sink
;
3991 source
= isl_union_set_empty(isl_space_params_alloc(ctx
, 0));
3992 sink
= isl_union_set_empty(isl_space_params_alloc(ctx
, 0));
3993 for (i
= 0; i
< graph
->n_edge
; ++i
) {
3994 isl_union_set
*uset
;
3995 isl_union_map
*umap
;
3998 if (!graph
->edge
[i
].conditional_validity
)
4001 violated
= is_violated(graph
, i
);
4009 umap
= isl_union_map_copy(graph
->edge
[i
].tagged_validity
);
4010 uset
= isl_union_map_domain(umap
);
4011 source
= isl_union_set_union(source
, uset
);
4012 source
= isl_union_set_coalesce(source
);
4014 umap
= isl_union_map_copy(graph
->edge
[i
].tagged_validity
);
4015 uset
= isl_union_map_range(umap
);
4016 sink
= isl_union_set_union(sink
, uset
);
4017 sink
= isl_union_set_coalesce(sink
);
4021 any
= has_adjacent_true_conditions(graph
, source
, sink
);
4023 isl_union_set_free(source
);
4024 isl_union_set_free(sink
);
4027 isl_union_set_free(source
);
4028 isl_union_set_free(sink
);
4032 /* Compute a schedule for a connected dependence graph and return
4033 * the updated schedule node.
4035 * We try to find a sequence of as many schedule rows as possible that result
4036 * in non-negative dependence distances (independent of the previous rows
4037 * in the sequence, i.e., such that the sequence is tilable), with as
4038 * many of the initial rows as possible satisfying the coincidence constraints.
4039 * If we can't find any more rows we either
4040 * - split between SCCs and start over (assuming we found an interesting
4041 * pair of SCCs between which to split)
4042 * - continue with the next band (assuming the current band has at least
4044 * - try to carry as many dependences as possible and continue with the next
4046 * In each case, we first insert a band node in the schedule tree
4047 * if any rows have been computed.
4049 * If Feautrier's algorithm is selected, we first recursively try to satisfy
4050 * as many validity dependences as possible. When all validity dependences
4051 * are satisfied we extend the schedule to a full-dimensional schedule.
4053 * If we manage to complete the schedule, we insert a band node
4054 * (if any schedule rows were computed) and we finish off by topologically
4055 * sorting the statements based on the remaining dependences.
4057 * If ctx->opt->schedule_outer_coincidence is set, then we force the
4058 * outermost dimension to satisfy the coincidence constraints. If this
4059 * turns out to be impossible, we fall back on the general scheme above
4060 * and try to carry as many dependences as possible.
4062 * If "graph" contains both condition and conditional validity dependences,
4063 * then we need to check that that the conditional schedule constraint
4064 * is satisfied, i.e., there are no violated conditional validity dependences
4065 * that are adjacent to any non-local condition dependences.
4066 * If there are, then we mark all those adjacent condition dependences
4067 * as local and recompute the current band. Those dependences that
4068 * are marked local will then be forced to be local.
4069 * The initial computation is performed with no dependences marked as local.
4070 * If we are lucky, then there will be no violated conditional validity
4071 * dependences adjacent to any non-local condition dependences.
4072 * Otherwise, we mark some additional condition dependences as local and
4073 * recompute. We continue this process until there are no violations left or
4074 * until we are no longer able to compute a schedule.
4075 * Since there are only a finite number of dependences,
4076 * there will only be a finite number of iterations.
4078 static __isl_give isl_schedule_node
*compute_schedule_wcc(
4079 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
)
4081 int has_coincidence
;
4082 int use_coincidence
;
4083 int force_coincidence
= 0;
4084 int check_conditional
;
4090 ctx
= isl_schedule_node_get_ctx(node
);
4091 if (detect_sccs(ctx
, graph
) < 0)
4092 return isl_schedule_node_free(node
);
4093 if (sort_sccs(graph
) < 0)
4094 return isl_schedule_node_free(node
);
4096 if (compute_maxvar(graph
) < 0)
4097 return isl_schedule_node_free(node
);
4099 if (need_feautrier_step(ctx
, graph
))
4100 return compute_schedule_wcc_feautrier(node
, graph
);
4102 clear_local_edges(graph
);
4103 check_conditional
= need_condition_check(graph
);
4104 has_coincidence
= has_any_coincidence(graph
);
4106 if (ctx
->opt
->schedule_outer_coincidence
)
4107 force_coincidence
= 1;
4109 use_coincidence
= has_coincidence
;
4110 while (graph
->n_row
< graph
->maxvar
) {
4115 graph
->src_scc
= -1;
4116 graph
->dst_scc
= -1;
4118 if (setup_lp(ctx
, graph
, use_coincidence
) < 0)
4119 return isl_schedule_node_free(node
);
4120 sol
= solve_lp(graph
);
4122 return isl_schedule_node_free(node
);
4123 if (sol
->size
== 0) {
4124 int empty
= graph
->n_total_row
== graph
->band_start
;
4127 if (use_coincidence
&& (!force_coincidence
|| !empty
)) {
4128 use_coincidence
= 0;
4131 if (!ctx
->opt
->schedule_maximize_band_depth
&& !empty
)
4132 return compute_next_band(node
, graph
, 1);
4133 if (graph
->src_scc
>= 0)
4134 return compute_split_schedule(node
, graph
);
4136 return compute_next_band(node
, graph
, 1);
4137 return carry_dependences(node
, graph
);
4139 coincident
= !has_coincidence
|| use_coincidence
;
4140 if (update_schedule(graph
, sol
, 1, coincident
) < 0)
4141 return isl_schedule_node_free(node
);
4143 if (!check_conditional
)
4145 violated
= has_violated_conditional_constraint(ctx
, graph
);
4147 return isl_schedule_node_free(node
);
4150 if (reset_band(graph
) < 0)
4151 return isl_schedule_node_free(node
);
4152 use_coincidence
= has_coincidence
;
4155 if (graph
->n_total_row
> graph
->band_start
) {
4156 node
= insert_current_band(node
, graph
, 1);
4157 node
= isl_schedule_node_child(node
, 0);
4159 node
= sort_statements(node
, graph
);
4160 if (graph
->n_total_row
> graph
->band_start
)
4161 node
= isl_schedule_node_parent(node
);
4166 /* Compute a schedule for each group of nodes identified by node->scc
4167 * separately and then combine them in a sequence node (or as set node
4168 * if graph->weak is set) inserted at position "node" of the schedule tree.
4169 * Return the updated schedule node.
4171 * If "wcc" is set then each of the groups belongs to a single
4172 * weakly connected component in the dependence graph so that
4173 * there is no need for compute_sub_schedule to look for weakly
4174 * connected components.
4176 static __isl_give isl_schedule_node
*compute_component_schedule(
4177 __isl_take isl_schedule_node
*node
, struct isl_sched_graph
*graph
,
4184 isl_union_set_list
*filters
;
4188 ctx
= isl_schedule_node_get_ctx(node
);
4190 filters
= extract_sccs(ctx
, graph
);
4192 node
= isl_schedule_node_insert_set(node
, filters
);
4194 node
= isl_schedule_node_insert_sequence(node
, filters
);
4196 orig_total_row
= graph
->n_total_row
;
4197 for (component
= 0; component
< graph
->scc
; ++component
) {
4199 for (i
= 0; i
< graph
->n
; ++i
)
4200 if (graph
->node
[i
].scc
== component
)
4203 for (i
= 0; i
< graph
->n_edge
; ++i
)
4204 if (graph
->edge
[i
].src
->scc
== component
&&
4205 graph
->edge
[i
].dst
->scc
== component
)
4208 node
= isl_schedule_node_child(node
, component
);
4209 node
= isl_schedule_node_child(node
, 0);
4210 node
= compute_sub_schedule(node
, ctx
, graph
, n
, n_edge
,
4212 &edge_scc_exactly
, component
, wcc
);
4213 node
= isl_schedule_node_parent(node
);
4214 node
= isl_schedule_node_parent(node
);
4215 graph
->n_total_row
= orig_total_row
;
4221 /* Compute a schedule for the given dependence graph and insert it at "node".
4222 * Return the updated schedule node.
4224 * We first check if the graph is connected (through validity and conditional
4225 * validity dependences) and, if not, compute a schedule
4226 * for each component separately.
4227 * If schedule_fuse is set to minimal fusion, then we check for strongly
4228 * connected components instead and compute a separate schedule for
4229 * each such strongly connected component.
4231 static __isl_give isl_schedule_node
*compute_schedule(isl_schedule_node
*node
,
4232 struct isl_sched_graph
*graph
)
4239 ctx
= isl_schedule_node_get_ctx(node
);
4240 if (ctx
->opt
->schedule_fuse
== ISL_SCHEDULE_FUSE_MIN
) {
4241 if (detect_sccs(ctx
, graph
) < 0)
4242 return isl_schedule_node_free(node
);
4244 if (detect_wccs(ctx
, graph
) < 0)
4245 return isl_schedule_node_free(node
);
4249 return compute_component_schedule(node
, graph
, 1);
4251 return compute_schedule_wcc(node
, graph
);
4254 /* Compute a schedule on sc->domain that respects the given schedule
4257 * In particular, the schedule respects all the validity dependences.
4258 * If the default isl scheduling algorithm is used, it tries to minimize
4259 * the dependence distances over the proximity dependences.
4260 * If Feautrier's scheduling algorithm is used, the proximity dependence
4261 * distances are only minimized during the extension to a full-dimensional
4264 * If there are any condition and conditional validity dependences,
4265 * then the conditional validity dependences may be violated inside
4266 * a tilable band, provided they have no adjacent non-local
4267 * condition dependences.
4269 * The context is included in the domain before the nodes of
4270 * the graphs are extracted in order to be able to exploit
4271 * any possible additional equalities.
4272 * However, the returned schedule contains the original domain
4273 * (before this intersection).
4275 __isl_give isl_schedule
*isl_schedule_constraints_compute_schedule(
4276 __isl_take isl_schedule_constraints
*sc
)
4278 isl_ctx
*ctx
= isl_schedule_constraints_get_ctx(sc
);
4279 struct isl_sched_graph graph
= { 0 };
4280 isl_schedule
*sched
;
4281 isl_schedule_node
*node
;
4282 isl_union_set
*domain
;
4283 struct isl_extract_edge_data data
;
4284 enum isl_edge_type i
;
4287 sc
= isl_schedule_constraints_align_params(sc
);
4291 graph
.n
= isl_union_set_n_set(sc
->domain
);
4293 isl_union_set
*domain
= isl_union_set_copy(sc
->domain
);
4294 sched
= isl_schedule_from_domain(domain
);
4297 if (graph_alloc(ctx
, &graph
, graph
.n
,
4298 isl_schedule_constraints_n_map(sc
)) < 0)
4300 if (compute_max_row(&graph
, sc
) < 0)
4304 domain
= isl_union_set_copy(sc
->domain
);
4305 domain
= isl_union_set_intersect_params(domain
,
4306 isl_set_copy(sc
->context
));
4307 r
= isl_union_set_foreach_set(domain
, &extract_node
, &graph
);
4308 isl_union_set_free(domain
);
4311 if (graph_init_table(ctx
, &graph
) < 0)
4313 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
)
4314 graph
.max_edge
[i
] = isl_union_map_n_map(sc
->constraint
[i
]);
4315 if (graph_init_edge_tables(ctx
, &graph
) < 0)
4318 data
.graph
= &graph
;
4319 for (i
= isl_edge_first
; i
<= isl_edge_last
; ++i
) {
4321 if (isl_union_map_foreach_map(sc
->constraint
[i
],
4322 &extract_edge
, &data
) < 0)
4326 node
= isl_schedule_node_from_domain(isl_union_set_copy(sc
->domain
));
4327 node
= isl_schedule_node_child(node
, 0);
4328 node
= compute_schedule(node
, &graph
);
4329 sched
= isl_schedule_node_get_schedule(node
);
4330 isl_schedule_node_free(node
);
4333 graph_free(ctx
, &graph
);
4334 isl_schedule_constraints_free(sc
);
4338 graph_free(ctx
, &graph
);
4339 isl_schedule_constraints_free(sc
);
4343 /* Compute a schedule for the given union of domains that respects
4344 * all the validity dependences and minimizes
4345 * the dependence distances over the proximity dependences.
4347 * This function is kept for backward compatibility.
4349 __isl_give isl_schedule
*isl_union_set_compute_schedule(
4350 __isl_take isl_union_set
*domain
,
4351 __isl_take isl_union_map
*validity
,
4352 __isl_take isl_union_map
*proximity
)
4354 isl_schedule_constraints
*sc
;
4356 sc
= isl_schedule_constraints_on_domain(domain
);
4357 sc
= isl_schedule_constraints_set_validity(sc
, validity
);
4358 sc
= isl_schedule_constraints_set_proximity(sc
, proximity
);
4360 return isl_schedule_constraints_compute_schedule(sc
);