* ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
*/
-#include <isl_flow.h>
+#include <isl/flow.h>
/* A private structure to keep track of a mapping together with
* a user-specified identifier and a boolean indicating whether
__isl_give isl_access_info *isl_access_info_alloc(__isl_take isl_map *sink,
void *sink_user, isl_access_level_before fn, int max_source)
{
+ isl_ctx *ctx;
struct isl_access_info *acc;
if (!sink)
return NULL;
- isl_assert(sink->ctx, max_source >= 0, goto error);
+ ctx = isl_map_get_ctx(sink);
+ isl_assert(ctx, max_source >= 0, goto error);
- acc = isl_alloc(sink->ctx, struct isl_access_info,
+ acc = isl_alloc(ctx, struct isl_access_info,
sizeof(struct isl_access_info) +
(max_source - 1) * sizeof(struct isl_labeled_map));
if (!acc)
}
/* Free the given isl_access_info structure.
- * This function is static because the user is expected to call
- * isl_access_info_compute_flow on any isl_access_info structure
- * he creates.
*/
-static void isl_access_info_free(__isl_take isl_access_info *acc)
+void isl_access_info_free(__isl_take isl_access_info *acc)
{
int i;
__isl_take isl_access_info *acc, __isl_take isl_map *source,
int must, void *source_user)
{
+ isl_ctx *ctx;
+
if (!acc)
return NULL;
- isl_assert(acc->sink.map->ctx,
- acc->n_must + acc->n_may < acc->max_source, goto error);
+ ctx = isl_map_get_ctx(acc->sink.map);
+ isl_assert(ctx, acc->n_must + acc->n_may < acc->max_source, goto error);
if (must) {
if (acc->n_may)
__isl_take isl_access_info *acc)
{
int i;
+ isl_ctx *ctx;
struct isl_access_sort_info *array;
if (!acc)
if (acc->n_must <= 1)
return acc;
- array = isl_alloc_array(acc->sink.map->ctx,
- struct isl_access_sort_info, acc->n_must);
+ ctx = isl_map_get_ctx(acc->sink.map);
+ array = isl_alloc_array(ctx, struct isl_access_sort_info, acc->n_must);
if (!array)
goto error;
if (!acc)
return NULL;
- ctx = acc->sink.map->ctx;
+ ctx = isl_map_get_ctx(acc->sink.map);
dep = isl_calloc_type(ctx, struct isl_flow);
if (!dep)
return NULL;
res = isl_flow_alloc(acc);
if (!res)
goto error;
- ctx = acc->sink.map->ctx;
+ ctx = isl_map_get_ctx(acc->sink.map);
depth = 2 * isl_map_dim(acc->sink.map, isl_dim_in) + 1;
mustdo = isl_map_domain(isl_map_copy(acc->sink.map));
{
int j;
struct isl_flow *res;
- isl_dim *dim;
- isl_map *id;
- unsigned n_sink;
- unsigned n_data;
+ isl_map *domain_map = NULL;
if (!acc)
return NULL;
- n_sink = isl_map_dim(acc->sink.map, isl_dim_in);
- n_data = isl_map_dim(acc->sink.map, isl_dim_out);
- dim = isl_dim_range(isl_map_get_dim(acc->sink.map));
- id = isl_map_identity(dim);
- id = isl_map_insert(id, isl_dim_in, 0, n_sink);
- acc->sink.map = isl_map_insert(acc->sink.map, isl_dim_in,
- n_sink, n_data);
- acc->sink.map = isl_map_intersect(acc->sink.map, id);
+ domain_map = isl_map_domain_map(isl_map_copy(acc->sink.map));
+ acc->sink.map = isl_map_range_map(acc->sink.map);
if (!acc->sink.map)
goto error;
return NULL;
for (j = 0; j < res->n_source; ++j) {
- res->dep[j].map = isl_map_project_out(res->dep[j].map,
- isl_dim_out, n_sink, n_data);
+ res->dep[j].map = isl_map_apply_range(res->dep[j].map,
+ isl_map_copy(domain_map));
if (!res->dep[j].map)
goto error2;
}
- res->must_no_source = isl_set_project_out(res->must_no_source, isl_dim_set, n_sink, n_data);
- res->may_no_source = isl_set_project_out(res->may_no_source, isl_dim_set, n_sink, n_data);
+ res->must_no_source = isl_set_apply(res->must_no_source,
+ isl_map_copy(domain_map));
+ res->may_no_source = isl_set_apply(res->may_no_source,
+ isl_map_copy(domain_map));
if (!res->must_no_source || !res->may_no_source)
goto error2;
+ isl_map_free(domain_map);
return res;
error:
+ isl_map_free(domain_map);
isl_access_info_free(acc);
return NULL;
error2:
+ isl_map_free(domain_map);
isl_flow_free(res);
return NULL;
}
+
+
+struct isl_compute_flow_data {
+ isl_union_map *must_source;
+ isl_union_map *may_source;
+ isl_union_map *must_dep;
+ isl_union_map *may_dep;
+ isl_union_set *must_no_source;
+ isl_union_set *may_no_source;
+
+ int count;
+ int must;
+ isl_dim *dim;
+ isl_dim *sink_dim;
+ isl_dim **source_dim;
+ isl_access_info *accesses;
+};
+
+static int count_matching_array(__isl_take isl_map *map, void *user)
+{
+ int eq;
+ isl_dim *dim;
+ struct isl_compute_flow_data *data;
+
+ data = (struct isl_compute_flow_data *)user;
+
+ dim = isl_dim_range(isl_map_get_dim(map));
+
+ eq = isl_dim_equal(dim, data->dim);
+
+ isl_dim_free(dim);
+ isl_map_free(map);
+
+ if (eq < 0)
+ return -1;
+ if (eq)
+ data->count++;
+
+ return 0;
+}
+
+static int collect_matching_array(__isl_take isl_map *map, void *user)
+{
+ int eq;
+ isl_dim *dim;
+ struct isl_compute_flow_data *data;
+
+ data = (struct isl_compute_flow_data *)user;
+
+ dim = isl_dim_range(isl_map_get_dim(map));
+
+ eq = isl_dim_equal(dim, data->dim);
+
+ isl_dim_free(dim);
+
+ if (eq < 0)
+ goto error;
+ if (!eq) {
+ isl_map_free(map);
+ return 0;
+ }
+
+ dim = isl_dim_unwrap(isl_dim_domain(isl_map_get_dim(map)));
+ data->source_dim[data->count] = dim;
+
+ data->accesses = isl_access_info_add_source(data->accesses,
+ map, data->must, dim);
+
+ data->count++;
+
+ return 0;
+error:
+ isl_map_free(map);
+ return -1;
+}
+
+static int before(void *first, void *second)
+{
+ isl_dim *dim1 = first;
+ isl_dim *dim2 = second;
+ int n1, n2;
+
+ n1 = isl_dim_size(dim1, isl_dim_in);
+ n2 = isl_dim_size(dim2, isl_dim_in);
+
+ if (n2 < n1)
+ n1 = n2;
+
+ return 2 * n1 + (dim1 < dim2);
+}
+
+/* Given a sink access, look for all the source accesses that access
+ * the same array and perform dataflow analysis on them using
+ * isl_access_info_compute_flow.
+ */
+static int compute_flow(__isl_take isl_map *map, void *user)
+{
+ int i;
+ isl_ctx *ctx;
+ struct isl_compute_flow_data *data;
+ isl_flow *flow;
+
+ data = (struct isl_compute_flow_data *)user;
+
+ ctx = isl_map_get_ctx(map);
+
+ data->accesses = NULL;
+ data->sink_dim = NULL;
+ data->source_dim = NULL;
+ data->count = 0;
+ data->dim = isl_dim_range(isl_map_get_dim(map));
+
+ if (isl_union_map_foreach_map(data->must_source,
+ &count_matching_array, data) < 0)
+ goto error;
+ if (isl_union_map_foreach_map(data->may_source,
+ &count_matching_array, data) < 0)
+ goto error;
+
+ data->sink_dim = isl_dim_unwrap(isl_dim_domain(isl_map_get_dim(map)));
+ data->source_dim = isl_calloc_array(ctx, isl_dim *, data->count);
+
+ data->accesses = isl_access_info_alloc(isl_map_copy(map),
+ data->sink_dim, &before, data->count);
+ data->count = 0;
+ data->must = 1;
+ if (isl_union_map_foreach_map(data->must_source,
+ &collect_matching_array, data) < 0)
+ goto error;
+ data->must = 0;
+ if (isl_union_map_foreach_map(data->may_source,
+ &collect_matching_array, data) < 0)
+ goto error;
+
+ flow = isl_access_info_compute_flow(data->accesses);
+ data->accesses = NULL;
+
+ if (!flow)
+ goto error;
+
+ data->must_no_source = isl_union_set_union(data->must_no_source,
+ isl_union_set_from_set(isl_set_copy(flow->must_no_source)));
+ data->may_no_source = isl_union_set_union(data->may_no_source,
+ isl_union_set_from_set(isl_set_copy(flow->may_no_source)));
+
+ for (i = 0; i < flow->n_source; ++i) {
+ isl_union_map *dep;
+ dep = isl_union_map_from_map(isl_map_copy(flow->dep[i].map));
+ if (flow->dep[i].must)
+ data->must_dep = isl_union_map_union(data->must_dep, dep);
+ else
+ data->may_dep = isl_union_map_union(data->may_dep, dep);
+ }
+
+ isl_flow_free(flow);
+
+ isl_dim_free(data->sink_dim);
+ if (data->source_dim) {
+ for (i = 0; i < data->count; ++i)
+ isl_dim_free(data->source_dim[i]);
+ free(data->source_dim);
+ }
+ isl_dim_free(data->dim);
+ isl_map_free(map);
+
+ return 0;
+error:
+ isl_access_info_free(data->accesses);
+ isl_dim_free(data->sink_dim);
+ if (data->source_dim) {
+ for (i = 0; i < data->count; ++i)
+ isl_dim_free(data->source_dim[i]);
+ free(data->source_dim);
+ }
+ isl_dim_free(data->dim);
+ isl_map_free(map);
+
+ return -1;
+}
+
+/* Given a collection of "sink" and "source" accesses,
+ * compute for each iteration of a sink access
+ * and for each element accessed by that iteration,
+ * the source access in the list that last accessed the
+ * element accessed by the sink access before this sink access.
+ * Each access is given as a map from the loop iterators
+ * to the array indices.
+ * The result is a relations between source and sink
+ * iterations and a subset of the domain of the sink accesses,
+ * corresponding to those iterations that access an element
+ * not previously accessed.
+ *
+ * We first prepend the schedule dimensions to the domain
+ * of the accesses so that we can easily compare their relative order.
+ * Then we consider each sink access individually in compute_flow.
+ */
+int isl_union_map_compute_flow(__isl_take isl_union_map *sink,
+ __isl_take isl_union_map *must_source,
+ __isl_take isl_union_map *may_source,
+ __isl_take isl_union_map *schedule,
+ __isl_give isl_union_map **must_dep, __isl_give isl_union_map **may_dep,
+ __isl_give isl_union_set **must_no_source,
+ __isl_give isl_union_set **may_no_source)
+{
+ isl_dim *dim;
+ isl_union_map *range_map = NULL;
+ struct isl_compute_flow_data data;
+
+ sink = isl_union_map_align_params(sink,
+ isl_union_map_get_dim(must_source));
+ sink = isl_union_map_align_params(sink,
+ isl_union_map_get_dim(may_source));
+ sink = isl_union_map_align_params(sink,
+ isl_union_map_get_dim(schedule));
+ dim = isl_union_map_get_dim(sink);
+ must_source = isl_union_map_align_params(must_source, isl_dim_copy(dim));
+ may_source = isl_union_map_align_params(may_source, isl_dim_copy(dim));
+ schedule = isl_union_map_align_params(schedule, isl_dim_copy(dim));
+
+ schedule = isl_union_map_reverse(schedule);
+ range_map = isl_union_map_range_map(schedule);
+ schedule = isl_union_map_reverse(isl_union_map_copy(range_map));
+ sink = isl_union_map_apply_domain(sink, isl_union_map_copy(schedule));
+ must_source = isl_union_map_apply_domain(must_source,
+ isl_union_map_copy(schedule));
+ may_source = isl_union_map_apply_domain(may_source, schedule);
+
+ data.must_source = must_source;
+ data.may_source = may_source;
+ data.must_dep = must_dep ?
+ isl_union_map_empty(isl_dim_copy(dim)) : NULL;
+ data.may_dep = may_dep ? isl_union_map_empty(isl_dim_copy(dim)) : NULL;
+ data.must_no_source = must_no_source ?
+ isl_union_set_empty(isl_dim_copy(dim)) : NULL;
+ data.may_no_source = may_no_source ?
+ isl_union_set_empty(isl_dim_copy(dim)) : NULL;
+
+ isl_dim_free(dim);
+
+ if (isl_union_map_foreach_map(sink, &compute_flow, &data) < 0)
+ goto error;
+
+ isl_union_map_free(sink);
+ isl_union_map_free(must_source);
+ isl_union_map_free(may_source);
+
+ if (must_dep) {
+ data.must_dep = isl_union_map_apply_domain(data.must_dep,
+ isl_union_map_copy(range_map));
+ data.must_dep = isl_union_map_apply_range(data.must_dep,
+ isl_union_map_copy(range_map));
+ *must_dep = data.must_dep;
+ }
+ if (may_dep) {
+ data.may_dep = isl_union_map_apply_domain(data.may_dep,
+ isl_union_map_copy(range_map));
+ data.may_dep = isl_union_map_apply_range(data.may_dep,
+ isl_union_map_copy(range_map));
+ *may_dep = data.may_dep;
+ }
+ if (must_no_source) {
+ data.must_no_source = isl_union_set_apply(data.must_no_source,
+ isl_union_map_copy(range_map));
+ *must_no_source = data.must_no_source;
+ }
+ if (may_no_source) {
+ data.may_no_source = isl_union_set_apply(data.may_no_source,
+ isl_union_map_copy(range_map));
+ *may_no_source = data.may_no_source;
+ }
+
+ isl_union_map_free(range_map);
+
+ return 0;
+error:
+ isl_union_map_free(range_map);
+ isl_union_map_free(sink);
+ isl_union_map_free(must_source);
+ isl_union_map_free(may_source);
+ isl_union_map_free(data.must_dep);
+ isl_union_map_free(data.may_dep);
+ isl_union_set_free(data.must_no_source);
+ isl_union_set_free(data.may_no_source);
+
+ if (must_dep)
+ *must_dep = NULL;
+ if (may_dep)
+ *may_dep = NULL;
+ if (must_no_source)
+ *must_no_source = NULL;
+ if (may_no_source)
+ *may_no_source = NULL;
+ return -1;
+}