[PATCH 1/9] add isl_local_copy

2 views
Skip to first unread message

skim...@kotnet.org

unread,
Aug 15, 2021, 12:35:42 PMAug 15
to isl-dev...@googlegroups.com
From: Sven Verdoolaege <sk...@kotnet.org>

This will be used in the implementation of isl_local_space_get_local
in an upcoming commit.

Signed-off-by: Sven Verdoolaege <sven.ver...@gmail.com>
---
isl_local.c | 7 +++++++
isl_local.h | 1 +
2 files changed, 8 insertions(+)

diff --git a/isl_local.c b/isl_local.c
index e409359d28..c5ba25d987 100644
--- a/isl_local.c
+++ b/isl_local.c
@@ -38,6 +38,13 @@ __isl_give isl_local *isl_local_alloc_from_mat(__isl_take isl_mat *mat)
return mat;
}

+/* Return a new reference to "local".
+ */
+__isl_give isl_local *isl_local_copy(__isl_keep isl_local *local)
+{
+ return isl_local_alloc_from_mat(isl_mat_copy(local));
+}
+
/* Free "local" and return NULL.
*/
__isl_null isl_local *isl_local_free(__isl_take isl_local *local)
diff --git a/isl_local.h b/isl_local.h
index a52622e4a1..a1f5184a97 100644
--- a/isl_local.h
+++ b/isl_local.h
@@ -6,6 +6,7 @@

typedef isl_mat isl_local;

+__isl_give isl_local *isl_local_copy(__isl_keep isl_local *local);
__isl_null isl_local *isl_local_free(__isl_take isl_local *local);

isl_bool isl_local_div_is_marked_unknown(__isl_keep isl_local *local, int pos);
--
2.25.1

skim...@kotnet.org

unread,
Aug 15, 2021, 12:35:44 PMAug 15
to isl-dev...@googlegroups.com
From: Sven Verdoolaege <sk...@kotnet.org>

This will be used in the implementation of isl_local_move_vars
in an upcoming commit.

Signed-off-by: Sven Verdoolaege <sven.ver...@gmail.com>
---
isl_local.c | 27 +++++++++++++++++++++++++++
isl_local.h | 3 +++
2 files changed, 30 insertions(+)

