#include <isl_ctx_private.h>
#include <isl_map_private.h>
-#include <isl_dim_private.h>
+#include <isl_space_private.h>
#include <isl/hash.h>
#include <isl/constraint.h>
#include <isl/schedule.h>
#include <isl_qsort.h>
#include <isl_schedule_private.h>
#include <isl_band_private.h>
+#include <isl_list_private.h>
+#include <isl_options_private.h>
/*
* The scheduling algorithm implemented in this file was inspired by
* on_stack indicates whether the node is currently on the stack.
*/
struct isl_sched_node {
- isl_dim *dim;
+ isl_space *dim;
isl_mat *sched;
isl_map *sched_map;
int rank;
static int node_has_dim(const void *entry, const void *val)
{
struct isl_sched_node *node = (struct isl_sched_node *)entry;
- isl_dim *dim = (isl_dim *)val;
+ isl_space *dim = (isl_space *)val;
- return isl_dim_equal(node->dim, dim);
+ return isl_space_is_equal(node->dim, dim);
}
/* An edge in the dependence graph. An edge may be used to
* n is the number of nodes
* node is the list of nodes
* maxvar is the maximal number of variables over all nodes
+ * max_row is the allocated number of rows in the schedule
* n_row is the current (maximal) number of linearly independent
* rows in the node schedules
* n_total_row is the current number of rows in the node schedules
struct isl_sched_node *node;
int n;
int maxvar;
+ int max_row;
int n_row;
int *sorted;
struct isl_hash_table_entry *entry;
uint32_t hash;
- hash = isl_dim_get_hash(graph->node[i].dim);
+ hash = isl_space_get_hash(graph->node[i].dim);
entry = isl_hash_table_find(ctx, graph->node_table, hash,
&node_has_dim,
graph->node[i].dim, 1);
* or NULL if there is no such node.
*/
static struct isl_sched_node *graph_find_node(isl_ctx *ctx,
- struct isl_sched_graph *graph, __isl_keep isl_dim *dim)
+ struct isl_sched_graph *graph, __isl_keep isl_space *dim)
{
struct isl_hash_table_entry *entry;
uint32_t hash;
- hash = isl_dim_get_hash(dim);
+ hash = isl_space_get_hash(dim);
entry = isl_hash_table_find(ctx, graph->node_table, hash,
&node_has_dim, dim, 0);
static int graph_has_edge(struct isl_sched_graph *graph,
struct isl_sched_node *src, struct isl_sched_node *dst)
{
- isl_ctx *ctx = isl_dim_get_ctx(src->dim);
+ isl_ctx *ctx = isl_space_get_ctx(src->dim);
struct isl_hash_table_entry *entry;
uint32_t hash;
struct isl_sched_edge temp = { .src = src, .dst = dst };
isl_hmap_map_basic_set_free(ctx, graph->inter_hmap);
for (i = 0; i < graph->n; ++i) {
- isl_dim_free(graph->node[i].dim);
+ isl_space_free(graph->node[i].dim);
isl_mat_free(graph->node[i].sched);
isl_map_free(graph->node[i].sched_map);
isl_mat_free(graph->node[i].cmap);
isl_basic_set_free(graph->lp);
}
+/* For each "set" on which this function is called, increment
+ * graph->n by one and update graph->maxvar.
+ */
+static int init_n_maxvar(__isl_take isl_set *set, void *user)
+{
+ struct isl_sched_graph *graph = user;
+ int nvar = isl_set_dim(set, isl_dim_set);
+
+ graph->n++;
+ if (nvar > graph->maxvar)
+ graph->maxvar = nvar;
+
+ isl_set_free(set);
+
+ return 0;
+}
+
+/* Compute the number of rows that should be allocated for the schedule.
+ * The graph can be split at most "n - 1" times, there can be at most
+ * two rows for each dimension in the iteration domains (in particular,
+ * we usually have one row, but it may be split by split_parallel),
+ * and there can be one extra row for ordering the statements.
+ * Note that if we have actually split "n - 1" times, then no ordering
+ * is needed, so in principle we could use "graph->n + 2 * graph->maxvar - 1".
+ */
+static int compute_max_row(struct isl_sched_graph *graph,
+ __isl_keep isl_union_set *domain)
+{
+ graph->n = 0;
+ graph->maxvar = 0;
+ if (isl_union_set_foreach_set(domain, &init_n_maxvar, graph) < 0)
+ return -1;
+ graph->max_row = graph->n + 2 * graph->maxvar;
+
+ return 0;
+}
+
/* Add a new node to the graph representing the given set.
*/
static int extract_node(__isl_take isl_set *set, void *user)
{
int nvar, nparam;
isl_ctx *ctx;
- isl_dim *dim;
+ isl_space *dim;
isl_mat *sched;
struct isl_sched_graph *graph = user;
int *band, *band_id, *zero;
ctx = isl_set_get_ctx(set);
- dim = isl_set_get_dim(set);
+ dim = isl_set_get_space(set);
isl_set_free(set);
- nvar = isl_dim_size(dim, isl_dim_set);
- nparam = isl_dim_size(dim, isl_dim_param);
+ nvar = isl_space_dim(dim, isl_dim_set);
+ nparam = isl_space_dim(dim, isl_dim_param);
if (!ctx->opt->schedule_parametric)
nparam = 0;
sched = isl_mat_alloc(ctx, 0, 1 + nparam + nvar);
graph->node[graph->n].nparam = nparam;
graph->node[graph->n].sched = sched;
graph->node[graph->n].sched_map = NULL;
- band = isl_alloc_array(ctx, int, graph->n_edge + nvar);
+ band = isl_alloc_array(ctx, int, graph->max_row);
graph->node[graph->n].band = band;
- band_id = isl_calloc_array(ctx, int, graph->n_edge + nvar);
+ band_id = isl_calloc_array(ctx, int, graph->max_row);
graph->node[graph->n].band_id = band_id;
- zero = isl_calloc_array(ctx, int, graph->n_edge + nvar);
+ zero = isl_calloc_array(ctx, int, graph->max_row);
graph->node[graph->n].zero = zero;
graph->n++;
isl_ctx *ctx = isl_map_get_ctx(map);
struct isl_sched_graph *graph = user;
struct isl_sched_node *src, *dst;
- isl_dim *dim;
+ isl_space *dim;
- dim = isl_dim_domain(isl_map_get_dim(map));
+ dim = isl_space_domain(isl_map_get_space(map));
src = graph_find_node(ctx, graph, dim);
- isl_dim_free(dim);
- dim = isl_dim_range(isl_map_get_dim(map));
+ isl_space_free(dim);
+ dim = isl_space_range(isl_map_get_space(map));
dst = graph_find_node(ctx, graph, dim);
- isl_dim_free(dim);
+ isl_space_free(dim);
if (!src || !dst) {
isl_map_free(map);
unsigned total;
isl_map *map = isl_map_copy(edge->map);
isl_ctx *ctx = isl_map_get_ctx(map);
- isl_dim *dim;
+ isl_space *dim;
isl_dim_map *dim_map;
isl_basic_set *coef;
struct isl_sched_node *node = edge->src;
coef = intra_coefficients(graph, map);
- dim = isl_dim_domain(isl_dim_unwrap(isl_basic_set_get_dim(coef)));
+ dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
coef = isl_basic_set_transform_dims(coef, isl_dim_set,
- isl_dim_size(dim, isl_dim_set), isl_mat_copy(node->cmap));
+ isl_space_dim(dim, isl_dim_set), isl_mat_copy(node->cmap));
total = isl_basic_set_total_dim(graph->lp);
dim_map = isl_dim_map_alloc(ctx, total);
isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 1, 2,
- isl_dim_size(dim, isl_dim_set), 1,
+ isl_space_dim(dim, isl_dim_set), 1,
node->nvar, -1);
isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 2, 2,
- isl_dim_size(dim, isl_dim_set), 1,
+ isl_space_dim(dim, isl_dim_set), 1,
node->nvar, 1);
graph->lp = isl_basic_set_extend_constraints(graph->lp,
coef->n_eq, coef->n_ineq);
graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
coef, dim_map);
- isl_dim_free(dim);
+ isl_space_free(dim);
return 0;
}
unsigned total;
isl_map *map = isl_map_copy(edge->map);
isl_ctx *ctx = isl_map_get_ctx(map);
- isl_dim *dim;
+ isl_space *dim;
isl_dim_map *dim_map;
isl_basic_set *coef;
struct isl_sched_node *src = edge->src;
coef = inter_coefficients(graph, map);
- dim = isl_dim_domain(isl_dim_unwrap(isl_basic_set_get_dim(coef)));
+ dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
coef = isl_basic_set_transform_dims(coef, isl_dim_set,
- isl_dim_size(dim, isl_dim_set), isl_mat_copy(src->cmap));
+ isl_space_dim(dim, isl_dim_set), isl_mat_copy(src->cmap));
coef = isl_basic_set_transform_dims(coef, isl_dim_set,
- isl_dim_size(dim, isl_dim_set) + src->nvar,
+ isl_space_dim(dim, isl_dim_set) + src->nvar,
isl_mat_copy(dst->cmap));
total = isl_basic_set_total_dim(graph->lp);
isl_dim_map_range(dim_map, dst->start + 1, 2, 1, 1, dst->nparam, -1);
isl_dim_map_range(dim_map, dst->start + 2, 2, 1, 1, dst->nparam, 1);
isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 1, 2,
- isl_dim_size(dim, isl_dim_set) + src->nvar, 1,
+ isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
dst->nvar, -1);
isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 2, 2,
- isl_dim_size(dim, isl_dim_set) + src->nvar, 1,
+ isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
dst->nvar, 1);
isl_dim_map_range(dim_map, src->start, 0, 0, 0, 1, -1);
isl_dim_map_range(dim_map, src->start + 1, 2, 1, 1, src->nparam, 1);
isl_dim_map_range(dim_map, src->start + 2, 2, 1, 1, src->nparam, -1);
isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 1, 2,
- isl_dim_size(dim, isl_dim_set), 1,
+ isl_space_dim(dim, isl_dim_set), 1,
src->nvar, 1);
isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 2, 2,
- isl_dim_size(dim, isl_dim_set), 1,
+ isl_space_dim(dim, isl_dim_set), 1,
src->nvar, -1);
edge->start = graph->lp->n_ineq;
coef->n_eq, coef->n_ineq);
graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
coef, dim_map);
- isl_dim_free(dim);
+ isl_space_free(dim);
edge->end = graph->lp->n_ineq;
return 0;
unsigned nparam;
isl_map *map = isl_map_copy(edge->map);
isl_ctx *ctx = isl_map_get_ctx(map);
- isl_dim *dim;
+ isl_space *dim;
isl_dim_map *dim_map;
isl_basic_set *coef;
struct isl_sched_node *node = edge->src;
coef = intra_coefficients(graph, map);
- dim = isl_dim_domain(isl_dim_unwrap(isl_basic_set_get_dim(coef)));
+ dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
coef = isl_basic_set_transform_dims(coef, isl_dim_set,
- isl_dim_size(dim, isl_dim_set), isl_mat_copy(node->cmap));
+ isl_space_dim(dim, isl_dim_set), isl_mat_copy(node->cmap));
- nparam = isl_dim_size(node->dim, isl_dim_param);
+ nparam = isl_space_dim(node->dim, isl_dim_param);
total = isl_basic_set_total_dim(graph->lp);
dim_map = isl_dim_map_alloc(ctx, total);
isl_dim_map_range(dim_map, 1, 0, 0, 0, 1, 1);
isl_dim_map_range(dim_map, 4, 2, 1, 1, nparam, -1);
isl_dim_map_range(dim_map, 5, 2, 1, 1, nparam, 1);
isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 1, 2,
- isl_dim_size(dim, isl_dim_set), 1,
+ isl_space_dim(dim, isl_dim_set), 1,
node->nvar, s);
isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 2, 2,
- isl_dim_size(dim, isl_dim_set), 1,
+ isl_space_dim(dim, isl_dim_set), 1,
node->nvar, -s);
graph->lp = isl_basic_set_extend_constraints(graph->lp,
coef->n_eq, coef->n_ineq);
graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
coef, dim_map);
- isl_dim_free(dim);
+ isl_space_free(dim);
return 0;
}
unsigned nparam;
isl_map *map = isl_map_copy(edge->map);
isl_ctx *ctx = isl_map_get_ctx(map);
- isl_dim *dim;
+ isl_space *dim;
isl_dim_map *dim_map;
isl_basic_set *coef;
struct isl_sched_node *src = edge->src;
coef = inter_coefficients(graph, map);
- dim = isl_dim_domain(isl_dim_unwrap(isl_basic_set_get_dim(coef)));
+ dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
coef = isl_basic_set_transform_dims(coef, isl_dim_set,
- isl_dim_size(dim, isl_dim_set), isl_mat_copy(src->cmap));
+ isl_space_dim(dim, isl_dim_set), isl_mat_copy(src->cmap));
coef = isl_basic_set_transform_dims(coef, isl_dim_set,
- isl_dim_size(dim, isl_dim_set) + src->nvar,
+ isl_space_dim(dim, isl_dim_set) + src->nvar,
isl_mat_copy(dst->cmap));
- nparam = isl_dim_size(src->dim, isl_dim_param);
+ nparam = isl_space_dim(src->dim, isl_dim_param);
total = isl_basic_set_total_dim(graph->lp);
dim_map = isl_dim_map_alloc(ctx, total);
isl_dim_map_range(dim_map, dst->start + 1, 2, 1, 1, dst->nparam, s);
isl_dim_map_range(dim_map, dst->start + 2, 2, 1, 1, dst->nparam, -s);
isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 1, 2,
- isl_dim_size(dim, isl_dim_set) + src->nvar, 1,
+ isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
dst->nvar, s);
isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 2, 2,
- isl_dim_size(dim, isl_dim_set) + src->nvar, 1,
+ isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
dst->nvar, -s);
isl_dim_map_range(dim_map, src->start, 0, 0, 0, 1, s);
isl_dim_map_range(dim_map, src->start + 1, 2, 1, 1, src->nparam, -s);
isl_dim_map_range(dim_map, src->start + 2, 2, 1, 1, src->nparam, s);
isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 1, 2,
- isl_dim_size(dim, isl_dim_set), 1,
+ isl_space_dim(dim, isl_dim_set), 1,
src->nvar, -s);
isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 2, 2,
- isl_dim_size(dim, isl_dim_set), 1,
+ isl_space_dim(dim, isl_dim_set), 1,
src->nvar, s);
graph->lp = isl_basic_set_extend_constraints(graph->lp,
coef->n_eq, coef->n_ineq);
graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
coef, dim_map);
- isl_dim_free(dim);
+ isl_space_free(dim);
return 0;
}
}
/* Count the number of equality and inequality constraints
- * that will be added. If once is set, then we count
+ * that will be added for the given map.
+ * If once is set, then we count
+ * each edge exactly once. Otherwise, we count as follows
+ * validity -> 1 (>= 0)
+ * validity+proximity -> 2 (>= 0 and upper bound)
+ * proximity -> 2 (lower and upper bound)
+ */
+static int count_map_constraints(struct isl_sched_graph *graph,
+ struct isl_sched_edge *edge, __isl_take isl_map *map,
+ int *n_eq, int *n_ineq, int once)
+{
+ isl_basic_set *coef;
+ int f = once ? 1 : edge->proximity ? 2 : 1;
+
+ if (edge->src == edge->dst)
+ coef = intra_coefficients(graph, map);
+ else
+ coef = inter_coefficients(graph, map);
+ if (!coef)
+ return -1;
+ *n_eq += f * coef->n_eq;
+ *n_ineq += f * coef->n_ineq;
+ isl_basic_set_free(coef);
+
+ return 0;
+}
+
+/* Count the number of equality and inequality constraints
+ * that will be added to the main lp problem.
+ * If once is set, then we count
* each edge exactly once. Otherwise, we count as follows
* validity -> 1 (>= 0)
* validity+proximity -> 2 (>= 0 and upper bound)
int *n_eq, int *n_ineq, int once)
{
int i;
- isl_basic_set *coef;
*n_eq = *n_ineq = 0;
for (i = 0; i < graph->n_edge; ++i) {
struct isl_sched_edge *edge= &graph->edge[i];
isl_map *map = isl_map_copy(edge->map);
- int f = once ? 1 : edge->proximity ? 2 : 1;
- if (edge->src == edge->dst)
- coef = intra_coefficients(graph, map);
- else
- coef = inter_coefficients(graph, map);
- if (!coef)
+ if (count_map_constraints(graph, edge, map,
+ n_eq, n_ineq, once) < 0)
return -1;
- *n_eq += f * coef->n_eq;
- *n_ineq += f * coef->n_ineq;
- isl_basic_set_free(coef);
}
return 0;
int k;
unsigned nparam;
unsigned total;
- isl_dim *dim;
+ isl_space *dim;
int parametric;
int param_pos;
int n_eq, n_ineq;
+ int max_constant_term;
+
+ max_constant_term = ctx->opt->schedule_max_constant_term;
parametric = ctx->opt->schedule_parametric;
- nparam = isl_dim_size(graph->node[0].dim, isl_dim_param);
+ nparam = isl_space_dim(graph->node[0].dim, isl_dim_param);
param_pos = 4;
total = param_pos + 2 * nparam;
for (i = 0; i < graph->n; ++i) {
if (count_constraints(graph, &n_eq, &n_ineq, 0) < 0)
return -1;
- dim = isl_dim_set_alloc(ctx, 0, total);
+ dim = isl_space_set_alloc(ctx, 0, total);
isl_basic_set_free(graph->lp);
n_eq += 2 + parametric + force_zero;
- graph->lp = isl_basic_set_alloc_dim(dim, 0, n_eq, n_ineq);
+ if (max_constant_term != -1)
+ n_ineq += graph->n;
+
+ graph->lp = isl_basic_set_alloc_space(dim, 0, n_eq, n_ineq);
k = isl_basic_set_alloc_equality(graph->lp);
if (k < 0)
isl_int_set_si(graph->lp->eq[k][pos + j], 1);
}
+ if (max_constant_term != -1)
+ for (i = 0; i < graph->n; ++i) {
+ struct isl_sched_node *node = &graph->node[i];
+ k = isl_basic_set_alloc_inequality(graph->lp);
+ if (k < 0)
+ return -1;
+ isl_seq_clr(graph->lp->ineq[k], 1 + total);
+ isl_int_set_si(graph->lp->ineq[k][1 + node->start], -1);
+ isl_int_set_si(graph->lp->ineq[k][0], max_constant_term);
+ }
+
if (add_all_validity_constraints(graph) < 0)
return -1;
if (add_all_proximity_constraints(graph) < 0)
static __isl_give isl_map *node_extract_schedule(struct isl_sched_node *node)
{
int i, j;
- isl_dim *dim;
+ isl_space *dim;
+ isl_local_space *ls;
isl_basic_map *bmap;
isl_constraint *c;
int nrow, ncol;
nrow = isl_mat_rows(node->sched);
ncol = isl_mat_cols(node->sched) - 1;
- dim = isl_dim_from_domain(isl_dim_copy(node->dim));
- dim = isl_dim_add(dim, isl_dim_out, nrow);
- bmap = isl_basic_map_universe(isl_dim_copy(dim));
+ dim = isl_space_from_domain(isl_space_copy(node->dim));
+ dim = isl_space_add_dims(dim, isl_dim_out, nrow);
+ bmap = isl_basic_map_universe(isl_space_copy(dim));
+ ls = isl_local_space_from_space(dim);
isl_int_init(v);
for (i = 0; i < nrow; ++i) {
- c = isl_equality_alloc(isl_dim_copy(dim));
+ c = isl_equality_alloc(isl_local_space_copy(ls));
isl_constraint_set_coefficient_si(c, isl_dim_out, i, -1);
isl_mat_get_element(node->sched, i, 0, &v);
isl_constraint_set_constant(c, v);
isl_int_clear(v);
- isl_dim_free(dim);
+ isl_local_space_free(ls);
node->sched_map = isl_map_from_basic_map(bmap);
return isl_map_copy(node->sched_map);
* in graph and with parameters specified by dim.
*/
static __isl_give isl_schedule *extract_schedule(struct isl_sched_graph *graph,
- __isl_take isl_dim *dim)
+ __isl_take isl_space *dim)
{
int i;
isl_ctx *ctx;
if (!dim)
return NULL;
- ctx = isl_dim_get_ctx(dim);
+ ctx = isl_space_get_ctx(dim);
sched = isl_calloc(ctx, struct isl_schedule,
sizeof(struct isl_schedule) +
(graph->n - 1) * sizeof(struct isl_schedule_node));
return sched;
error:
- isl_dim_free(dim);
+ isl_space_free(dim);
isl_schedule_free(sched);
return NULL;
}
for (i = 0; i < src->n; ++i) {
if (!node_pred(&src->node[i], data))
continue;
- dst->node[dst->n].dim = isl_dim_copy(src->node[i].dim);
+ dst->node[dst->n].dim = isl_space_copy(src->node[i].dim);
dst->node[dst->n].nvar = src->node[i].nvar;
dst->node[dst->n].nparam = src->node[i].nparam;
dst->node[dst->n].sched = isl_mat_copy(src->node[i].sched);
/* Copy non-empty edges that satisfy edge_pred from the src dependence graph
* to the dst dependence graph.
+ * If the source or destination node of the edge is not in the destination
+ * graph, then it must be a backward proximity edge and it should simply
+ * be ignored.
*/
static int copy_edges(isl_ctx *ctx, struct isl_sched_graph *dst,
struct isl_sched_graph *src,
for (i = 0; i < src->n_edge; ++i) {
struct isl_sched_edge *edge = &src->edge[i];
isl_map *map;
+ struct isl_sched_node *dst_src, *dst_dst;
if (!edge_pred(edge, data))
continue;
if (isl_map_plain_is_empty(edge->map))
continue;
+ dst_src = graph_find_node(ctx, dst, edge->src->dim);
+ dst_dst = graph_find_node(ctx, dst, edge->dst->dim);
+ if (!dst_src || !dst_dst) {
+ if (edge->validity)
+ isl_die(ctx, isl_error_internal,
+ "backward validity edge", return -1);
+ continue;
+ }
+
map = isl_map_copy(edge->map);
- dst->edge[dst->n_edge].src =
- graph_find_node(ctx, dst, edge->src->dim);
- dst->edge[dst->n_edge].dst =
- graph_find_node(ctx, dst, edge->dst->dim);
+ dst->edge[dst->n_edge].src = dst_src;
+ dst->edge[dst->n_edge].dst = dst_dst;
dst->edge[dst->n_edge].map = map;
dst->edge[dst->n_edge].validity = edge->validity;
dst->edge[dst->n_edge].proximity = edge->proximity;
* It would be possible to reuse them as the first rows in the next
* band, but recomputing them may result in better rows as we are looking
* at a smaller part of the dependence graph.
+ * compute_split_schedule is only called when no zero-distance schedule row
+ * could be found on the entire graph, so we wark the splitting row as
+ * non zero-distance.
*
* The band_id of the second group is set to n, where n is the number
* of nodes in the first group. This ensures that the band_ids over
node->sched = isl_mat_set_element_si(node->sched,
row, j, 0);
node->band[graph->n_total_row] = graph->n_band;
+ node->zero[graph->n_total_row] = 0;
}
e1 = e2 = 0;
return compute_schedule(ctx, graph);
}
-/* Add constraints to graph->lp that force the dependence of edge i
- * to be respected and attempt to carry it, where edge i is one from
- * a node j to itself.
+/* Add constraints to graph->lp that force the dependence "map" (which
+ * is part of the dependence relation of "edge")
+ * to be respected and attempt to carry it, where the edge is one from
+ * a node j to itself. "pos" is the sequence number of the given map.
* That is, add constraints that enforce
*
* (c_j_0 + c_j_n n + c_j_x y) - (c_j_0 + c_j_n n + c_j_x x)
* with each coefficient in c_j_x represented as a pair of non-negative
* coefficients.
*/
-static int add_intra_constraints(struct isl_sched_graph *graph, int i)
+static int add_intra_constraints(struct isl_sched_graph *graph,
+ struct isl_sched_edge *edge, __isl_take isl_map *map, int pos)
{
unsigned total;
- struct isl_sched_edge *edge= &graph->edge[i];
- isl_map *map = isl_map_copy(edge->map);
isl_ctx *ctx = isl_map_get_ctx(map);
- isl_dim *dim;
+ isl_space *dim;
isl_dim_map *dim_map;
isl_basic_set *coef;
struct isl_sched_node *node = edge->src;
coef = intra_coefficients(graph, map);
- dim = isl_dim_domain(isl_dim_unwrap(isl_basic_set_get_dim(coef)));
+ dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
total = isl_basic_set_total_dim(graph->lp);
dim_map = isl_dim_map_alloc(ctx, total);
- isl_dim_map_range(dim_map, 3 + i, 0, 0, 0, 1, -1);
+ isl_dim_map_range(dim_map, 3 + pos, 0, 0, 0, 1, -1);
isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 1, 2,
- isl_dim_size(dim, isl_dim_set), 1,
+ isl_space_dim(dim, isl_dim_set), 1,
node->nvar, -1);
isl_dim_map_range(dim_map, node->start + 2 * node->nparam + 2, 2,
- isl_dim_size(dim, isl_dim_set), 1,
+ isl_space_dim(dim, isl_dim_set), 1,
node->nvar, 1);
graph->lp = isl_basic_set_extend_constraints(graph->lp,
coef->n_eq, coef->n_ineq);
graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
coef, dim_map);
- isl_dim_free(dim);
+ isl_space_free(dim);
return 0;
}
-/* Add constraints to graph->lp that force the dependence of edge i
- * to be respected and attempt to carry it, where edge i is one from
- * node j to node k.
+/* Add constraints to graph->lp that force the dependence "map" (which
+ * is part of the dependence relation of "edge")
+ * to be respected and attempt to carry it, where the edge is one from
+ * node j to node k. "pos" is the sequence number of the given map.
* That is, add constraints that enforce
*
* (c_k_0 + c_k_n n + c_k_x y) - (c_j_0 + c_j_n n + c_j_x x) >= e_i
* with each coefficient (except e_i, c_k_0 and c_j_0)
* represented as a pair of non-negative coefficients.
*/
-static int add_inter_constraints(struct isl_sched_graph *graph, int i)
+static int add_inter_constraints(struct isl_sched_graph *graph,
+ struct isl_sched_edge *edge, __isl_take isl_map *map, int pos)
{
unsigned total;
- struct isl_sched_edge *edge= &graph->edge[i];
- isl_map *map = isl_map_copy(edge->map);
isl_ctx *ctx = isl_map_get_ctx(map);
- isl_dim *dim;
+ isl_space *dim;
isl_dim_map *dim_map;
isl_basic_set *coef;
struct isl_sched_node *src = edge->src;
coef = inter_coefficients(graph, map);
- dim = isl_dim_domain(isl_dim_unwrap(isl_basic_set_get_dim(coef)));
+ dim = isl_space_domain(isl_space_unwrap(isl_basic_set_get_space(coef)));
total = isl_basic_set_total_dim(graph->lp);
dim_map = isl_dim_map_alloc(ctx, total);
- isl_dim_map_range(dim_map, 3 + i, 0, 0, 0, 1, -1);
+ isl_dim_map_range(dim_map, 3 + pos, 0, 0, 0, 1, -1);
isl_dim_map_range(dim_map, dst->start, 0, 0, 0, 1, 1);
isl_dim_map_range(dim_map, dst->start + 1, 2, 1, 1, dst->nparam, -1);
isl_dim_map_range(dim_map, dst->start + 2, 2, 1, 1, dst->nparam, 1);
isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 1, 2,
- isl_dim_size(dim, isl_dim_set) + src->nvar, 1,
+ isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
dst->nvar, -1);
isl_dim_map_range(dim_map, dst->start + 2 * dst->nparam + 2, 2,
- isl_dim_size(dim, isl_dim_set) + src->nvar, 1,
+ isl_space_dim(dim, isl_dim_set) + src->nvar, 1,
dst->nvar, 1);
isl_dim_map_range(dim_map, src->start, 0, 0, 0, 1, -1);
isl_dim_map_range(dim_map, src->start + 1, 2, 1, 1, src->nparam, 1);
isl_dim_map_range(dim_map, src->start + 2, 2, 1, 1, src->nparam, -1);
isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 1, 2,
- isl_dim_size(dim, isl_dim_set), 1,
+ isl_space_dim(dim, isl_dim_set), 1,
src->nvar, 1);
isl_dim_map_range(dim_map, src->start + 2 * src->nparam + 2, 2,
- isl_dim_size(dim, isl_dim_set), 1,
+ isl_space_dim(dim, isl_dim_set), 1,
src->nvar, -1);
graph->lp = isl_basic_set_extend_constraints(graph->lp,
coef->n_eq, coef->n_ineq);
graph->lp = isl_basic_set_add_constraints_dim_map(graph->lp,
coef, dim_map);
- isl_dim_free(dim);
+ isl_space_free(dim);
return 0;
}
*/
static int add_all_constraints(struct isl_sched_graph *graph)
{
- int i;
+ int i, j;
+ int pos;
+ pos = 0;
for (i = 0; i < graph->n_edge; ++i) {
struct isl_sched_edge *edge= &graph->edge[i];
- if (edge->src == edge->dst &&
- add_intra_constraints(graph, i) < 0)
- return -1;
- if (edge->src != edge->dst &&
- add_inter_constraints(graph, i) < 0)
- return -1;
+ for (j = 0; j < edge->map->n; ++j) {
+ isl_basic_map *bmap;
+ isl_map *map;
+
+ bmap = isl_basic_map_copy(edge->map->p[j]);
+ map = isl_map_from_basic_map(bmap);
+
+ if (edge->src == edge->dst &&
+ add_intra_constraints(graph, edge, map, pos) < 0)
+ return -1;
+ if (edge->src != edge->dst &&
+ add_inter_constraints(graph, edge, map, pos) < 0)
+ return -1;
+ ++pos;
+ }
+ }
+
+ return 0;
+}
+
+/* Count the number of equality and inequality constraints
+ * that will be added to the carry_lp problem.
+ * If once is set, then we count
+ * each edge exactly once. Otherwise, we count as follows
+ * validity -> 1 (>= 0)
+ * validity+proximity -> 2 (>= 0 and upper bound)
+ * proximity -> 2 (lower and upper bound)
+ */
+static int count_all_constraints(struct isl_sched_graph *graph,
+ int *n_eq, int *n_ineq, int once)
+{
+ int i, j;
+
+ *n_eq = *n_ineq = 0;
+ for (i = 0; i < graph->n_edge; ++i) {
+ struct isl_sched_edge *edge= &graph->edge[i];
+ for (j = 0; j < edge->map->n; ++j) {
+ isl_basic_map *bmap;
+ isl_map *map;
+
+ bmap = isl_basic_map_copy(edge->map->p[j]);
+ map = isl_map_from_basic_map(bmap);
+
+ if (count_map_constraints(graph, edge, map,
+ n_eq, n_ineq, once) < 0)
+ return -1;
+ }
}
return 0;
* from below by e_i, with 0 <= e_i <= 1 and then maximize the sum
* of all e_i's. Dependence with e_i = 0 in the solution are simply
* respected, while those with e_i > 0 (in practice e_i = 1) are carried.
+ * Note that if the dependence relation is a union of basic maps,
+ * then we have to consider each basic map individually as it may only
+ * be possible to carry the dependences expressed by some of those
+ * basic maps and not all off them.
+ * Below, we consider each of those basic maps as a separate "edge".
*
* All variables of the LP are non-negative. The actual coefficients
* may be negative, so each coefficient is represented as the difference
{
int i, j;
int k;
- isl_dim *dim;
+ isl_space *dim;
unsigned total;
int n_eq, n_ineq;
+ int n_edge;
- total = 3 + graph->n_edge;
+ n_edge = 0;
+ for (i = 0; i < graph->n_edge; ++i)
+ n_edge += graph->edge[i].map->n;
+
+ total = 3 + n_edge;
for (i = 0; i < graph->n; ++i) {
struct isl_sched_node *node = &graph->node[graph->sorted[i]];
node->start = total;
total += 1 + 2 * (node->nparam + node->nvar);
}
- if (count_constraints(graph, &n_eq, &n_ineq, 1) < 0)
+ if (count_all_constraints(graph, &n_eq, &n_ineq, 1) < 0)
return -1;
- dim = isl_dim_set_alloc(ctx, 0, total);
+ dim = isl_space_set_alloc(ctx, 0, total);
isl_basic_set_free(graph->lp);
n_eq += 3;
- n_ineq += graph->n_edge;
- graph->lp = isl_basic_set_alloc_dim(dim, 0, n_eq, n_ineq);
+ n_ineq += n_edge;
+ graph->lp = isl_basic_set_alloc_space(dim, 0, n_eq, n_ineq);
graph->lp = isl_basic_set_set_rational(graph->lp);
k = isl_basic_set_alloc_equality(graph->lp);
if (k < 0)
return -1;
isl_seq_clr(graph->lp->eq[k], 1 + total);
- isl_int_set_si(graph->lp->eq[k][0], -graph->n_edge);
+ isl_int_set_si(graph->lp->eq[k][0], -n_edge);
isl_int_set_si(graph->lp->eq[k][1], 1);
- for (i = 0; i < graph->n_edge; ++i)
+ for (i = 0; i < n_edge; ++i)
isl_int_set_si(graph->lp->eq[k][4 + i], 1);
k = isl_basic_set_alloc_equality(graph->lp);
isl_int_set_si(graph->lp->eq[k][pos + j], 1);
}
- for (i = 0; i < graph->n_edge; ++i) {
+ for (i = 0; i < n_edge; ++i) {
k = isl_basic_set_alloc_inequality(graph->lp);
if (k < 0)
return -1;
for (i = 1; i < graph->n; ++i) {
struct isl_sched_node *node = &graph->node[i];
+ if (isl_mat_cols(node->sched) != cols)
+ return 0;
if (!isl_seq_eq(node0->sched->row[row] + 1,
node->sched->row[row] + 1, cols - 1))
return 0;
*/
static int carry_dependences(isl_ctx *ctx, struct isl_sched_graph *graph)
{
+ int i;
+ int n_edge;
isl_vec *sol;
isl_basic_set *lp;
+ n_edge = 0;
+ for (i = 0; i < graph->n_edge; ++i)
+ n_edge += graph->edge[i].map->n;
+
if (setup_carry_lp(ctx, graph) < 0)
return -1;
"error in schedule construction", return -1);
}
- if (isl_int_cmp_si(sol->el[1], graph->n_edge) >= 0) {
+ if (isl_int_cmp_si(sol->el[1], n_edge) >= 0) {
isl_vec_free(sol);
isl_die(ctx, isl_error_unknown,
"unable to carry dependences", return -1);
return compute_next_band(ctx, graph);
}
+/* Are there any validity edges in the graph?
+ */
+static int has_validity_edges(struct isl_sched_graph *graph)
+{
+ int i;
+
+ for (i = 0; i < graph->n_edge; ++i)
+ if (graph->edge[i].validity)
+ return 1;
+
+ return 0;
+}
+
+/* Should we apply a Feautrier step?
+ * That is, did the user request the Feautrier algorithm and are
+ * there any validity dependences (left)?
+ */
+static int need_feautrier_step(isl_ctx *ctx, struct isl_sched_graph *graph)
+{
+ if (ctx->opt->schedule_algorithm != ISL_SCHEDULE_ALGORITHM_FEAUTRIER)
+ return 0;
+
+ return has_validity_edges(graph);
+}
+
+/* Compute a schedule for a connected dependence graph using Feautrier's
+ * multi-dimensional scheduling algorithm.
+ * The original algorithm is described in [1].
+ * The main idea is to minimize the number of scheduling dimensions, by
+ * trying to satisfy as many dependences as possible per scheduling dimension.
+ *
+ * [1] P. Feautrier, Some Efficient Solutions to the Affine Scheduling
+ * Problem, Part II: Multi-Dimensional Time.
+ * In Intl. Journal of Parallel Programming, 1992.
+ */
+static int compute_schedule_wcc_feautrier(isl_ctx *ctx,
+ struct isl_sched_graph *graph)
+{
+ return carry_dependences(ctx, graph);
+}
+
/* Compute a schedule for a connected dependence graph.
* We try to find a sequence of as many schedule rows as possible that result
* in non-negative dependence distances (independent of the previous rows
* - try to carry as many dependences as possible and continue with the next
* band
*
+ * If Feautrier's algorithm is selected, we first recursively try to satisfy
+ * as many validity dependences as possible. When all validity dependences
+ * are satisfied we extend the schedule to a full-dimensional schedule.
+ *
* If we manage to complete the schedule, we finish off by topologically
* sorting the statements based on the remaining dependences.
*
if (compute_maxvar(graph) < 0)
return -1;
+ if (need_feautrier_step(ctx, graph))
+ return compute_schedule_wcc_feautrier(ctx, graph);
+
if (ctx->opt->schedule_outer_zero_distance)
force_zero = 1;
return -1;
if (sol->size == 0) {
isl_vec_free(sol);
+ if (!ctx->opt->schedule_maximize_band_depth &&
+ graph->n_total_row > graph->band_start)
+ return compute_next_band(ctx, graph);
if (graph->src_scc >= 0)
return compute_split_schedule(ctx, graph);
if (graph->n_total_row > graph->band_start)
/* Compute a schedule for the given dependence graph.
* We first check if the graph is connected (through validity dependences)
- * and if so compute a schedule for each component separately.
+ * and, if not, compute a schedule for each component separately.
*/
static int compute_schedule(isl_ctx *ctx, struct isl_sched_graph *graph)
{
}
/* Compute a schedule for the given union of domains that respects
- * all the validity dependences and tries to minimize the dependence
- * distances over the proximity dependences.
+ * all the validity dependences.
+ * If the default isl scheduling algorithm is used, it tries to minimize
+ * the dependence distances over the proximity dependences.
+ * If Feautrier's scheduling algorithm is used, the proximity dependence
+ * distances are only minimized during the extension to a full-dimensional
+ * schedule.
*/
__isl_give isl_schedule *isl_union_set_compute_schedule(
__isl_take isl_union_set *domain,
__isl_take isl_union_map *proximity)
{
isl_ctx *ctx = isl_union_set_get_ctx(domain);
- isl_dim *dim;
+ isl_space *dim;
struct isl_sched_graph graph = { 0 };
isl_schedule *sched;
domain = isl_union_set_align_params(domain,
- isl_union_map_get_dim(validity));
+ isl_union_map_get_space(validity));
domain = isl_union_set_align_params(domain,
- isl_union_map_get_dim(proximity));
- dim = isl_union_set_get_dim(domain);
- validity = isl_union_map_align_params(validity, isl_dim_copy(dim));
+ isl_union_map_get_space(proximity));
+ dim = isl_union_set_get_space(domain);
+ validity = isl_union_map_align_params(validity, isl_space_copy(dim));
proximity = isl_union_map_align_params(proximity, dim);
if (!domain)
if (graph_alloc(ctx, &graph, graph.n,
isl_union_map_n_map(validity) + isl_union_map_n_map(proximity)) < 0)
goto error;
+ if (compute_max_row(&graph, domain) < 0)
+ goto error;
graph.root = 1;
graph.n = 0;
if (isl_union_set_foreach_set(domain, &extract_node, &graph) < 0)
goto error;
empty:
- sched = extract_schedule(&graph, isl_union_set_get_dim(domain));
+ sched = extract_schedule(&graph, isl_union_set_get_space(domain));
graph_free(ctx, &graph);
isl_union_set_free(domain);
free(sched->node[i].band_id);
free(sched->node[i].zero);
}
- isl_dim_free(sched->dim);
+ isl_space_free(sched->dim);
isl_band_list_free(sched->band_forest);
free(sched);
return NULL;
isl_ctx *isl_schedule_get_ctx(__isl_keep isl_schedule *schedule)
{
- return schedule ? isl_dim_get_ctx(schedule->dim) : NULL;
+ return schedule ? isl_space_get_ctx(schedule->dim) : NULL;
}
__isl_give isl_union_map *isl_schedule_get_map(__isl_keep isl_schedule *sched)
if (!sched)
return NULL;
- umap = isl_union_map_empty(isl_dim_copy(sched->dim));
+ umap = isl_union_map_empty(isl_space_copy(sched->dim));
for (i = 0; i < sched->n; ++i)
umap = isl_union_map_add_map(umap,
isl_map_copy(sched->node[i].sched));
return umap;
}
-int isl_schedule_n_band(__isl_keep isl_schedule *sched)
-{
- return sched ? sched->n_band : 0;
-}
-
-/* Construct a mapping that maps each domain to the band in its schedule
- * with the specified band index. Note that bands with the same index
- * but for different domains do not need to be related.
- */
-__isl_give isl_union_map *isl_schedule_get_band(__isl_keep isl_schedule *sched,
- unsigned band)
-{
- int i;
- isl_union_map *umap;
-
- if (!sched)
- return NULL;
-
- umap = isl_union_map_empty(isl_dim_copy(sched->dim));
- for (i = 0; i < sched->n; ++i) {
- int start, end;
- isl_map *map;
-
- if (band >= sched->node[i].n_band)
- continue;
-
- start = band > 0 ? sched->node[i].band_end[band - 1] : 0;
- end = sched->node[i].band_end[band];
-
- map = isl_map_copy(sched->node[i].sched);
-
- map = isl_map_project_out(map, isl_dim_out, end,
- sched->n_total_row - end);
- map = isl_map_project_out(map, isl_dim_out, 0, start);
-
- umap = isl_union_map_add_map(umap, map);
- }
-
- return umap;
-}
-
static __isl_give isl_band_list *construct_band_list(
__isl_keep isl_schedule *schedule, __isl_keep isl_band *parent,
int band_nr, int *parent_active, int n_active);
for (j = 0; j < band->n; ++j)
band->zero[j] = schedule->node[i].zero[start + j];
- band->map = isl_union_map_empty(isl_dim_copy(schedule->dim));
+ band->map = isl_union_map_empty(isl_space_copy(schedule->dim));
for (i = 0; i < schedule->n; ++i) {
isl_map *map;
unsigned n_out;
return NULL;
if (!schedule->band_forest)
schedule->band_forest = construct_forest(schedule);
- return isl_band_list_copy(schedule->band_forest);
+ return isl_band_list_dup(schedule->band_forest);
}
static __isl_give isl_printer *print_band_list(__isl_take isl_printer *p,