isl_space_unwrap: fix error message
[platform/upstream/isl.git] / isl_schedule.c
index 9697161..358467c 100644 (file)
@@ -11,6 +11,7 @@
 #include <isl_ctx_private.h>
 #include <isl_map_private.h>
 #include <isl_space_private.h>
+#include <isl/aff.h>
 #include <isl/hash.h>
 #include <isl/constraint.h>
 #include <isl/schedule.h>
@@ -141,6 +142,7 @@ enum isl_edge_type {
  * 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
@@ -184,6 +186,7 @@ struct isl_sched_graph {
        struct isl_sched_node *node;
        int n;
        int maxvar;
+       int max_row;
        int n_row;
 
        int *sorted;
@@ -399,6 +402,24 @@ static void graph_remove_edge(struct isl_sched_graph *graph,
        }
 }
 
+/* Check whether the dependence graph has any edge
+ * between the given two nodes.
+ */
+static int graph_has_any_edge(struct isl_sched_graph *graph,
+       struct isl_sched_node *src, struct isl_sched_node *dst)
+{
+       int i;
+       int r;
+
+       for (i = 0; i <= isl_edge_last; ++i) {
+               r = graph_has_edge(graph, i, src, dst);
+               if (r < 0 || r)
+                       return r;
+       }
+
+       return r;
+}
+
 /* Check whether the dependence graph has a validity edge
  * between the given two nodes.
  */
@@ -466,6 +487,43 @@ static void graph_free(isl_ctx *ctx, struct isl_sched_graph *graph)
        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_scaled),
+ * 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)
@@ -490,11 +548,11 @@ static int extract_node(__isl_take isl_set *set, void *user)
        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++;
 
@@ -571,20 +629,25 @@ static int extract_edge(__isl_take isl_map *map, void *user)
 }
 
 /* Check whether there is a validity dependence from src to dst,
- * forcing dst to follow src.
+ * forcing dst to follow src (if weak is not set).
+ * If weak is set, then check if there is any dependence from src to dst.
  */
 static int node_follows(struct isl_sched_graph *graph, 
-       struct isl_sched_node *dst, struct isl_sched_node *src)
+       struct isl_sched_node *dst, struct isl_sched_node *src, int weak)
 {
-       return graph_has_validity_edge(graph, src, dst);
+       if (weak)
+               return graph_has_any_edge(graph, src, dst);
+       else
+               return graph_has_validity_edge(graph, src, dst);
 }
 
 /* Perform Tarjan's algorithm for computing the strongly connected components
  * in the dependence graph (only validity edges).
- * If directed is not set, we consider the graph to be undirected and
+ * If weak is set, we consider the graph to be undirected and
  * we effectively compute the (weakly) connected components.
+ * Additionally, we also consider other edges when weak is set.
  */
-static int detect_sccs_tarjan(struct isl_sched_graph *g, int i, int directed)
+static int detect_sccs_tarjan(struct isl_sched_graph *g, int i, int weak)
 {
        int j;
 
@@ -604,18 +667,18 @@ static int detect_sccs_tarjan(struct isl_sched_graph *g, int i, int directed)
                         g->node[j].index > g->node[i].min_index))
                        continue;
                
-               f = node_follows(g, &g->node[i], &g->node[j]);
+               f = node_follows(g, &g->node[i], &g->node[j], weak);
                if (f < 0)
                        return -1;
-               if (!f && !directed) {
-                       f = node_follows(g, &g->node[j], &g->node[i]);
+               if (!f && weak) {
+                       f = node_follows(g, &g->node[j], &g->node[i], weak);
                        if (f < 0)
                                return -1;
                }
                if (!f)
                        continue;
                if (g->node[j].index < 0) {
-                       detect_sccs_tarjan(g, j, directed);
+                       detect_sccs_tarjan(g, j, weak);
                        if (g->node[j].min_index < g->node[i].min_index)
                                g->node[i].min_index = g->node[j].min_index;
                } else if (g->node[j].index < g->node[i].min_index)
@@ -635,7 +698,7 @@ static int detect_sccs_tarjan(struct isl_sched_graph *g, int i, int directed)
        return 0;
 }
 
-static int detect_ccs(struct isl_sched_graph *graph, int directed)
+static int detect_ccs(struct isl_sched_graph *graph, int weak)
 {
        int i;
 
@@ -648,7 +711,7 @@ static int detect_ccs(struct isl_sched_graph *graph, int directed)
        for (i = graph->n - 1; i >= 0; --i) {
                if (graph->node[i].index >= 0)
                        continue;
-               if (detect_sccs_tarjan(graph, i, directed) < 0)
+               if (detect_sccs_tarjan(graph, i, weak) < 0)
                        return -1;
        }
 
@@ -660,7 +723,7 @@ static int detect_ccs(struct isl_sched_graph *graph, int directed)
  */
 static int detect_sccs(struct isl_sched_graph *graph)
 {
-       return detect_ccs(graph, 1);
+       return detect_ccs(graph, 0);
 }
 
 /* Apply Tarjan's algorithm to detect the (weakly) connected components
@@ -668,7 +731,7 @@ static int detect_sccs(struct isl_sched_graph *graph)
  */
 static int detect_wccs(struct isl_sched_graph *graph)
 {
-       return detect_ccs(graph, 0);
+       return detect_ccs(graph, 1);
 }
 
 static int cmp_scc(const void *a, const void *b, void *data)
@@ -1540,58 +1603,65 @@ error:
        return -1;
 }
 