diff --git a/isl_local.c b/isl_local.c
index c5ba25d987..f3fa0ecf55 100644
--- a/isl_local.c
+++ b/isl_local.c
@@ -1,6 +1,7 @@
/*
* Copyright 2011 INRIA Saclay
* Copyright 2014 Ecole Normale Superieure
+ * Copyright 2015 Sven Verdoolaege
*
* Use of this software is governed by the MIT license
*
@@ -229,6 +230,32 @@ int isl_local_cmp(__isl_keep isl_local *local1, __isl_keep isl_local *local2)
return 0;
}

+/* Return the position of the variables of the given type
+ * within the sequence of variables of "local".
+ *
+ * Only the position of the local variables can be obtained.
+ * It is equal to the total number of variables minus
+ * the number of local variables.
+ */
+isl_size isl_local_var_offset(__isl_keep isl_local *local,
+ enum isl_dim_type type)
+{
+ isl_size n_div, n_all;
+
+ if (!local)
+ return isl_size_error;
+ if (type != isl_dim_div)
+ isl_die(isl_local_get_ctx(local), isl_error_unsupported,
+ "only the offset of the local variables "
+ "can be obtained", return isl_size_error);
+
+ n_div = isl_local_dim(local, isl_dim_div);
+ n_all = isl_local_dim(local, isl_dim_all);
+ if (n_div < 0 || n_all < 0)
+ return isl_size_error;
+ return n_all - n_div;
+}
+
/* Reorder the columns of the given local variables according to the
* given reordering.
* The order of the local variables themselves is assumed not to change.
diff --git a/isl_local.h b/isl_local.h
index a1f5184a97..0efa443f5f 100644
--- a/isl_local.h
+++ b/isl_local.h
@@ -15,6 +15,9 @@ isl_bool isl_local_divs_known(__isl_keep isl_local *local);

int isl_local_cmp(__isl_keep isl_local *local1, __isl_keep isl_local *local2);

+isl_size isl_local_var_offset(__isl_keep isl_local *local,
+ enum isl_dim_type type);
+
__isl_give isl_local *isl_local_reorder(__isl_take isl_local *local,
__isl_take isl_reordering *r);

--
2.25.1

skim...@kotnet.org

unread,
Aug 15, 2021, 12:35:45 PMAug 15
to isl-dev...@googlegroups.com
From: Sven Verdoolaege <sven.ver...@gmail.com>

This reduces the dependence on the internal representation.

Signed-off-by: Sven Verdoolaege <sven.ver...@gmail.com>
---
isl_local_space.c | 5 ++---
1 file changed, 2 insertions(+), 3 deletions(-)

diff --git a/isl_local_space.c b/isl_local_space.c
index 1eebd44755..009ad25b67 100644
--- a/isl_local_space.c
+++ b/isl_local_space.c
@@ -250,10 +250,9 @@ unsigned isl_local_space_offset(__isl_keep isl_local_space *ls,
{
isl_space *space;

- if (!ls)
+ space = isl_local_space_peek_space(ls);
+ if (space < 0)
return 0;
-
- space = ls->dim;
switch (type) {
case isl_dim_cst: return 0;
case isl_dim_param: return 1;
--
2.25.1

skim...@kotnet.org

unread,
Aug 15, 2021, 12:35:46 PMAug 15
to isl-dev...@googlegroups.com
From: Sven Verdoolaege <sk...@kotnet.org>

This reduces the dependence on the internal representation.

Signed-off-by: Sven Verdoolaege <sven.ver...@gmail.com>
---
isl_local_space.c | 9 ++++-----
1 file changed, 4 insertions(+), 5 deletions(-)

diff --git a/isl_local_space.c b/isl_local_space.c
index 009ad25b67..aed1b7aa16 100644
--- a/isl_local_space.c
+++ b/isl_local_space.c
@@ -255,11 +255,10 @@ unsigned isl_local_space_offset(__isl_keep isl_local_space *ls,
return 0;
switch (type) {
case isl_dim_cst: return 0;
- case isl_dim_param: return 1;
- case isl_dim_in: return 1 + space->nparam;
- case isl_dim_out: return 1 + space->nparam + space->n_in;
- case isl_dim_div:
- return 1 + space->nparam + space->n_in + space->n_out;
+ case isl_dim_param:
+ case isl_dim_in:
+ case isl_dim_out: return 1 + isl_space_offset(space, type);
+ case isl_dim_div: return 1 + isl_space_dim(space, isl_dim_all);
default: return 0;
}
}
--
2.25.1

skim...@kotnet.org

unread,
Aug 15, 2021, 12:35:47 PMAug 15
to isl-dev...@googlegroups.com
From: Sven Verdoolaege <sven.ver...@gmail.com>

As in the case of isl_basic_map_var_offset, it can in some cases
be more convenient to think in terms of the position among the variables
rather than the position of the coefficients.

Signed-off-by: Sven Verdoolaege <sven.ver...@gmail.com>
---
isl_local_space.c | 30 +++++++++++++++++++++++++++---
isl_local_space_private.h | 2 ++
2 files changed, 29 insertions(+), 3 deletions(-)

diff --git a/isl_local_space.c b/isl_local_space.c
index aed1b7aa16..d59831648b 100644
--- a/isl_local_space.c
+++ b/isl_local_space.c
@@ -245,20 +245,44 @@ isl_size isl_local_space_dim(__isl_keep isl_local_space *ls,
#define TYPE isl_local_space
#include "check_type_range_templ.c"

-unsigned isl_local_space_offset(__isl_keep isl_local_space *ls,
+/* Return the position of the variables of the given type
+ * within the sequence of variables of "ls".
+ */
+isl_size isl_local_space_var_offset(__isl_keep isl_local_space *ls,
enum isl_dim_type type)
{
isl_space *space;

space = isl_local_space_peek_space(ls);
if (space < 0)
+ return isl_size_error;
+ switch (type) {
+ case isl_dim_param:
+ case isl_dim_in:
+ case isl_dim_out: return isl_space_offset(space, type);
+ case isl_dim_div: return isl_space_dim(space, isl_dim_all);
+ case isl_dim_cst:
+ default:
+ isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
+ "invalid dimension type", return isl_size_error);
+ }
+}
+
+/* Return the position of the coefficients of the variables of the given type
+ * within the sequence of coefficients of "ls".
+ */
+unsigned isl_local_space_offset(__isl_keep isl_local_space *ls,
+ enum isl_dim_type type)
+{
+ if (!ls)
return 0;
+
switch (type) {
case isl_dim_cst: return 0;
case isl_dim_param:
case isl_dim_in:
- case isl_dim_out: return 1 + isl_space_offset(space, type);
- case isl_dim_div: return 1 + isl_space_dim(space, isl_dim_all);
+ case isl_dim_out:
+ case isl_dim_div: return 1 + isl_local_space_var_offset(ls, type);
default: return 0;
}
}
diff --git a/isl_local_space_private.h b/isl_local_space_private.h
index 42c2ad0fc0..fbf2159c88 100644
--- a/isl_local_space_private.h
+++ b/isl_local_space_private.h
@@ -34,6 +34,8 @@ int isl_mat_cmp_div(__isl_keep isl_mat *div, int i, int j);
__isl_give isl_mat *isl_merge_divs(__isl_keep isl_mat *div1,
__isl_keep isl_mat *div2, int *exp1, int *exp2);

