add exported isl_fixed_box_read_from_str
authorSven Verdoolaege <sven@cerebras.net>
Wed, 25 Jan 2023 11:22:27 +0000 (25 12:22 +0100)
committerSven Verdoolaege <sven@cerebras.net>
Sat, 28 Jan 2023 13:25:22 +0000 (28 14:25 +0100)
This was arguably missing for a proper export of isl_fixed_box
in isl-0.21-48-g868330757f (export isl_map_get_range_simple_fixed_box_hull,
Tue Oct 23 16:54:20 2018 +0200).

Signed-off-by: Sven Verdoolaege <sven@cerebras.net>
doc/user.pod
include/isl/fixed_box.h
isl_box.c

index e0433a0..f76b19f 100644 (file)
@@ -6499,6 +6499,14 @@ The box can be copied and freed using the following functions.
        __isl_null isl_fixed_box *isl_fixed_box_free(
                __isl_take isl_fixed_box *box);
 
+An object of type C<isl_fixed_box> can be read from input
+using the following function.
+
+       #include <isl/fixed_box.h>
+       __isl_give isl_fixed_box *
+       isl_fixed_box_read_from_str(isl_ctx *ctx,
+               const char *str);
+
 A representation of the information contained in an object
 of type C<isl_fixed_box> can be obtained using
 
index d8c78e3..2debfd0 100644 (file)
@@ -31,6 +31,9 @@ __isl_give isl_multi_val *isl_fixed_box_get_size(__isl_keep isl_fixed_box *box);
 __isl_give isl_fixed_box *isl_fixed_box_copy(__isl_keep isl_fixed_box *box);
 __isl_null isl_fixed_box *isl_fixed_box_free(__isl_take isl_fixed_box *box);
 
+__isl_constructor
+__isl_give isl_fixed_box *isl_fixed_box_read_from_str(isl_ctx *ctx,
+       const char *str);
 __isl_give isl_printer *isl_printer_print_fixed_box(
        __isl_take isl_printer *p, __isl_keep isl_fixed_box *box);
 __isl_give char *isl_fixed_box_to_str(__isl_keep isl_fixed_box *box);
index 88268a9..c674af0 100644 (file)
--- a/isl_box.c
+++ b/isl_box.c
@@ -17,6 +17,7 @@
 #include <isl/constraint.h>
 #include <isl/ilp.h>
 #include <isl/fixed_box.h>
+#include <isl/stream.h>
 
 /* Representation of a box of fixed size containing the elements
  * [offset, offset + size).
@@ -502,8 +503,10 @@ __isl_give isl_fixed_box *isl_set_get_lattice_tile(__isl_keep isl_set *set)
  * of an isl_fixed_box object.
  */
 enum isl_fb_key {
+       isl_fb_key_error = -1,
        isl_fb_key_offset,
        isl_fb_key_size,
+       isl_fb_key_end,
 };
 
 /* Textual representations of the YAML keys for an isl_fixed_box object.
@@ -542,3 +545,92 @@ __isl_give isl_printer *isl_printer_print_fixed_box(
 #undef BASE
 #define BASE fixed_box
 #include <print_templ_yaml.c>
+
+#undef KEY
+#define KEY enum isl_fb_key
+#undef KEY_ERROR
+#define KEY_ERROR isl_fb_key_error
+#undef KEY_END
+#define KEY_END isl_fb_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 multi_val
+#include "read_in_string_templ.c"
+
+#undef BASE
+#define BASE multi_aff
+#include "read_in_string_templ.c"
+
+/* Read an isl_fixed_box object from "s".
+ *
+ * The input needs to contain both an offset and a size.
+ * If either is specified multiple times, then the last specification
+ * overrides all previous ones.  This is simpler than checking
+ * that each is only specified once.
+ */
+static __isl_give isl_fixed_box *isl_stream_read_fixed_box(isl_stream *s)
+{
+       isl_bool more;
+       isl_multi_aff *offset = NULL;
+       isl_multi_val *size = NULL;
+
+       if (isl_stream_yaml_read_start_mapping(s) < 0)
+               return NULL;
+
+       while ((more = isl_stream_yaml_next(s)) == isl_bool_true) {
+               enum isl_fb_key key;
+
+               key = get_key(s);
+               if (isl_stream_yaml_next(s) < 0)
+                       goto error;
+               switch (key) {
+               case isl_fb_key_end:
+               case isl_fb_key_error:
+                       goto error;
+               case isl_fb_key_offset:
+                       isl_multi_aff_free(offset);
+                       offset = read_multi_aff(s);
+                       if (!offset)
+                               goto error;
+                       break;
+               case isl_fb_key_size:
+                       isl_multi_val_free(size);
+                       size = read_multi_val(s);
+                       if (!size)
+                               goto error;
+                       break;
+               }
+       }
+       if (more < 0)
+               goto error;
+
+       if (isl_stream_yaml_read_end_mapping(s) < 0)
+               goto error;
+
+       if (!offset) {
+               isl_stream_error(s, NULL, "no offset specified");
+               goto error;
+       }
+
+       if (!size) {
+               isl_stream_error(s, NULL, "no size specified");
+               goto error;
+       }
+
+       return isl_fixed_box_alloc(offset, size);
+error:
+       isl_multi_aff_free(offset);
+       isl_multi_val_free(size);
+       return NULL;
+}
+
+#undef TYPE_BASE
+#define TYPE_BASE      fixed_box
+#include "isl_read_from_str_templ.c"