-/* Convert node->sched into a map and return this map.
- * We simply add equality constraints that express each output variable
- * as the affine combination of parameters and input variables specified
- * by the schedule matrix.
- *
- * The result is cached in node->sched_map, which needs to be released
- * whenever node->sched is updated.
+/* Convert node->sched into a multi_aff and return this multi_aff.
  */
-static __isl_give isl_map *node_extract_schedule(struct isl_sched_node *node)
+static __isl_give isl_multi_aff *node_extract_schedule_multi_aff(
+       struct isl_sched_node *node)
 {
        int i, j;
-       isl_space *dim;
+       isl_space *space;
        isl_local_space *ls;
-       isl_basic_map *bmap;
-       isl_constraint *c;
+       isl_aff *aff;
+       isl_multi_aff *ma;
        int nrow, ncol;
        isl_int v;
 
-       if (node->sched_map)
-               return isl_map_copy(node->sched_map);
-
        nrow = isl_mat_rows(node->sched);
        ncol = isl_mat_cols(node->sched) - 1;
-       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);
+       space = isl_space_from_domain(isl_space_copy(node->dim));
+       space = isl_space_add_dims(space, isl_dim_out, nrow);
+       ma = isl_multi_aff_zero(space);
+       ls = isl_local_space_from_space(isl_space_copy(node->dim));
 
        isl_int_init(v);
 
        for (i = 0; i < nrow; ++i) {
-               c = isl_equality_alloc(isl_local_space_copy(ls));
-               isl_constraint_set_coefficient_si(c, isl_dim_out, i, -1);
+               aff = isl_aff_zero_on_domain(isl_local_space_copy(ls));
                isl_mat_get_element(node->sched, i, 0, &v);
-               isl_constraint_set_constant(c, v);
+               aff = isl_aff_set_constant(aff, v);
                for (j = 0; j < node->nparam; ++j) {
                        isl_mat_get_element(node->sched, i, 1 + j, &v);
-                       isl_constraint_set_coefficient(c, isl_dim_param, j, v);
+                       aff = isl_aff_set_coefficient(aff, isl_dim_param, j, v);
                }
                for (j = 0; j < node->nvar; ++j) {
                        isl_mat_get_element(node->sched,
                                            i, 1 + node->nparam + j, &v);
-                       isl_constraint_set_coefficient(c, isl_dim_in, j, v);
+                       aff = isl_aff_set_coefficient(aff, isl_dim_in, j, v);
                }
-               bmap = isl_basic_map_add_constraint(bmap, c);
+               ma = isl_multi_aff_set_aff(ma, i, aff);
        }
 
        isl_int_clear(v);
 
        isl_local_space_free(ls);
 
-       node->sched_map = isl_map_from_basic_map(bmap);
+       return ma;
+}
+
+/* Convert node->sched into a map and return this map.
+ *
+ * The result is cached in node->sched_map, which needs to be released
+ * whenever node->sched is updated.
+ */
+static __isl_give isl_map *node_extract_schedule(struct isl_sched_node *node)
+{
+       if (!node->sched_map) {
+               isl_multi_aff *ma;
+
+               ma = node_extract_schedule_multi_aff(node);
+               node->sched_map = isl_map_from_multi_aff(ma);
+       }
+
        return isl_map_copy(node->sched_map);
 }
 