+isl_size isl_local_space_var_offset(__isl_keep isl_local_space *ls,
+ enum isl_dim_type type);
unsigned isl_local_space_offset(__isl_keep isl_local_space *ls,
enum isl_dim_type type);

--
2.25.1

skim...@kotnet.org

unread,
Aug 15, 2021, 12:35:48 PMAug 15
to isl-dev...@googlegroups.com
From: Sven Verdoolaege <sv...@cerebras.net>

This prepares for the next commit.

Signed-off-by: Sven Verdoolaege <sv...@cerebras.net>
---
isl_local_space.c | 11 +++++++----
1 file changed, 7 insertions(+), 4 deletions(-)

diff --git a/isl_local_space.c b/isl_local_space.c
index d59831648b..0553828d44 100644
--- a/isl_local_space.c
+++ b/isl_local_space.c
@@ -1579,6 +1579,7 @@ __isl_give isl_local_space *isl_local_space_move_dims(
enum isl_dim_type dst_type, unsigned dst_pos,
enum isl_dim_type src_type, unsigned src_pos, unsigned n)
{
+ isl_size v_src, v_dst;
unsigned g_dst_pos;
unsigned g_src_pos;

@@ -1607,11 +1608,13 @@ __isl_give isl_local_space *isl_local_space_move_dims(
return isl_local_space_free(ls));

ls = isl_local_space_cow(ls);
- if (!ls)
- return NULL;

- g_src_pos = 1 + isl_local_space_offset(ls, src_type) + src_pos;
- g_dst_pos = 1 + isl_local_space_offset(ls, dst_type) + dst_pos;
+ v_src = isl_local_space_var_offset(ls, src_type);
+ v_dst = isl_local_space_var_offset(ls, dst_type);
+ if (v_src < 0 || v_dst < 0)
+ return isl_local_space_free(ls);
+ g_src_pos = 1 + 1 + v_src + src_pos;
+ g_dst_pos = 1 + 1 + v_dst + dst_pos;
if (dst_type > src_type)
g_dst_pos -= n;
ls->div = isl_mat_move_cols(ls->div, g_dst_pos, g_src_pos, n);
--
2.25.1

skim...@kotnet.org

unread,
Aug 15, 2021, 12:35:49 PMAug 15
to isl-dev...@googlegroups.com
From: Sven Verdoolaege <sk...@kotnet.org>

This isolates the local variable manipulation.

Signed-off-by: Sven Verdoolaege <sven.ver...@gmail.com>
---
isl_local.c | 26 ++++++++++++++++++++++++++
isl_local.h | 3 +++
isl_local_space.c | 6 +++---
3 files changed, 32 insertions(+), 3 deletions(-)

diff --git a/isl_local.c b/isl_local.c
index f3fa0ecf55..88d25cd693 100644
--- a/isl_local.c
+++ b/isl_local.c
@@ -299,6 +299,32 @@ error:
return NULL;
}

