#include <isl/val.h>
#include <isl/space.h>
#include <isl/set.h>
#include <isl/map.h>
#include <isl/union_set.h>
#include <isl/union_map.h>
#include <isl/flow.h>
#include <isl/schedule_node.h>
#include <isl_sort.h>
#include <isl/stream.h>
enum isl_restriction_type {
isl_restriction_type_empty,
isl_restriction_type_none,
isl_restriction_type_input,
isl_restriction_type_output
};
struct isl_restriction {
enum isl_restriction_type type;
isl_set *source;
isl_set *sink;
};
static __isl_give isl_restriction *isl_restriction_alloc(
__isl_take isl_map *source_map, enum isl_restriction_type type)
{
isl_ctx *ctx;
isl_restriction *restr;
if (!source_map)
return NULL;
ctx = isl_map_get_ctx(source_map);
restr = isl_calloc_type(ctx, struct isl_restriction);
if (!restr)
goto error;
restr->type = type;
isl_map_free(source_map);
return restr;
error:
isl_map_free(source_map);
return NULL;
}
__isl_give isl_restriction *isl_restriction_none(__isl_take isl_map *source_map)
{
return isl_restriction_alloc(source_map, isl_restriction_type_none);
}
__isl_give isl_restriction *isl_restriction_empty(
__isl_take isl_map *source_map)
{
return isl_restriction_alloc(source_map, isl_restriction_type_empty);
}
__isl_give isl_restriction *isl_restriction_input(
__isl_take isl_set *source_restr, __isl_take isl_set *sink_restr)
{
isl_ctx *ctx;
isl_restriction *restr;
if (!source_restr || !sink_restr)
goto error;
ctx = isl_set_get_ctx(source_restr);
restr = isl_calloc_type(ctx, struct isl_restriction);
if (!restr)
goto error;
restr->type = isl_restriction_type_input;
restr->source = source_restr;
restr->sink = sink_restr;
return restr;
error:
isl_set_free(source_restr);
isl_set_free(sink_restr);
return NULL;
}
__isl_give isl_restriction *isl_restriction_output(
__isl_take isl_set *source_restr)
{
isl_ctx *ctx;
isl_restriction *restr;
if (!source_restr)
return NULL;
ctx = isl_set_get_ctx(source_restr);
restr = isl_calloc_type(ctx, struct isl_restriction);
if (!restr)
goto error;
restr->type = isl_restriction_type_output;
restr->source = source_restr;
return restr;
error:
isl_set_free(source_restr);
return NULL;
}
__isl_null isl_restriction *isl_restriction_free(
__isl_take isl_restriction *restr)
{
if (!restr)
return NULL;
isl_set_free(restr->source);
isl_set_free(restr->sink);
free(restr);
return NULL;
}
isl_ctx *isl_restriction_get_ctx(__isl_keep isl_restriction *restr)
{
return restr ? isl_set_get_ctx(restr->source) : NULL;
}
struct isl_labeled_map {
struct isl_map *map;
void *data;
int must;
};
typedef isl_bool (*isl_access_coscheduled)(void *first, void *second);
struct isl_access_info {
isl_map *domain_map;
struct isl_labeled_map sink;
isl_access_level_before level_before;
isl_access_coscheduled coscheduled;
isl_access_restrict restrict_fn;
void *restrict_user;
int max_source;
int n_must;
int n_may;
struct isl_labeled_map source[1];
};
struct isl_flow {
isl_set *must_no_source;
isl_set *may_no_source;
int n_source;
struct isl_labeled_map *dep;
};
__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;
ctx = isl_map_get_ctx(sink);
isl_assert(ctx, max_source >= 0, goto error);
acc = isl_calloc(ctx, struct isl_access_info,
sizeof(struct isl_access_info) +
(max_source - 1) * sizeof(struct isl_labeled_map));
if (!acc)
goto error;
acc->sink.map = sink;
acc->sink.data = sink_user;
acc->level_before = fn;
acc->max_source = max_source;
acc->n_must = 0;
acc->n_may = 0;
return acc;
error:
isl_map_free(sink);
return NULL;
}
__isl_null isl_access_info *isl_access_info_free(
__isl_take isl_access_info *acc)
{
int i;
if (!acc)
return NULL;
isl_map_free(acc->domain_map);
isl_map_free(acc->sink.map);
for (i = 0; i < acc->n_must + acc->n_may; ++i)
isl_map_free(acc->source[i].map);
free(acc);
return NULL;
}
isl_ctx *isl_access_info_get_ctx(__isl_keep isl_access_info *acc)
{
return acc ? isl_map_get_ctx(acc->sink.map) : NULL;
}
__isl_give isl_access_info *isl_access_info_set_restrict(
__isl_take isl_access_info *acc, isl_access_restrict fn, void *user)
{
if (!acc)
return NULL;
acc->restrict_fn = fn;
acc->restrict_user = user;
return acc;
}
__isl_give isl_access_info *isl_access_info_add_source(
__isl_take isl_access_info *acc, __isl_take isl_map *source,
int must, void *source_user)
{
isl_ctx *ctx;
if (!acc)
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)
acc->source[acc->n_must + acc->n_may] =
acc->source[acc->n_must];
acc->source[acc->n_must].map = source;
acc->source[acc->n_must].data = source_user;
acc->source[acc->n_must].must = 1;
acc->n_must++;
} else {
acc->source[acc->n_must + acc->n_may].map = source;
acc->source[acc->n_must + acc->n_may].data = source_user;
acc->source[acc->n_must + acc->n_may].must = 0;
acc->n_may++;
}
return acc;
error:
isl_map_free(source);
isl_access_info_free(acc);
return NULL;
}
struct access_sort_info {
isl_access_info *access_info;
int error;
};
static int access_sort_cmp(const void *p1, const void *p2, void *user)
{
struct access_sort_info *sort_info = user;
isl_access_info *acc = sort_info->access_info;
if (sort_info->error)
return 0;
const struct isl_labeled_map *i1, *i2;
int level1, level2;
uint32_t h1, h2;
i1 = (const struct isl_labeled_map *) p1;
i2 = (const struct isl_labeled_map *) p2;
level1 = acc->level_before(i1->data, i2->data);
if (level1 < 0)
goto error;
if (level1 % 2)
return -1;
level2 = acc->level_before(i2->data, i1->data);
if (level2 < 0)
goto error;
if (level2 % 2)
return 1;
h1 = isl_map_get_hash(i1->map);
h2 = isl_map_get_hash(i2->map);
return h1 > h2 ? 1 : h1 < h2 ? -1 : 0;
error:
sort_info->error = 1;
return 0;
}
static __isl_give isl_access_info *isl_access_info_sort_sources(
__isl_take isl_access_info *acc)
{
struct access_sort_info sort_info;
sort_info.access_info = acc;
sort_info.error = 0;
if (!acc)
return NULL;
if (acc->n_must <= 1)
return acc;
if (isl_sort(acc->source, acc->n_must, sizeof(struct isl_labeled_map),
access_sort_cmp, &sort_info) < 0)
return isl_access_info_free(acc);
if (sort_info.error)
return isl_access_info_free(acc);
return acc;
}
static __isl_give isl_space *space_align_and_join(__isl_take isl_space *left,
__isl_take isl_space *right)
{
isl_bool equal_params;
equal_params = isl_space_has_equal_params(left, right);
if (equal_params < 0)
goto error;
if (equal_params)
return isl_space_join(left, right);
left = isl_space_align_params(left, isl_space_copy(right));
right = isl_space_align_params(right, isl_space_copy(left));
return isl_space_join(left, right);
error:
isl_space_free(left);
isl_space_free(right);
return NULL;
}
static __isl_give isl_flow *isl_flow_alloc(__isl_keep isl_access_info *acc)
{
int i, n;
struct isl_ctx *ctx;
struct isl_flow *dep;
if (!acc)
return NULL;
ctx = isl_map_get_ctx(acc->sink.map);
dep = isl_calloc_type(ctx, struct isl_flow);
if (!dep)
return NULL;
n = 2 * acc->n_must + acc->n_may;
dep->dep = isl_calloc_array(ctx, struct isl_labeled_map, n);
if (n && !dep->dep)
goto error;
dep->n_source = n;
for (i = 0; i < acc->n_must; ++i) {
isl_space *space;
space = space_align_and_join(
isl_map_get_space(acc->source[i].map),
isl_space_reverse(isl_map_get_space(acc->sink.map)));
dep->dep[2 * i].map = isl_map_empty(space);
dep->dep[2 * i + 1].map = isl_map_copy(dep->dep[2 * i].map);
dep->dep[2 * i].data = acc->source[i].data;
dep->dep[2 * i + 1].data = acc->source[i].data;
dep->dep[2 * i].must = 1;
dep->dep[2 * i + 1].must = 0;
if (!dep->dep[2 * i].map || !dep->dep[2 * i + 1].map)
goto error;
}
for (i = acc->n_must; i < acc->n_must + acc->n_may; ++i) {
isl_space *space;
space = space_align_and_join(
isl_map_get_space(acc->source[i].map),
isl_space_reverse(isl_map_get_space(acc->sink.map)));
dep->dep[acc->n_must + i].map = isl_map_empty(space);
dep->dep[acc->n_must + i].data = acc->source[i].data;
dep->dep[acc->n_must + i].must = 0;
if (!dep->dep[acc->n_must + i].map)
goto error;
}
return dep;
error:
isl_flow_free(dep);
return NULL;
}
isl_stat isl_flow_foreach(__isl_keep isl_flow *deps,
isl_stat (*fn)(__isl_take isl_map *dep, int must, void *dep_user,
void *user),
void *user)
{
int i;
if (!deps)
return isl_stat_error;
for (i = 0; i < deps->n_source; ++i) {
if (isl_map_plain_is_empty(deps->dep[i].map))
continue;
if (fn(isl_map_copy(deps->dep[i].map), deps->dep[i].must,
deps->dep[i].data, user) < 0)
return isl_stat_error;
}
return isl_stat_ok;
}
__isl_give isl_map *isl_flow_get_no_source(__isl_keep isl_flow *deps, int must)
{
if (!deps)
return NULL;
if (must)
return isl_set_unwrap(isl_set_copy(deps->must_no_source));
else
return isl_set_unwrap(isl_set_copy(deps->may_no_source));
}
__isl_null isl_flow *isl_flow_free(__isl_take isl_flow *deps)
{
int i;
if (!deps)
return NULL;
isl_set_free(deps->must_no_source);
isl_set_free(deps->may_no_source);
if (deps->dep) {
for (i = 0; i < deps->n_source; ++i)
isl_map_free(deps->dep[i].map);
free(deps->dep);
}
free(deps);
return NULL;
}
isl_ctx *isl_flow_get_ctx(__isl_keep isl_flow *deps)
{
return deps ? isl_set_get_ctx(deps->must_no_source) : NULL;
}
static __isl_give isl_map *after_at_level(__isl_take isl_space *space,
int level)
{
struct isl_basic_map *bmap;
if (level % 2)
bmap = isl_basic_map_equal(space, level/2);
else
bmap = isl_basic_map_more_at(space, level/2 - 1);
return isl_map_from_basic_map(bmap);
}
static __isl_give isl_map *restricted_partial_lexmax(
__isl_keep isl_access_info *acc, __isl_take isl_map *dep,
int source, __isl_take isl_set *sink, __isl_give isl_set **empty)
{
isl_map *source_map;
isl_restriction *restr;
isl_set *sink_domain;
isl_set *sink_restr;
isl_map *res;
if (!acc->restrict_fn)
return isl_map_partial_lexmax(dep, sink, empty);
source_map = isl_map_copy(dep);
source_map = isl_map_apply_domain(source_map,
isl_map_copy(acc->domain_map));
sink_domain = isl_set_copy(sink);
sink_domain = isl_set_apply(sink_domain, isl_map_copy(acc->domain_map));
restr = acc->restrict_fn(source_map, sink_domain,
acc->source[source].data, acc->restrict_user);
isl_set_free(sink_domain);
isl_map_free(source_map);
if (!restr)
goto error;
if (restr->type == isl_restriction_type_input) {
dep = isl_map_intersect_range(dep, isl_set_copy(restr->source));
sink_restr = isl_set_copy(restr->sink);
sink_restr = isl_set_apply(sink_restr,
isl_map_reverse(isl_map_copy(acc->domain_map)));
sink = isl_set_intersect(sink, sink_restr);
} else if (restr->type == isl_restriction_type_empty) {
isl_space *space = isl_map_get_space(dep);
isl_map_free(dep);
dep = isl_map_empty(space);
}
res = isl_map_partial_lexmax(dep, sink, empty);
if (restr->type == isl_restriction_type_output)
res = isl_map_intersect_range(res, isl_set_copy(restr->source));
isl_restriction_free(restr);
return res;
error:
isl_map_free(dep);
isl_set_free(sink);
*empty = NULL;
return NULL;
}
static struct isl_map *last_source(struct isl_access_info *acc,
struct isl_set *set_C,
int j, int level, struct isl_set **empty)
{
struct isl_map *read_map;
struct isl_map *write_map;
struct isl_map *dep_map;
struct isl_map *after;
struct isl_map *result;
read_map = isl_map_copy(acc->sink.map);
write_map = isl_map_copy(acc->source[j].map);
write_map = isl_map_reverse(write_map);
dep_map = isl_map_apply_range(read_map, write_map);
after = after_at_level(isl_map_get_space(dep_map), level);
dep_map = isl_map_intersect(dep_map, after);
result = restricted_partial_lexmax(acc, dep_map, j, set_C, empty);
result = isl_map_reverse(result);
return result;
}
static struct isl_map *last_later_source(struct isl_access_info *acc,
struct isl_map *old_map,
int j, int before_level,
int k, int after_level,
struct isl_set **empty)
{
isl_space *space;
struct isl_set *set_C;
struct isl_map *read_map;
struct isl_map *write_map;
struct isl_map *dep_map;
struct isl_map *after_write;
struct isl_map *before_read;
struct isl_map *result;
set_C = isl_map_range(isl_map_copy(old_map));
read_map = isl_map_copy(acc->sink.map);
write_map = isl_map_copy(acc->source[k].map);
write_map = isl_map_reverse(write_map);
dep_map = isl_map_apply_range(read_map, write_map);
space = space_align_and_join(isl_map_get_space(acc->source[k].map),
isl_space_reverse(isl_map_get_space(acc->source[j].map)));
after_write = after_at_level(space, after_level);
after_write = isl_map_apply_range(after_write, old_map);
after_write = isl_map_reverse(after_write);
dep_map = isl_map_intersect(dep_map, after_write);
before_read = after_at_level(isl_map_get_space(dep_map), before_level);
dep_map = isl_map_intersect(dep_map, before_read);
result = restricted_partial_lexmax(acc, dep_map, k, set_C, empty);
result = isl_map_reverse(result);
return result;
}
static int can_precede_at_level(int shared_level, int target_level)
{
if (shared_level < target_level)
return 0;
if ((target_level % 2) && shared_level > target_level)
return 0;
return 1;
}
static isl_stat intermediate_sources(__isl_keep isl_access_info *acc,
struct isl_map **temp_rel, int j, int sink_level)
{
int k, level;
isl_size n_in = isl_map_dim(acc->source[j].map, isl_dim_in);
int depth = 2 * n_in + 1;
if (n_in < 0)
return isl_stat_error;
if (isl_map_plain_is_empty(temp_rel[j]))
return isl_stat_ok;
for (k = j - 1; k >= 0; --k) {
int plevel, plevel2;
plevel = acc->level_before(acc->source[k].data, acc->sink.data);
if (plevel < 0)
return isl_stat_error;
if (!can_precede_at_level(plevel, sink_level))
continue;
plevel2 = acc->level_before(acc->source[j].data,
acc->source[k].data);
if (plevel2 < 0)
return isl_stat_error;
for (level = sink_level; level <= depth; ++level) {
struct isl_map *T;
struct isl_set *trest;
struct isl_map *copy;
if (!can_precede_at_level(plevel2, level))
continue;
copy = isl_map_copy(temp_rel[j]);
T = last_later_source(acc, copy, j, sink_level, k,
level, &trest);
if (isl_map_plain_is_empty(T)) {
isl_set_free(trest);
isl_map_free(T);
continue;
}
temp_rel[j] = isl_map_intersect_range(temp_rel[j], trest);
temp_rel[k] = isl_map_union_disjoint(temp_rel[k], T);
}
}
return isl_stat_ok;
}
static __isl_give isl_map *all_sources(__isl_keep isl_access_info *acc,
__isl_take isl_set *set_C, int j, int level)
{
isl_map *read_map;
isl_map *write_map;
isl_map *dep_map;
isl_map *after;
read_map = isl_map_copy(acc->sink.map);
read_map = isl_map_intersect_domain(read_map, set_C);
write_map = isl_map_copy(acc->source[acc->n_must + j].map);
write_map = isl_map_reverse(write_map);
dep_map = isl_map_apply_range(read_map, write_map);
after = after_at_level(isl_map_get_space(dep_map), level);
dep_map = isl_map_intersect(dep_map, after);
return isl_map_reverse(dep_map);
}
static __isl_give isl_map *all_later_sources(__isl_keep isl_access_info *acc,
__isl_take isl_map *old_map,
int j, int before_level, int k, int after_level)
{
isl_space *space;
isl_set *set_C;
isl_map *read_map;
isl_map *write_map;
isl_map *dep_map;
isl_map *after_write;
isl_map *before_read;
set_C = isl_map_range(isl_map_copy(old_map));
read_map = isl_map_copy(acc->sink.map);
read_map = isl_map_intersect_domain(read_map, set_C);
write_map = isl_map_copy(acc->source[acc->n_must + j].map);
write_map = isl_map_reverse(write_map);
dep_map = isl_map_apply_range(read_map, write_map);
space = isl_space_join(isl_map_get_space(
acc->source[acc->n_must + j].map),
isl_space_reverse(isl_map_get_space(acc->source[k].map)));
after_write = after_at_level(space, after_level);
after_write = isl_map_apply_range(after_write, old_map);
after_write = isl_map_reverse(after_write);
dep_map = isl_map_intersect(dep_map, after_write);
before_read = after_at_level(isl_map_get_space(dep_map), before_level);
dep_map = isl_map_intersect(dep_map, before_read);
return isl_map_reverse(dep_map);
}
static __isl_give isl_map *all_intermediate_sources(
__isl_keep isl_access_info *acc, __isl_take isl_map *map,
struct isl_map **must_rel, struct isl_map **may_rel,
int j, int sink_level)
{
int k, level;
isl_size n_in = isl_map_dim(acc->source[acc->n_must + j].map,
isl_dim_in);
int depth = 2 * n_in + 1;
if (n_in < 0)
return isl_map_free(map);
for (k = 0; k < acc->n_must; ++k) {
int plevel;
if (isl_map_plain_is_empty(may_rel[k]) &&
isl_map_plain_is_empty(must_rel[k]))
continue;
plevel = acc->level_before(acc->source[k].data,
acc->source[acc->n_must + j].data);
if (plevel < 0)
return isl_map_free(map);
for (level = sink_level; level <= depth; ++level) {
isl_map *T;
isl_map *copy;
isl_set *ran;
if (!can_precede_at_level(plevel, level))
continue;
copy = isl_map_copy(may_rel[k]);
T = all_later_sources(acc, copy, j, sink_level, k, level);
map = isl_map_union(map, T);
copy = isl_map_copy(must_rel[k]);
T = all_later_sources(acc, copy, j, sink_level, k, level);
ran = isl_map_range(isl_map_copy(T));
map = isl_map_union(map, T);
may_rel[k] = isl_map_union_disjoint(may_rel[k],
isl_map_intersect_range(isl_map_copy(must_rel[k]),
isl_set_copy(ran)));
T = isl_map_from_domain_and_range(
isl_set_universe(
isl_space_domain(isl_map_get_space(must_rel[k]))),
ran);
must_rel[k] = isl_map_subtract(must_rel[k], T);
}
}
return map;
}
static __isl_give isl_map *coscheduled_source(__isl_keep isl_access_info *acc,
__isl_keep isl_map *old_map, int pos, int depth)
{
isl_space *space;
isl_set *set_C;
isl_map *read_map;
isl_map *write_map;
isl_map *dep_map;
isl_map *equal;
isl_map *map;
if (depth < 0)
return NULL;
set_C = isl_map_range(isl_map_copy(old_map));
read_map = isl_map_copy(acc->sink.map);
read_map = isl_map_intersect_domain(read_map, set_C);
write_map = isl_map_copy(acc->source[pos].map);
dep_map = isl_map_domain_product(write_map, read_map);
dep_map = isl_set_unwrap(isl_map_domain(dep_map));
space = isl_space_join(isl_map_get_space(old_map),
isl_space_reverse(isl_map_get_space(dep_map)));
equal = isl_map_from_basic_map(isl_basic_map_equal(space, depth));
map = isl_map_range_product(equal, isl_map_copy(old_map));
map = isl_map_uncurry(map);
map = isl_map_intersect_domain_factor_range(map, dep_map);
return map;
}
static __isl_give isl_flow *handle_coscheduled(__isl_keep isl_access_info *acc,
__isl_keep isl_map **must_rel, __isl_keep isl_map **may_rel,
__isl_take isl_flow *flow)
{
int i, j;
if (!acc->coscheduled)
return flow;
for (i = acc->n_must - 1; i >= 0; --i) {
isl_map *move;
if (isl_map_plain_is_empty(must_rel[i]))
continue;
move = isl_map_empty(isl_map_get_space(must_rel[i]));
for (j = i - 1; j >= 0; --j) {
int depth;
isl_bool coscheduled;
isl_map *map, *factor;
coscheduled = acc->coscheduled(acc->source[i].data,
acc->source[j].data);
if (coscheduled < 0) {
isl_map_free(move);
return isl_flow_free(flow);
}
if (!coscheduled)
continue;
depth = acc->level_before(acc->source[i].data,
acc->source[j].data) / 2;
map = coscheduled_source(acc, must_rel[i], j, depth);
factor = isl_map_domain_factor_range(isl_map_copy(map));
may_rel[j] = isl_map_union(may_rel[j], factor);
map = isl_map_domain_factor_domain(map);
move = isl_map_union(move, map);
}
for (j = 0; j < acc->n_may; ++j) {
int depth, pos;
isl_bool coscheduled;
isl_map *map, *factor;
pos = acc->n_must + j;
coscheduled = acc->coscheduled(acc->source[i].data,
acc->source[pos].data);
if (coscheduled < 0) {
isl_map_free(move);
return isl_flow_free(flow);
}
if (!coscheduled)
continue;
depth = acc->level_before(acc->source[i].data,
acc->source[pos].data) / 2;
map = coscheduled_source(acc, must_rel[i], pos, depth);
factor = isl_map_domain_factor_range(isl_map_copy(map));
pos = 2 * acc->n_must + j;
flow->dep[pos].map = isl_map_union(flow->dep[pos].map,
factor);
map = isl_map_domain_factor_domain(map);
move = isl_map_union(move, map);
}
must_rel[i] = isl_map_subtract(must_rel[i], isl_map_copy(move));
may_rel[i] = isl_map_union(may_rel[i], move);
}
return flow;
}
static __isl_give isl_flow *compute_mem_based_dependences(
__isl_keep isl_access_info *acc)
{
int i;
isl_set *mustdo;
isl_set *maydo;
isl_flow *res;
res = isl_flow_alloc(acc);
if (!res)
return NULL;
mustdo = isl_map_domain(isl_map_copy(acc->sink.map));
maydo = isl_set_copy(mustdo);
for (i = 0; i < acc->n_may; ++i) {
int plevel;
int is_before;
isl_space *space;
isl_map *before;
isl_map *dep;
plevel = acc->level_before(acc->source[i].data, acc->sink.data);
if (plevel < 0)
goto error;
is_before = plevel & 1;
plevel >>= 1;
space = isl_map_get_space(res->dep[i].map);
if (is_before)
before = isl_map_lex_le_first(space, plevel);
else
before = isl_map_lex_lt_first(space, plevel);
dep = isl_map_apply_range(isl_map_copy(acc->source[i].map),
isl_map_reverse(isl_map_copy(acc->sink.map)));
dep = isl_map_intersect(dep, before);
mustdo = isl_set_subtract(mustdo,
isl_map_range(isl_map_copy(dep)));
res->dep[i].map = isl_map_union(res->dep[i].map, dep);
}
res->may_no_source = isl_set_subtract(maydo, isl_set_copy(mustdo));
res->must_no_source = mustdo;
return res;
error:
isl_set_free(mustdo);
isl_set_free(maydo);
isl_flow_free(res);
return NULL;
}
static __isl_give isl_flow *compute_val_based_dependences(
__isl_keep isl_access_info *acc)
{
isl_ctx *ctx;
isl_flow *res;
isl_set *mustdo = NULL;
isl_set *maydo = NULL;
int level, j;
isl_size n_in;
int depth;
isl_map **must_rel = NULL;
isl_map **may_rel = NULL;
if (!acc)
return NULL;
res = isl_flow_alloc(acc);
if (!res)
goto error;
ctx = isl_map_get_ctx(acc->sink.map);
n_in = isl_map_dim(acc->sink.map, isl_dim_in);
if (n_in < 0)
goto error;
depth = 2 * n_in + 1;
mustdo = isl_map_domain(isl_map_copy(acc->sink.map));
maydo = isl_set_empty(isl_set_get_space(mustdo));
if (!mustdo || !maydo)
goto error;
if (isl_set_plain_is_empty(mustdo))
goto done;
must_rel = isl_calloc_array(ctx, struct isl_map *, acc->n_must);
may_rel = isl_calloc_array(ctx, struct isl_map *, acc->n_must);
if (!must_rel || !may_rel)
goto error;
for (level = depth; level >= 1; --level) {
for (j = acc->n_must-1; j >=0; --j) {
isl_space *space;
space = isl_map_get_space(res->dep[2 * j].map);
must_rel[j] = isl_map_empty(space);
may_rel[j] = isl_map_copy(must_rel[j]);
}
for (j = acc->n_must - 1; j >= 0; --j) {
struct isl_map *T;
struct isl_set *rest;
int plevel;
plevel = acc->level_before(acc->source[j].data,
acc->sink.data);
if (plevel < 0)
goto error;
if (!can_precede_at_level(plevel, level))
continue;
T = last_source(acc, mustdo, j, level, &rest);
must_rel[j] = isl_map_union_disjoint(must_rel[j], T);
mustdo = rest;
if (intermediate_sources(acc, must_rel, j, level) < 0)
goto error;
T = last_source(acc, maydo, j, level, &rest);
may_rel[j] = isl_map_union_disjoint(may_rel[j], T);
maydo = rest;
if (intermediate_sources(acc, may_rel, j, level) < 0)
goto error;
if (isl_set_plain_is_empty(mustdo) &&
isl_set_plain_is_empty(maydo))
break;
}
for (j = j - 1; j >= 0; --j) {
int plevel;
plevel = acc->level_before(acc->source[j].data,
acc->sink.data);
if (plevel < 0)
goto error;
if (!can_precede_at_level(plevel, level))
continue;
if (intermediate_sources(acc, must_rel, j, level) < 0)
goto error;
if (intermediate_sources(acc, may_rel, j, level) < 0)
goto error;
}
res = handle_coscheduled(acc, must_rel, may_rel, res);
if (!res)
goto error;
for (j = 0; j < acc->n_may; ++j) {
int plevel;
isl_map *T;
isl_set *ran;
plevel = acc->level_before(acc->source[acc->n_must + j].data,
acc->sink.data);
if (plevel < 0)
goto error;
if (!can_precede_at_level(plevel, level))
continue;
T = all_sources(acc, isl_set_copy(maydo), j, level);
res->dep[2 * acc->n_must + j].map =
isl_map_union(res->dep[2 * acc->n_must + j].map, T);
T = all_sources(acc, isl_set_copy(mustdo), j, level);
ran = isl_map_range(isl_map_copy(T));
res->dep[2 * acc->n_must + j].map =
isl_map_union(res->dep[2 * acc->n_must + j].map, T);
mustdo = isl_set_subtract(mustdo, isl_set_copy(ran));
maydo = isl_set_union_disjoint(maydo, ran);
T = res->dep[2 * acc->n_must + j].map;
T = all_intermediate_sources(acc, T, must_rel, may_rel,
j, level);
res->dep[2 * acc->n_must + j].map = T;
}
for (j = acc->n_must - 1; j >= 0; --j) {
res->dep[2 * j].map =
isl_map_union_disjoint(res->dep[2 * j].map,
must_rel[j]);
res->dep[2 * j + 1].map =
isl_map_union_disjoint(res->dep[2 * j + 1].map,
may_rel[j]);
}
if (isl_set_plain_is_empty(mustdo) &&
isl_set_plain_is_empty(maydo))
break;
}
free(must_rel);
free(may_rel);
done:
res->must_no_source = mustdo;
res->may_no_source = maydo;
return res;
error:
if (must_rel)
for (j = 0; j < acc->n_must; ++j)
isl_map_free(must_rel[j]);
if (may_rel)
for (j = 0; j < acc->n_must; ++j)
isl_map_free(may_rel[j]);
isl_flow_free(res);
isl_set_free(mustdo);
isl_set_free(maydo);
free(must_rel);
free(may_rel);
return NULL;
}
static __isl_give isl_flow *access_info_compute_flow_core(
__isl_take isl_access_info *acc)
{
struct isl_flow *res = NULL;
if (!acc)
return NULL;
acc->sink.map = isl_map_range_map(acc->sink.map);
if (!acc->sink.map)
goto error;
if (acc->n_must == 0)
res = compute_mem_based_dependences(acc);
else {
acc = isl_access_info_sort_sources(acc);
res = compute_val_based_dependences(acc);
}
acc = isl_access_info_free(acc);
if (!res)
return NULL;
if (!res->must_no_source || !res->may_no_source)
goto error;
return res;
error:
isl_access_info_free(acc);
isl_flow_free(res);
return NULL;
}
__isl_give isl_flow *isl_access_info_compute_flow(__isl_take isl_access_info *acc)
{
int j;
struct isl_flow *res;
if (!acc)
return NULL;
acc->domain_map = isl_map_domain_map(isl_map_copy(acc->sink.map));
res = access_info_compute_flow_core(acc);
if (!res)
return NULL;
for (j = 0; j < res->n_source; ++j) {
res->dep[j].map = isl_map_range_factor_domain(res->dep[j].map);
if (!res->dep[j].map)
goto error;
}
return res;
error:
isl_flow_free(res);
return NULL;
}
struct isl_sched_info {
int *is_cst;
isl_vec *cst;
};
static void sched_info_free(__isl_take struct isl_sched_info *info)
{
if (!info)
return;
isl_vec_free(info->cst);
free(info->is_cst);
free(info);
}
static __isl_give struct isl_sched_info *sched_info_alloc(
__isl_keep isl_map *map)
{
isl_ctx *ctx;
isl_space *space;
struct isl_sched_info *info;
int i;
isl_size n;
if (!map)
return NULL;
space = isl_space_unwrap(isl_space_domain(isl_map_get_space(map)));
if (!space)
return NULL;
n = isl_space_dim(space, isl_dim_in);
isl_space_free(space);
if (n < 0)
return NULL;
ctx = isl_map_get_ctx(map);
info = isl_alloc_type(ctx, struct isl_sched_info);
if (!info)
return NULL;
info->is_cst = isl_alloc_array(ctx, int, n);
info->cst = isl_vec_alloc(ctx, n);
if (n && (!info->is_cst || !info->cst))
goto error;
for (i = 0; i < n; ++i) {
isl_val *v;
v = isl_map_plain_get_val_if_fixed(map, isl_dim_in, i);
if (!v)
goto error;
info->is_cst[i] = !isl_val_is_nan(v);
if (info->is_cst[i])
info->cst = isl_vec_set_element_val(info->cst, i, v);
else
isl_val_free(v);
}
return info;
error:
sched_info_free(info);
return NULL;
}
enum isl_access_type {
isl_access_sink,
isl_access_must_source,
isl_access_may_source,
isl_access_kill,
isl_access_end
};
struct isl_union_access_info {
isl_union_map *access[isl_access_end];
isl_schedule *schedule;
isl_union_map *schedule_map;
};
__isl_null isl_union_access_info *isl_union_access_info_free(
__isl_take isl_union_access_info *access)
{
enum isl_access_type i;
if (!access)
return NULL;
for (i = isl_access_sink; i < isl_access_end; ++i)
isl_union_map_free(access->access[i]);
isl_schedule_free(access->schedule);
isl_union_map_free(access->schedule_map);
free(access);
return NULL;
}
isl_ctx *isl_union_access_info_get_ctx(__isl_keep isl_union_access_info *access)
{
if (!access)
return NULL;
return isl_union_map_get_ctx(access->access[isl_access_sink]);
}
static __isl_give isl_union_access_info *isl_union_access_info_alloc(
isl_ctx *ctx)
{
return isl_calloc_type(ctx, isl_union_access_info);
}
static __isl_give isl_union_access_info *isl_union_access_info_init(
__isl_take isl_union_access_info *info)
{
isl_space *space;
isl_union_map *empty;
enum isl_access_type i;
if (!info)
return NULL;
if (!info->access[isl_access_sink])
return isl_union_access_info_free(info);
space = isl_union_map_get_space(info->access[isl_access_sink]);
empty = isl_union_map_empty(isl_space_copy(space));
for (i = isl_access_sink + 1; i < isl_access_end; ++i)
if (!info->access[i])
info->access[i] = isl_union_map_copy(empty);
isl_union_map_free(empty);
if (!info->schedule && !info->schedule_map)
info->schedule = isl_schedule_empty(isl_space_copy(space));
isl_space_free(space);
for (i = isl_access_sink + 1; i < isl_access_end; ++i)
if (!info->access[i])
return isl_union_access_info_free(info);
if (!info->schedule && !info->schedule_map)
return isl_union_access_info_free(info);
return info;
}
__isl_give isl_union_access_info *isl_union_access_info_from_sink(
__isl_take isl_union_map *sink)
{
isl_ctx *ctx;
isl_union_access_info *access;
if (!sink)
return NULL;
ctx = isl_union_map_get_ctx(sink);
access = isl_union_access_info_alloc(ctx);
if (!access)
goto error;
access->access[isl_access_sink] = sink;
return isl_union_access_info_init(access);
error:
isl_union_map_free(sink);
return NULL;
}
static __isl_give isl_union_access_info *isl_union_access_info_set(
__isl_take isl_union_access_info *info,
enum isl_access_type type, __isl_take isl_union_map *access)
{
if (!info || !access)
goto error;
isl_union_map_free(info->access[type]);
info->access[type] = access;
return info;
error:
isl_union_access_info_free(info);
isl_union_map_free(access);
return NULL;
}
__isl_give isl_union_access_info *isl_union_access_info_set_must_source(
__isl_take isl_union_access_info *access,
__isl_take isl_union_map *must_source)
{
return isl_union_access_info_set(access, isl_access_must_source,
must_source);
}
__isl_give isl_union_access_info *isl_union_access_info_set_may_source(
__isl_take isl_union_access_info *access,
__isl_take isl_union_map *may_source)
{
return isl_union_access_info_set(access, isl_access_may_source,
may_source);
}
__isl_give isl_union_access_info *isl_union_access_info_set_kill(
__isl_take isl_union_access_info *info, __isl_take isl_union_map *kill)
{
return isl_union_access_info_set(info, isl_access_kill, kill);
}
static __isl_give isl_union_map *isl_union_access_info_get(
__isl_keep isl_union_access_info *info, enum isl_access_type type)
{
if (!info)
return NULL;
return isl_union_map_copy(info->access[type]);
}
__isl_give isl_union_map *isl_union_access_info_get_must_source(
__isl_keep isl_union_access_info *info)
{
return isl_union_access_info_get(info, isl_access_must_source);
}
__isl_give isl_union_map *isl_union_access_info_get_may_source(
__isl_keep isl_union_access_info *info)
{
return isl_union_access_info_get(info, isl_access_may_source);
}
__isl_give isl_union_map *isl_union_access_info_get_kill(
__isl_keep isl_union_access_info *info)
{
return isl_union_access_info_get(info, isl_access_kill);
}
static isl_bool isl_union_access_has_kill(
__isl_keep isl_union_access_info *info)
{
isl_bool empty;
if (!info)
return isl_bool_error;
empty = isl_union_map_is_empty(info->access[isl_access_kill]);
return isl_bool_not(empty);
}
__isl_give isl_union_access_info *isl_union_access_info_set_schedule(
__isl_take isl_union_access_info *access,
__isl_take isl_schedule *schedule)
{
if (!access || !schedule)
goto error;
access->schedule_map = isl_union_map_free(access->schedule_map);
isl_schedule_free(access->schedule);
access->schedule = schedule;
return access;
error:
isl_union_access_info_free(access);
isl_schedule_free(schedule);
return NULL;
}
__isl_give isl_union_access_info *isl_union_access_info_set_schedule_map(
__isl_take isl_union_access_info *access,
__isl_take isl_union_map *schedule_map)
{
if (!access || !schedule_map)
goto error;
isl_union_map_free(access->schedule_map);
access->schedule = isl_schedule_free(access->schedule);
access->schedule_map = schedule_map;
return access;
error:
isl_union_access_info_free(access);
isl_union_map_free(schedule_map);
return NULL;
}
__isl_give isl_union_access_info *isl_union_access_info_copy(
__isl_keep isl_union_access_info *access)
{
isl_union_access_info *copy;
enum isl_access_type i;
if (!access)
return NULL;
copy = isl_union_access_info_from_sink(
isl_union_map_copy(access->access[isl_access_sink]));
for (i = isl_access_sink + 1; i < isl_access_end; ++i)
copy = isl_union_access_info_set(copy, i,
isl_union_map_copy(access->access[i]));
if (access->schedule)
copy = isl_union_access_info_set_schedule(copy,
isl_schedule_copy(access->schedule));
else
copy = isl_union_access_info_set_schedule_map(copy,
isl_union_map_copy(access->schedule_map));
return copy;
}
#undef BASE
#define BASE union_map
#include "print_yaml_field_templ.c"
enum isl_ai_key {
isl_ai_key_error = -1,
isl_ai_key_sink = isl_access_sink,
isl_ai_key_must_source = isl_access_must_source,
isl_ai_key_may_source = isl_access_may_source,
isl_ai_key_kill = isl_access_kill,
isl_ai_key_schedule_map,
isl_ai_key_schedule,
isl_ai_key_end
};
static char *key_str[] = {
[isl_ai_key_sink] = "sink",
[isl_ai_key_must_source] = "must_source",
[isl_ai_key_may_source] = "may_source",
[isl_ai_key_kill] = "kill",
[isl_ai_key_schedule_map] = "schedule_map",
[isl_ai_key_schedule] = "schedule",
};
static __isl_give isl_printer *print_access_field(__isl_take isl_printer *p,
__isl_keep isl_union_access_info *info, enum isl_access_type type)
{
if (type != isl_access_sink) {
isl_bool empty;
empty = isl_union_map_is_empty(info->access[type]);
if (empty < 0)
return isl_printer_free(p);
if (empty)
return p;
}
return print_yaml_field_union_map(p, key_str[type], info->access[type]);
}
__isl_give isl_printer *isl_printer_print_union_access_info(
__isl_take isl_printer *p, __isl_keep isl_union_access_info *access)
{
enum isl_access_type i;
if (!access)
return isl_printer_free(p);
p = isl_printer_yaml_start_mapping(p);
for (i = isl_access_sink; i < isl_access_end; ++i)
p = print_access_field(p, access, i);
if (access->schedule) {
p = isl_printer_print_str(p, key_str[isl_ai_key_schedule]);
p = isl_printer_yaml_next(p);
p = isl_printer_print_schedule(p, access->schedule);
p = isl_printer_yaml_next(p);
} else {
p = print_yaml_field_union_map(p,
key_str[isl_ai_key_schedule_map], access->schedule_map);
}
p = isl_printer_yaml_end_mapping(p);
return p;
}
__isl_give char *isl_union_access_info_to_str(
__isl_keep isl_union_access_info *access)
{
isl_printer *p;
char *s;
if (!access)
return NULL;
p = isl_printer_to_str(isl_union_access_info_get_ctx(access));
p = isl_printer_set_yaml_style(p, ISL_YAML_STYLE_FLOW);
p = isl_printer_print_union_access_info(p, access);
s = isl_printer_get_str(p);
isl_printer_free(p);
return s;
}
#undef KEY
#define KEY enum isl_ai_key
#undef KEY_ERROR
#define KEY_ERROR isl_ai_key_error
#undef KEY_END
#define KEY_END isl_ai_key_end
#undef KEY_STR
#define KEY_STR key_str
#undef KEY_EXTRACT
#define KEY_EXTRACT extract_key
#undef KEY_GET
#define KEY_GET get_key
#include "extract_key.c"
#undef BASE
#define BASE union_map
#include "read_in_string_templ.c"
__isl_give isl_union_access_info *isl_stream_read_union_access_info(
isl_stream *s)
{
isl_ctx *ctx;
isl_union_access_info *info;
isl_bool more;
int sink_set = 0;
int schedule_set = 0;
if (isl_stream_yaml_read_start_mapping(s) < 0)
return NULL;
ctx = isl_stream_get_ctx(s);
info = isl_union_access_info_alloc(ctx);
while ((more = isl_stream_yaml_next(s)) == isl_bool_true) {
enum isl_ai_key key;
enum isl_access_type type;
isl_union_map *access, *schedule_map;
isl_schedule *schedule;
key = get_key(s);
if (isl_stream_yaml_next(s) < 0)
return isl_union_access_info_free(info);
switch (key) {
case isl_ai_key_end:
case isl_ai_key_error:
return isl_union_access_info_free(info);
case isl_ai_key_sink:
sink_set = 1;
case isl_ai_key_must_source:
case isl_ai_key_may_source:
case isl_ai_key_kill:
type = (enum isl_access_type) key;
access = read_union_map(s);
info = isl_union_access_info_set(info, type, access);
if (!info)
return NULL;
break;
case isl_ai_key_schedule_map:
schedule_set = 1;
schedule_map = read_union_map(s);
info = isl_union_access_info_set_schedule_map(info,
schedule_map);
if (!info)
return NULL;
break;
case isl_ai_key_schedule:
schedule_set = 1;
schedule = isl_stream_read_schedule(s);
info = isl_union_access_info_set_schedule(info,
schedule);
if (!info)
return NULL;
break;
}
}
if (more < 0)
return isl_union_access_info_free(info);
if (isl_stream_yaml_read_end_mapping(s) < 0)
return isl_union_access_info_free(info);
if (!sink_set) {
isl_stream_error(s, NULL, "no sink specified");
return isl_union_access_info_free(info);
}
if (!schedule_set) {
isl_stream_error(s, NULL, "no schedule specified");
return isl_union_access_info_free(info);
}
return isl_union_access_info_init(info);
}
__isl_give isl_union_access_info *isl_union_access_info_read_from_file(
isl_ctx *ctx, FILE *input)
{
isl_stream *s;
isl_union_access_info *access;
s = isl_stream_new_file(ctx, input);
if (!s)
return NULL;
access = isl_stream_read_union_access_info(s);
isl_stream_free(s);
return access;
}
static __isl_give isl_union_access_info *isl_union_access_info_align_params(
__isl_take isl_union_access_info *access)
{
isl_space *space;
enum isl_access_type i;
if (!access)
return NULL;
space = isl_union_map_get_space(access->access[isl_access_sink]);
for (i = isl_access_sink + 1; i < isl_access_end; ++i)
space = isl_space_align_params(space,
isl_union_map_get_space(access->access[i]));
if (access->schedule_map)
space = isl_space_align_params(space,
isl_union_map_get_space(access->schedule_map));
for (i = isl_access_sink; i < isl_access_end; ++i)
access->access[i] =
isl_union_map_align_params(access->access[i],
isl_space_copy(space));
if (!access->schedule_map) {
isl_space_free(space);
} else {
access->schedule_map =
isl_union_map_align_params(access->schedule_map, space);
if (!access->schedule_map)
return isl_union_access_info_free(access);
}
for (i = isl_access_sink; i < isl_access_end; ++i)
if (!access->access[i])
return isl_union_access_info_free(access);
return access;
}
static __isl_give isl_union_access_info *
isl_union_access_info_introduce_schedule(
__isl_take isl_union_access_info *access)
{
isl_union_map *sm;
enum isl_access_type i;
if (!access)
return NULL;
sm = isl_union_map_reverse(access->schedule_map);
sm = isl_union_map_range_map(sm);
for (i = isl_access_sink; i < isl_access_end; ++i)
access->access[i] =
isl_union_map_apply_range(isl_union_map_copy(sm),
access->access[i]);
access->schedule_map = sm;
for (i = isl_access_sink; i < isl_access_end; ++i)
if (!access->access[i])
return isl_union_access_info_free(access);
if (!access->schedule_map)
return isl_union_access_info_free(access);
return access;
}
struct isl_union_flow {
isl_union_map *must_dep;
isl_union_map *may_dep;
isl_union_map *must_no_source;
isl_union_map *may_no_source;
};
isl_ctx *isl_union_flow_get_ctx(__isl_keep isl_union_flow *flow)
{
return flow ? isl_union_map_get_ctx(flow->must_dep) : NULL;
}
__isl_null isl_union_flow *isl_union_flow_free(__isl_take isl_union_flow *flow)
{
if (!flow)
return NULL;
isl_union_map_free(flow->must_dep);
isl_union_map_free(flow->may_dep);
isl_union_map_free(flow->must_no_source);
isl_union_map_free(flow->may_no_source);
free(flow);
return NULL;
}
void isl_union_flow_dump(__isl_keep isl_union_flow *flow)
{
if (!flow)
return;
fprintf(stderr, "must dependences: ");
isl_union_map_dump(flow->must_dep);
fprintf(stderr, "may dependences: ");
isl_union_map_dump(flow->may_dep);
fprintf(stderr, "must no source: ");
isl_union_map_dump(flow->must_no_source);
fprintf(stderr, "may no source: ");
isl_union_map_dump(flow->may_no_source);
}
__isl_give isl_union_map *isl_union_flow_get_full_must_dependence(
__isl_keep isl_union_flow *flow)
{
if (!flow)
return NULL;
return isl_union_map_copy(flow->must_dep);
}
__isl_give isl_union_map *isl_union_flow_get_full_may_dependence(
__isl_keep isl_union_flow *flow)
{
if (!flow)
return NULL;
return isl_union_map_union(isl_union_map_copy(flow->must_dep),
isl_union_map_copy(flow->may_dep));
}
__isl_give isl_union_map *isl_union_flow_get_must_dependence(
__isl_keep isl_union_flow *flow)
{
isl_union_map *dep;
if (!flow)
return NULL;
dep = isl_union_map_copy(flow->must_dep);
return isl_union_map_range_factor_domain(dep);
}
__isl_give isl_union_map *isl_union_flow_get_may_dependence(
__isl_keep isl_union_flow *flow)
{
isl_union_map *dep;
if (!flow)
return NULL;
dep = isl_union_map_union(isl_union_map_copy(flow->must_dep),
isl_union_map_copy(flow->may_dep));
return isl_union_map_range_factor_domain(dep);
}
static __isl_give isl_union_map *isl_union_flow_get_non_must_dependence(
__isl_keep isl_union_flow *flow)
{
if (!flow)
return NULL;
return isl_union_map_copy(flow->may_dep);
}
__isl_give isl_union_map *isl_union_flow_get_must_no_source(
__isl_keep isl_union_flow *flow)
{
if (!flow)
return NULL;
return isl_union_map_copy(flow->must_no_source);
}
__isl_give isl_union_map *isl_union_flow_get_may_no_source(
__isl_keep isl_union_flow *flow)
{
if (!flow)
return NULL;
return isl_union_map_union(isl_union_map_copy(flow->must_no_source),
isl_union_map_copy(flow->may_no_source));
}
static __isl_give isl_union_map *isl_union_flow_get_non_must_no_source(
__isl_keep isl_union_flow *flow)
{
if (!flow)
return NULL;
return isl_union_map_copy(flow->may_no_source);
}
static __isl_give isl_union_flow *isl_union_flow_alloc(
__isl_take isl_space *space)
{
isl_ctx *ctx;
isl_union_map *empty;
isl_union_flow *flow;
if (!space)
return NULL;
ctx = isl_space_get_ctx(space);
flow = isl_alloc_type(ctx, isl_union_flow);
if (!flow)
goto error;
empty = isl_union_map_empty(space);
flow->must_dep = isl_union_map_copy(empty);
flow->may_dep = isl_union_map_copy(empty);
flow->must_no_source = isl_union_map_copy(empty);
flow->may_no_source = empty;
if (!flow->must_dep || !flow->may_dep ||
!flow->must_no_source || !flow->may_no_source)
return isl_union_flow_free(flow);
return flow;
error:
isl_space_free(space);
return NULL;
}
__isl_give isl_union_flow *isl_union_flow_copy(__isl_keep isl_union_flow *flow)
{
isl_union_flow *copy;
if (!flow)
return NULL;
copy = isl_union_flow_alloc(isl_union_map_get_space(flow->must_dep));
if (!copy)
return NULL;
copy->must_dep = isl_union_map_union(copy->must_dep,
isl_union_map_copy(flow->must_dep));
copy->may_dep = isl_union_map_union(copy->may_dep,
isl_union_map_copy(flow->may_dep));
copy->must_no_source = isl_union_map_union(copy->must_no_source,
isl_union_map_copy(flow->must_no_source));
copy->may_no_source = isl_union_map_union(copy->may_no_source,
isl_union_map_copy(flow->may_no_source));
if (!copy->must_dep || !copy->may_dep ||
!copy->must_no_source || !copy->may_no_source)
return isl_union_flow_free(copy);
return copy;
}
static __isl_give isl_union_flow *isl_union_flow_drop_schedule(
__isl_take isl_union_flow *flow)
{
if (!flow)
return NULL;
flow->must_dep = isl_union_map_range_curry(flow->must_dep);
flow->must_dep = isl_union_map_factor_range(flow->must_dep);
flow->may_dep = isl_union_map_range_curry(flow->may_dep);
flow->may_dep = isl_union_map_factor_range(flow->may_dep);
flow->must_no_source =
isl_union_map_domain_factor_range(flow->must_no_source);
flow->may_no_source =
isl_union_map_domain_factor_range(flow->may_no_source);
if (!flow->must_dep || !flow->may_dep ||
!flow->must_no_source || !flow->may_no_source)
return isl_union_flow_free(flow);
return flow;
}
struct isl_compute_flow_data {
isl_union_map *must_source;
isl_union_map *may_source;
isl_union_flow *flow;
int count;
int must;
isl_space *dim;
struct isl_sched_info *sink_info;
struct isl_sched_info **source_info;
isl_access_info *accesses;
};
static isl_stat count_matching_array(__isl_take isl_map *map, void *user)
{
int eq;
isl_space *space;
struct isl_compute_flow_data *data;
data = (struct isl_compute_flow_data *)user;
space = isl_space_range(isl_map_get_space(map));
eq = isl_space_is_equal(space, data->dim);
isl_space_free(space);
isl_map_free(map);
if (eq < 0)
return isl_stat_error;
if (eq)
data->count++;
return isl_stat_ok;
}
static isl_stat collect_matching_array(__isl_take isl_map *map, void *user)
{
int eq;
isl_space *space;
struct isl_sched_info *info;
struct isl_compute_flow_data *data;
data = (struct isl_compute_flow_data *)user;
space = isl_space_range(isl_map_get_space(map));
eq = isl_space_is_equal(space, data->dim);
isl_space_free(space);
if (eq < 0)
goto error;
if (!eq) {
isl_map_free(map);
return isl_stat_ok;
}
info = sched_info_alloc(map);
data->source_info[data->count] = info;
data->accesses = isl_access_info_add_source(data->accesses,
map, data->must, info);
data->count++;
return isl_stat_ok;
error:
isl_map_free(map);
return isl_stat_error;
}
static int before(void *first, void *second)
{
struct isl_sched_info *info1 = first;
struct isl_sched_info *info2 = second;
isl_size n1, n2;
int i;
n1 = isl_vec_size(info1->cst);
n2 = isl_vec_size(info2->cst);
if (n1 < 0 || n2 < 0)
return -1;
if (n2 < n1)
n1 = n2;
for (i = 0; i < n1; ++i) {
int r;
int cmp;
if (!info1->is_cst[i])
continue;
if (!info2->is_cst[i])
continue;
cmp = isl_vec_cmp_element(info1->cst, info2->cst, i);
if (cmp == 0)
continue;
r = 2 * i + (cmp < 0);
return r;
}
return 2 * n1;
}
static isl_bool coscheduled(void *first, void *second)
{
struct isl_sched_info *info1 = first;
struct isl_sched_info *info2 = second;
isl_size n1, n2;
int i;
n1 = isl_vec_size(info1->cst);
n2 = isl_vec_size(info2->cst);
if (n1 < 0 || n2 < 0)
return isl_bool_error;
if (n2 < n1)
n1 = n2;
for (i = 0; i < n1; ++i) {
int cmp;
if (!info1->is_cst[i])
continue;
if (!info2->is_cst[i])
continue;
cmp = isl_vec_cmp_element(info1->cst, info2->cst, i);
if (cmp != 0)
return isl_bool_false;
}
return isl_bool_true;
}
static isl_stat compute_flow(__isl_take isl_map *map, void *user)
{
int i;
isl_ctx *ctx;
struct isl_compute_flow_data *data;
isl_flow *flow;
isl_union_flow *df;
data = (struct isl_compute_flow_data *)user;
df = data->flow;
ctx = isl_map_get_ctx(map);
data->accesses = NULL;
data->sink_info = NULL;
data->source_info = NULL;
data->count = 0;
data->dim = isl_space_range(isl_map_get_space(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_info = sched_info_alloc(map);
data->source_info = isl_calloc_array(ctx, struct isl_sched_info *,
data->count);
data->accesses = isl_access_info_alloc(isl_map_copy(map),
data->sink_info, &before, data->count);
if (!data->sink_info || (data->count && !data->source_info) ||
!data->accesses)
goto error;
data->accesses->coscheduled = &coscheduled;
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 = access_info_compute_flow_core(data->accesses);
data->accesses = NULL;
if (!flow)
goto error;
df->must_no_source = isl_union_map_union(df->must_no_source,
isl_union_map_from_map(isl_flow_get_no_source(flow, 1)));
df->may_no_source = isl_union_map_union(df->may_no_source,
isl_union_map_from_map(isl_flow_get_no_source(flow, 0)));
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)
df->must_dep = isl_union_map_union(df->must_dep, dep);
else
df->may_dep = isl_union_map_union(df->may_dep, dep);
}
isl_flow_free(flow);
sched_info_free(data->sink_info);
if (data->source_info) {
for (i = 0; i < data->count; ++i)
sched_info_free(data->source_info[i]);
free(data->source_info);
}
isl_space_free(data->dim);
isl_map_free(map);
return isl_stat_ok;
error:
isl_access_info_free(data->accesses);
sched_info_free(data->sink_info);
if (data->source_info) {
for (i = 0; i < data->count; ++i)
sched_info_free(data->source_info[i]);
free(data->source_info);
}
isl_space_free(data->dim);
isl_map_free(map);
return isl_stat_error;
}
static __isl_give isl_union_access_info *
isl_union_access_info_add_kill_to_must_source(
__isl_take isl_union_access_info *info)
{
isl_union_map *must, *kill;
must = isl_union_access_info_get_must_source(info);
kill = isl_union_access_info_get_kill(info);
must = isl_union_map_union(must, kill);
return isl_union_access_info_set_must_source(info, must);
}
static __isl_give isl_union_flow *isl_union_flow_drop_kill_source(
__isl_take isl_union_flow *flow, __isl_take isl_union_map *must,
__isl_take isl_union_map *may)
{
isl_union_map *move;
if (!flow)
goto error;
move = isl_union_map_copy(flow->must_dep);
move = isl_union_map_intersect_range_factor_range(move,
isl_union_map_copy(may));
may = isl_union_map_union(may, isl_union_map_copy(must));
flow->may_dep = isl_union_map_intersect_range_factor_range(
flow->may_dep, may);
flow->must_dep = isl_union_map_intersect_range_factor_range(
flow->must_dep, must);
flow->may_dep = isl_union_map_union(flow->may_dep, move);
if (!flow->must_dep || !flow->may_dep)
return isl_union_flow_free(flow);
return flow;
error:
isl_union_map_free(must);
isl_union_map_free(may);
return NULL;
}
static __isl_give isl_union_access_info *isl_union_access_info_normalize(
__isl_take isl_union_access_info *access)
{
if (!access)
return NULL;
access->access[isl_access_may_source] =
isl_union_map_subtract(access->access[isl_access_may_source],
isl_union_map_copy(access->access[isl_access_must_source]));
if (!access->access[isl_access_may_source])
return isl_union_access_info_free(access);
return access;
}
static __isl_give isl_union_flow *compute_flow_union_map(
__isl_take isl_union_access_info *access)
{
struct isl_compute_flow_data data;
isl_union_map *sink;
access = isl_union_access_info_align_params(access);
access = isl_union_access_info_introduce_schedule(access);
if (!access)
return NULL;
data.must_source = access->access[isl_access_must_source];
data.may_source = access->access[isl_access_may_source];
sink = access->access[isl_access_sink];
data.flow = isl_union_flow_alloc(isl_union_map_get_space(sink));
if (isl_union_map_foreach_map(sink, &compute_flow, &data) < 0)
goto error;
data.flow = isl_union_flow_drop_schedule(data.flow);
isl_union_access_info_free(access);
return data.flow;
error:
isl_union_access_info_free(access);
isl_union_flow_free(data.flow);
return NULL;
}
struct isl_scheduled_access {
isl_map *access;
int must;
isl_schedule_node *node;
};
struct isl_compute_flow_schedule_data {
isl_union_access_info *access;
int n_sink;
int n_source;
struct isl_scheduled_access *sink;
struct isl_scheduled_access *source;
int set_sink;
int must;
isl_schedule_node *node;
};
static void isl_compute_flow_schedule_data_align_params(
struct isl_compute_flow_schedule_data *data)
{
int i;
isl_space *space;
if (data->n_sink == 0 || data->n_source == 0)
return;
space = isl_map_get_space(data->sink[0].access);
for (i = 1; i < data->n_sink; ++i)
space = isl_space_align_params(space,
isl_map_get_space(data->sink[i].access));
for (i = 0; i < data->n_source; ++i)
space = isl_space_align_params(space,
isl_map_get_space(data->source[i].access));
for (i = 0; i < data->n_sink; ++i)
data->sink[i].access =
isl_map_align_params(data->sink[i].access,
isl_space_copy(space));
for (i = 0; i < data->n_source; ++i)
data->source[i].access =
isl_map_align_params(data->source[i].access,
isl_space_copy(space));
isl_space_free(space);
}
static void isl_compute_flow_schedule_data_clear(
struct isl_compute_flow_schedule_data *data)
{
int i;
if (!data->sink)
return;
for (i = 0; i < data->n_sink; ++i) {
isl_map_free(data->sink[i].access);
isl_schedule_node_free(data->sink[i].node);
}
for (i = 0; i < data->n_source; ++i) {
isl_map_free(data->source[i].access);
isl_schedule_node_free(data->source[i].node);
}
free(data->sink);
}
static isl_bool count_sink_source(__isl_keep isl_schedule_node *node,
void *user)
{
struct isl_compute_flow_schedule_data *data = user;
isl_union_set *domain;
isl_union_map *umap;
isl_bool r = isl_bool_false;
isl_size n;
if (isl_schedule_node_get_type(node) != isl_schedule_node_leaf)
return isl_bool_true;
domain = isl_schedule_node_get_universe_domain(node);
umap = isl_union_map_copy(data->access->access[isl_access_sink]);
umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(domain));
data->n_sink += n = isl_union_map_n_map(umap);
isl_union_map_free(umap);
if (n < 0)
r = isl_bool_error;
umap = isl_union_map_copy(data->access->access[isl_access_must_source]);
umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(domain));
data->n_source += n = isl_union_map_n_map(umap);
isl_union_map_free(umap);
if (n < 0)
r = isl_bool_error;
umap = isl_union_map_copy(data->access->access[isl_access_may_source]);
umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(domain));
data->n_source += n = isl_union_map_n_map(umap);
isl_union_map_free(umap);
if (n < 0)
r = isl_bool_error;
isl_union_set_free(domain);
return r;
}
static isl_stat extract_sink_source(__isl_take isl_map *map, void *user)
{
struct isl_compute_flow_schedule_data *data = user;
struct isl_scheduled_access *access;
if (data->set_sink)
access = data->sink + data->n_sink++;
else
access = data->source + data->n_source++;
access->access = map;
access->must = data->must;
access->node = isl_schedule_node_copy(data->node);
return isl_stat_ok;
}
static isl_bool collect_sink_source(__isl_keep isl_schedule_node *node,
void *user)
{
struct isl_compute_flow_schedule_data *data = user;
isl_union_map *prefix;
isl_union_map *umap;
isl_bool r = isl_bool_false;
if (isl_schedule_node_get_type(node) != isl_schedule_node_leaf)
return isl_bool_true;
data->node = node;
prefix = isl_schedule_node_get_prefix_schedule_relation(node);
prefix = isl_union_map_reverse(prefix);
prefix = isl_union_map_range_map(prefix);
data->set_sink = 1;
umap = isl_union_map_copy(data->access->access[isl_access_sink]);
umap = isl_union_map_apply_range(isl_union_map_copy(prefix), umap);
if (isl_union_map_foreach_map(umap, &extract_sink_source, data) < 0)
r = isl_bool_error;
isl_union_map_free(umap);
data->set_sink = 0;
data->must = 1;
umap = isl_union_map_copy(data->access->access[isl_access_must_source]);
umap = isl_union_map_apply_range(isl_union_map_copy(prefix), umap);
if (isl_union_map_foreach_map(umap, &extract_sink_source, data) < 0)
r = isl_bool_error;
isl_union_map_free(umap);
data->set_sink = 0;
data->must = 0;
umap = isl_union_map_copy(data->access->access[isl_access_may_source]);
umap = isl_union_map_apply_range(isl_union_map_copy(prefix), umap);
if (isl_union_map_foreach_map(umap, &extract_sink_source, data) < 0)
r = isl_bool_error;
isl_union_map_free(umap);
isl_union_map_free(prefix);
return r;
}
static int before_node(void *first, void *second)
{
isl_schedule_node *node1 = first;
isl_schedule_node *node2 = second;
isl_schedule_node *shared;
isl_size depth;
int before = 0;
shared = isl_schedule_node_get_shared_ancestor(node1, node2);
depth = isl_schedule_node_get_schedule_depth(shared);
if (depth < 0) {
isl_schedule_node_free(shared);
return -1;
}
if (isl_schedule_node_get_type(shared) == isl_schedule_node_sequence) {
isl_size pos1, pos2;
pos1 = isl_schedule_node_get_ancestor_child_position(node1,
shared);
pos2 = isl_schedule_node_get_ancestor_child_position(node2,
shared);
if (pos1 < 0 || pos2 < 0) {
isl_schedule_node_free(shared);
return -1;
}
before = pos1 < pos2;
}
isl_schedule_node_free(shared);
return 2 * depth + before;
}
static isl_bool coscheduled_node(void *first, void *second)
{
isl_schedule_node *node1 = first;
isl_schedule_node *node2 = second;
return isl_bool_ok(node1 == node2);
}
static __isl_give isl_access_info *add_matching_sources(
__isl_take isl_access_info *access, struct isl_scheduled_access *sink,
struct isl_compute_flow_schedule_data *data)
{
int i;
isl_space *space;
space = isl_space_range(isl_map_get_space(sink->access));
for (i = 0; i < data->n_source; ++i) {
struct isl_scheduled_access *source;
isl_space *source_space;
int eq;
source = &data->source[i];
source_space = isl_map_get_space(source->access);
source_space = isl_space_range(source_space);
eq = isl_space_is_equal(space, source_space);
isl_space_free(source_space);
if (!eq)
continue;
if (eq < 0)
goto error;
access = isl_access_info_add_source(access,
isl_map_copy(source->access), source->must, source->node);
}
isl_space_free(space);
return access;
error:
isl_space_free(space);
isl_access_info_free(access);
return NULL;
}
static __isl_give isl_union_flow *compute_single_flow(
__isl_take isl_union_flow *uf, struct isl_scheduled_access *sink,
struct isl_compute_flow_schedule_data *data)
{
int i;
isl_access_info *access;
isl_flow *flow;
isl_map *map;
if (!uf)
return NULL;
access = isl_access_info_alloc(isl_map_copy(sink->access), sink->node,
&before_node, data->n_source);
if (access)
access->coscheduled = &coscheduled_node;
access = add_matching_sources(access, sink, data);
flow = access_info_compute_flow_core(access);
if (!flow)
return isl_union_flow_free(uf);
map = isl_map_domain_factor_range(isl_flow_get_no_source(flow, 1));
uf->must_no_source = isl_union_map_union(uf->must_no_source,
isl_union_map_from_map(map));
map = isl_map_domain_factor_range(isl_flow_get_no_source(flow, 0));
uf->may_no_source = isl_union_map_union(uf->may_no_source,
isl_union_map_from_map(map));
for (i = 0; i < flow->n_source; ++i) {
isl_union_map *dep;
map = isl_map_range_curry(isl_map_copy(flow->dep[i].map));
map = isl_map_factor_range(map);
dep = isl_union_map_from_map(map);
if (flow->dep[i].must)
uf->must_dep = isl_union_map_union(uf->must_dep, dep);
else
uf->may_dep = isl_union_map_union(uf->may_dep, dep);
}
isl_flow_free(flow);
return uf;
}
static __isl_give isl_union_flow *compute_flow_schedule(
__isl_take isl_union_access_info *access)
{
struct isl_compute_flow_schedule_data data = { access };
int i, n;
isl_ctx *ctx;
isl_space *space;
isl_union_flow *flow;
ctx = isl_union_access_info_get_ctx(access);
data.n_sink = 0;
data.n_source = 0;
if (isl_schedule_foreach_schedule_node_top_down(access->schedule,
&count_sink_source, &data) < 0)
goto error;
n = data.n_sink + data.n_source;
data.sink = isl_calloc_array(ctx, struct isl_scheduled_access, n);
if (n && !data.sink)
goto error;
data.source = data.sink + data.n_sink;
data.n_sink = 0;
data.n_source = 0;
if (isl_schedule_foreach_schedule_node_top_down(access->schedule,
&collect_sink_source, &data) < 0)
goto error;
space = isl_union_map_get_space(access->access[isl_access_sink]);
flow = isl_union_flow_alloc(space);
isl_compute_flow_schedule_data_align_params(&data);
for (i = 0; i < data.n_sink; ++i)
flow = compute_single_flow(flow, &data.sink[i], &data);
isl_compute_flow_schedule_data_clear(&data);
isl_union_access_info_free(access);
return flow;
error:
isl_union_access_info_free(access);
isl_compute_flow_schedule_data_clear(&data);
return NULL;
}
__isl_give isl_union_flow *isl_union_access_info_compute_flow(
__isl_take isl_union_access_info *access)
{
isl_bool has_kill;
isl_union_map *must = NULL, *may = NULL;
isl_union_flow *flow;
has_kill = isl_union_access_has_kill(access);
if (has_kill < 0)
goto error;
if (has_kill) {
must = isl_union_access_info_get_must_source(access);
may = isl_union_access_info_get_may_source(access);
}
access = isl_union_access_info_add_kill_to_must_source(access);
access = isl_union_access_info_normalize(access);
if (!access)
goto error;
if (access->schedule)
flow = compute_flow_schedule(access);
else
flow = compute_flow_union_map(access);
if (has_kill)
flow = isl_union_flow_drop_kill_source(flow, must, may);
return flow;
error:
isl_union_access_info_free(access);
isl_union_map_free(must);
isl_union_map_free(may);
return NULL;
}
__isl_give isl_printer *isl_printer_print_union_flow(
__isl_take isl_printer *p, __isl_keep isl_union_flow *flow)
{
isl_union_map *umap;
if (!flow)
return isl_printer_free(p);
p = isl_printer_yaml_start_mapping(p);
umap = isl_union_flow_get_full_must_dependence(flow);
p = print_yaml_field_union_map(p, "must_dependence", umap);
isl_union_map_free(umap);
umap = isl_union_flow_get_full_may_dependence(flow);
p = print_yaml_field_union_map(p, "may_dependence", umap);
isl_union_map_free(umap);
p = print_yaml_field_union_map(p, "must_no_source",
flow->must_no_source);
umap = isl_union_flow_get_may_no_source(flow);
p = print_yaml_field_union_map(p, "may_no_source", umap);
isl_union_map_free(umap);
p = isl_printer_yaml_end_mapping(p);
return p;
}
__isl_give char *isl_union_flow_to_str(__isl_keep isl_union_flow *flow)
{
isl_printer *p;
char *s;
if (!flow)
return NULL;
p = isl_printer_to_str(isl_union_flow_get_ctx(flow));
p = isl_printer_set_yaml_style(p, ISL_YAML_STYLE_FLOW);
p = isl_printer_print_union_flow(p, flow);
s = isl_printer_get_str(p);
isl_printer_free(p);
return s;
}
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_map **must_no_source,
__isl_give isl_union_map **may_no_source)
{
isl_union_access_info *access;
isl_union_flow *flow;
access = isl_union_access_info_from_sink(sink);
access = isl_union_access_info_set_must_source(access, must_source);
access = isl_union_access_info_set_may_source(access, may_source);
access = isl_union_access_info_set_schedule_map(access, schedule);
flow = isl_union_access_info_compute_flow(access);
if (must_dep)
*must_dep = isl_union_flow_get_must_dependence(flow);
if (may_dep)
*may_dep = isl_union_flow_get_non_must_dependence(flow);
if (must_no_source)
*must_no_source = isl_union_flow_get_must_no_source(flow);
if (may_no_source)
*may_no_source = isl_union_flow_get_non_must_no_source(flow);
isl_union_flow_free(flow);
if ((must_dep && !*must_dep) || (may_dep && !*may_dep) ||
(must_no_source && !*must_no_source) ||
(may_no_source && !*may_no_source))
goto error;
return 0;
error:
if (must_dep)
*must_dep = isl_union_map_free(*must_dep);
if (may_dep)
*may_dep = isl_union_map_free(*may_dep);
if (must_no_source)
*must_no_source = isl_union_map_free(*must_no_source);
if (may_no_source)
*may_no_source = isl_union_map_free(*may_no_source);
return -1;
}