@@ -1716,7 +1786,8 @@ static __isl_give isl_schedule *extract_schedule(struct isl_sched_graph *graph,
                band_end = isl_alloc_array(ctx, int, graph->n_band);
                band_id = isl_alloc_array(ctx, int, graph->n_band);
                zero = isl_alloc_array(ctx, int, graph->n_total_row);
-               sched->node[i].sched = node_extract_schedule(&graph->node[i]);
+               sched->node[i].sched =
+                       node_extract_schedule_multi_aff(&graph->node[i]);
                sched->node[i].band_end = band_end;
                sched->node[i].band_id = band_id;
                sched->node[i].zero = zero;
@@ -2667,7 +2738,8 @@ static int compute_component_schedule(isl_ctx *ctx,
        int n_total_row, orig_total_row;
        int n_band, orig_band;
 
-       if (ctx->opt->schedule_fuse == ISL_SCHEDULE_FUSE_MIN)
+       if (ctx->opt->schedule_fuse == ISL_SCHEDULE_FUSE_MIN ||
+           ctx->opt->schedule_separate_components)
                split_on_scc(graph);
 
        n_total_row = 0;
@@ -2764,6 +2836,8 @@ __isl_give isl_schedule *isl_union_set_compute_schedule(
        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)
@@ -2813,7 +2887,7 @@ void *isl_schedule_free(__isl_take isl_schedule *sched)
                return NULL;
 
        for (i = 0; i < sched->n; ++i) {
-               isl_map_free(sched->node[i].sched);
+               isl_multi_aff_free(sched->node[i].sched);
                free(sched->node[i].band_end);
                free(sched->node[i].band_id);
                free(sched->node[i].zero);
@@ -2829,6 +2903,11 @@ isl_ctx *isl_schedule_get_ctx(__isl_keep isl_schedule *schedule)
        return schedule ? isl_space_get_ctx(schedule->dim) : NULL;
 }
 
+/* Return an isl_union_map of the schedule.  If we have already constructed
+ * a band forest, then this band forest may have been modified so we need
+ * to extract the isl_union_map from the forest rather than from
+ * the originally computed schedule.
+ */
 __isl_give isl_union_map *isl_schedule_get_map(__isl_keep isl_schedule *sched)
 {
        int i;
@@ -2837,10 +2916,16 @@ __isl_give isl_union_map *isl_schedule_get_map(__isl_keep isl_schedule *sched)
        if (!sched)
                return NULL;
 
+       if (sched->band_forest)
+               return isl_band_list_get_suffix_schedule(sched->band_forest);
+
        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));
+       for (i = 0; i < sched->n; ++i) {
+               isl_multi_aff *ma;
+
+               ma = isl_multi_aff_copy(sched->node[i].sched);
+               umap = isl_union_map_add_map(umap, isl_map_from_multi_aff(ma));
+       }
 
        return umap;
 }
@@ -2867,11 +2952,10 @@ static __isl_give isl_band *construct_band(__isl_keep isl_schedule *schedule,
        isl_band *band;
        unsigned start, end;
 
-       band = isl_calloc_type(ctx, isl_band);
+       band = isl_band_alloc(ctx);
        if (!band)
                return NULL;
 
-       band->ref = 1;
        band->schedule = schedule;
        band->parent = parent;
 
@@ -2906,22 +2990,24 @@ static __isl_give isl_band *construct_band(__isl_keep isl_schedule *schedule,
        for (j = 0; j < band->n; ++j)
                band->zero[j] = schedule->node[i].zero[start + j];
 
-       band->map = isl_union_map_empty(isl_space_copy(schedule->dim));
+       band->pma = isl_union_pw_multi_aff_empty(isl_space_copy(schedule->dim));
        for (i = 0; i < schedule->n; ++i) {
-               isl_map *map;
+               isl_multi_aff *ma;
+               isl_pw_multi_aff *pma;
                unsigned n_out;
 
                if (!active[i])
                        continue;
 
-               map = isl_map_copy(schedule->node[i].sched);
-               n_out = isl_map_dim(map, isl_dim_out);
-               map = isl_map_project_out(map, isl_dim_out, end, n_out - end);
-               map = isl_map_project_out(map, isl_dim_out, 0, start);
-               band->map = isl_union_map_union(band->map,
-                                               isl_union_map_from_map(map));
+               ma = isl_multi_aff_copy(schedule->node[i].sched);
+               n_out = isl_multi_aff_dim(ma, isl_dim_out);
+               ma = isl_multi_aff_drop_dims(ma, isl_dim_out, end, n_out - end);
+               ma = isl_multi_aff_drop_dims(ma, isl_dim_out, 0, start);
+               pma = isl_pw_multi_aff_from_multi_aff(ma);
+               band->pma = isl_union_pw_multi_aff_add_pw_multi_aff(band->pma,
+                                                                   pma);
        }
-       if (!band->map)
+       if (!band->pma)
                goto error;
 
        return band;
@@ -3054,6 +3140,24 @@ __isl_give isl_band_list *isl_schedule_get_band_forest(
        return isl_band_list_dup(schedule->band_forest);
 }
 
+/* Call "fn" on each band in the schedule in depth-first post-order.
+ */
+int isl_schedule_foreach_band(__isl_keep isl_schedule *sched,
+       int (*fn)(__isl_keep isl_band *band, void *user), void *user)
+{
+       int r;
+       isl_band_list *forest;
+
+       if (!sched)
+               return -1;
+
+       forest = isl_schedule_get_band_forest(sched);
+       r = isl_band_list_foreach_band(forest, fn, user);
+       isl_band_list_free(forest);
+
+       return r;
+}
+
 static __isl_give isl_printer *print_band_list(__isl_take isl_printer *p,
        __isl_keep isl_band_list *list);
 
@@ -3063,7 +3167,7 @@ static __isl_give isl_printer *print_band(__isl_take isl_printer *p,
        isl_band_list *children;
 
        p = isl_printer_start_line(p);
-       p = isl_printer_print_union_map(p, band->map);
+       p = isl_printer_print_union_pw_multi_aff(p, band->pma);
        p = isl_printer_end_line(p);
 
        if (!isl_band_has_children(band))