+/* Move the "n" variables starting at "src_pos" of "local" to "dst_pos".
+ *
+ * Moving local variables is not allowed.
+ */
+__isl_give isl_local *isl_local_move_vars(__isl_take isl_local *local,
+ unsigned dst_pos, unsigned src_pos, unsigned n)
+{
+ isl_mat *mat = local;
+ isl_size v_div;
+
+ v_div = isl_local_var_offset(local, isl_dim_div);
+ if (v_div < 0)
+ return isl_local_free(local);
+ if (n == 0)
+ return local;
+
+ if (dst_pos >= v_div || src_pos >= v_div)
+ isl_die(isl_local_get_ctx(local), isl_error_invalid,
+ "cannot move local variables",
+ return isl_local_free(local));
+
+ mat = isl_mat_move_cols(mat, 2 + dst_pos, 2 + src_pos, n);
+
+ return isl_local_alloc_from_mat(mat);
+}
+
/* Extend a vector "v" representing an integer point
* in the domain space of "local"
* to one that also includes values for the local variables.
diff --git a/isl_local.h b/isl_local.h
index 0efa443f5f..76527036e2 100644
--- a/isl_local.h
+++ b/isl_local.h
@@ -21,6 +21,9 @@ isl_size isl_local_var_offset(__isl_keep isl_local *local,
__isl_give isl_local *isl_local_reorder(__isl_take isl_local *local,
__isl_take isl_reordering *r);

+__isl_give isl_local *isl_local_move_vars(__isl_take isl_local *local,
+ unsigned dst_pos, unsigned src_pos, unsigned n);
+
__isl_give isl_vec *isl_local_extend_point_vec(__isl_keep isl_local *local,
__isl_take isl_vec *v);

diff --git a/isl_local_space.c b/isl_local_space.c
index 0553828d44..7057b9f08e 100644
--- a/isl_local_space.c
+++ b/isl_local_space.c
@@ -1613,11 +1613,11 @@ __isl_give isl_local_space *isl_local_space_move_dims(
v_dst = isl_local_space_var_offset(ls, dst_type);
if (v_src < 0 || v_dst < 0)
return isl_local_space_free(ls);
- g_src_pos = 1 + 1 + v_src + src_pos;
- g_dst_pos = 1 + 1 + v_dst + dst_pos;
+ g_src_pos = v_src + src_pos;
+ g_dst_pos = v_dst + dst_pos;
if (dst_type > src_type)
g_dst_pos -= n;
- ls->div = isl_mat_move_cols(ls->div, g_dst_pos, g_src_pos, n);
+ ls->div = isl_local_move_vars(ls->div, g_dst_pos, g_src_pos, n);
if (!ls->div)
return isl_local_space_free(ls);
ls->dim = isl_space_move_dims(ls->dim, dst_type, dst_pos,
--
2.25.1

skim...@kotnet.org

unread,
Aug 15, 2021, 12:35:50 PMAug 15
to isl-dev...@googlegroups.com
From: Sven Verdoolaege <sv...@cerebras.net>

This reduces the dependence on the internal representation.

Signed-off-by: Sven Verdoolaege <sv...@cerebras.net>
---
isl_local_space.c | 8 +++++---
1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/isl_local_space.c b/isl_local_space.c
index 7057b9f08e..0dc55aa1f7 100644
--- a/isl_local_space.c
+++ b/isl_local_space.c
@@ -1579,6 +1579,7 @@ __isl_give isl_local_space *isl_local_space_move_dims(
enum isl_dim_type dst_type, unsigned dst_pos,
enum isl_dim_type src_type, unsigned src_pos, unsigned n)
{
+ isl_space *space;
isl_size v_src, v_dst;
unsigned g_dst_pos;
unsigned g_src_pos;
@@ -1620,10 +1621,11 @@ __isl_give isl_local_space *isl_local_space_move_dims(
ls->div = isl_local_move_vars(ls->div, g_dst_pos, g_src_pos, n);
if (!ls->div)
return isl_local_space_free(ls);
- ls->dim = isl_space_move_dims(ls->dim, dst_type, dst_pos,
+
+ space = isl_local_space_take_space(ls);
+ space = isl_space_move_dims(space, dst_type, dst_pos,
src_type, src_pos, n);
- if (!ls->dim)
- return isl_local_space_free(ls);
+ ls = isl_local_space_restore_space(ls, space);

return ls;
}
--
2.25.1

skim...@kotnet.org

unread,
Aug 15, 2021, 12:35:51 PMAug 15
to isl-dev...@googlegroups.com
From: Sven Verdoolaege <sk...@kotnet.org>

This reduces the dependence on the internal representation.

Note that the calls to isl_local_space_cow can be removed
because it is called by isl_local_space_restore_local
(or isl_local_space_restore_space) when needed.

Signed-off-by: Sven Verdoolaege <sven.ver...@gmail.com>
---
isl_local_space.c | 84 ++++++++++++++++++++++++++++++++++++++---------
1 file changed, 69 insertions(+), 15 deletions(-)

diff --git a/isl_local_space.c b/isl_local_space.c
index 0dc55aa1f7..604e276a0d 100644
--- a/isl_local_space.c
+++ b/isl_local_space.c
@@ -486,6 +486,66 @@ __isl_keep isl_local *isl_local_space_peek_local(__isl_keep isl_local_space *ls)
return ls ? ls->div : NULL;
}

+/* Return a copy of the local variables of "ls".
+ */
+__isl_keep isl_local *isl_local_space_get_local(__isl_keep isl_local_space *ls)
+{
+ return isl_local_copy(isl_local_space_peek_local(ls));
+}
+
+/* Return the local variables of "ls".
+ * This may be either a copy or the local variables itself
+ * if there is only one reference to "ls".
+ * This allows the local variables to be modified inplace
+ * if both the local space and its local variables have only a single reference.
+ * The caller is not allowed to modify "ls" between this call and
+ * the subsequent call to isl_local_space_restore_local.
+ * The only exception is that isl_local_space_free can be called instead.
+ */
+static __isl_give isl_local *isl_local_space_take_local(
+ __isl_keep isl_local_space *ls)
+{
+ isl_local *local;
+
+ if (!ls)
+ return NULL;
+ if (ls->ref != 1)
+ return isl_local_space_get_local(ls);
+ local = ls->div;
+ ls->div = NULL;
+ return local;
+}
+
+/* Set the local variables of "ls" to "local",
+ * where the local variables of "ls" may be missing
+ * due to a preceding call to isl_local_space_take_local.
+ * However, in this case, "ls" only has a single reference and
+ * then the call to isl_local_space_cow has no effect.
+ */
+static __isl_give isl_local_space *isl_local_space_restore_local(
+ __isl_take isl_local_space *ls, __isl_take isl_local *local)
+{
+ if (!ls || !local)
+ goto error;
+
+ if (ls->div == local) {
+ isl_local_free(local);
+ return ls;
+ }
+
+ ls = isl_local_space_cow(ls);
+ if (!ls)
+ goto error;
+ isl_local_free(ls->div);
+ ls->div = local;
+
+ return ls;
+error:
+ isl_local_space_free(ls);
+ isl_local_free(local);
+ return NULL;
+}
+
/* Replace the identifier of the tuple of type "type" by "id".
*/
__isl_give isl_local_space *isl_local_space_set_tuple_id(
@@ -573,22 +633,16 @@ error:
__isl_give isl_local_space *isl_local_space_realign(
__isl_take isl_local_space *ls, __isl_take isl_reordering *r)
{
- ls = isl_local_space_cow(ls);
- if (!ls || !r)
- goto error;
+ isl_local *local;

- ls->div = isl_local_reorder(ls->div, isl_reordering_copy(r));
- if (!ls->div)
- goto error;
+ local = isl_local_space_take_local(ls);
+ local = isl_local_reorder(local, isl_reordering_copy(r));
+ ls = isl_local_space_restore_local(ls, local);

ls = isl_local_space_reset_space(ls, isl_reordering_get_space(r));

isl_reordering_free(r);
return ls;
-error:
- isl_local_space_free(ls);
- isl_reordering_free(r);
- return NULL;
}

__isl_give isl_local_space *isl_local_space_add_div(
@@ -1580,6 +1634,7 @@ __isl_give isl_local_space *isl_local_space_move_dims(
enum isl_dim_type src_type, unsigned src_pos, unsigned n)
{
isl_space *space;
+ isl_local *local;
isl_size v_src, v_dst;
unsigned g_dst_pos;
unsigned g_src_pos;
@@ -1608,8 +1663,6 @@ __isl_give isl_local_space *isl_local_space_move_dims(
"moving dims within the same type not supported",
return isl_local_space_free(ls));

- ls = isl_local_space_cow(ls);
-
v_src = isl_local_space_var_offset(ls, src_type);
v_dst = isl_local_space_var_offset(ls, dst_type);
if (v_src < 0 || v_dst < 0)
@@ -1618,9 +1671,10 @@ __isl_give isl_local_space *isl_local_space_move_dims(
g_dst_pos = v_dst + dst_pos;
if (dst_type > src_type)
g_dst_pos -= n;
- ls->div = isl_local_move_vars(ls->div, g_dst_pos, g_src_pos, n);
- if (!ls->div)
- return isl_local_space_free(ls);
+
+ local = isl_local_space_take_local(ls);
+ local = isl_local_move_vars(local, g_dst_pos, g_src_pos, n);
+ ls = isl_local_space_restore_local(ls, local);

space = isl_local_space_take_space(ls);
space = isl_space_move_dims(space, dst_type, dst_pos,
--
2.25.1

Reply all
Reply to author
Forward
0 new messages