util/ra: Add [de]serialization support
authorJason Ekstrand <jason@jlekstrand.net>
Wed, 18 Sep 2019 19:57:11 +0000 (14:57 -0500)
committerMarge Bot <eric+marge@anholt.net>
Wed, 13 May 2020 23:36:44 +0000 (23:36 +0000)
Reviewed-by: Kenneth Graunke <kenneth@whitecape.org>
Reviewed-by: Eric Anholt <eric@anholt.net>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/5019>

src/util/register_allocate.c
src/util/register_allocate.h

index c0cd879c9cac85b300988b17e5c6e81973f9b473..1d9a074174fc297b0ede8847b307efef9cb59dd9 100644 (file)
@@ -73,6 +73,7 @@
 #include <stdbool.h>
 #include <stdlib.h>
 
+#include "blob.h"
 #include "ralloc.h"
 #include "main/macros.h"
 #include "util/bitset.h"
@@ -421,6 +422,68 @@ ra_set_finalize(struct ra_regs *regs, unsigned int **q_values)
    }
 }
 
+void
+ra_set_serialize(const struct ra_regs *regs, struct blob *blob)
+{
+   blob_write_uint32(blob, regs->count);
+   blob_write_uint32(blob, regs->class_count);
+
+   for (unsigned int r = 0; r < regs->count; r++) {
+      struct ra_reg *reg = &regs->regs[r];
+      blob_write_bytes(blob, reg->conflicts, BITSET_WORDS(regs->count) *
+                                             sizeof(BITSET_WORD));
+      assert(util_dynarray_num_elements(&reg->conflict_list, unsigned int) == 0);
+   }
+
+   for (unsigned int c = 0; c < regs->class_count; c++) {
+      struct ra_class *class = regs->classes[c];
+      blob_write_bytes(blob, class->regs, BITSET_WORDS(regs->count) *
+                                          sizeof(BITSET_WORD));
+      blob_write_uint32(blob, class->p);
+      blob_write_bytes(blob, class->q, regs->class_count * sizeof(*class->q));
+   }
+
+   blob_write_uint32(blob, regs->round_robin);
+}
+
+struct ra_regs *
+ra_set_deserialize(void *mem_ctx, struct blob_reader *blob)
+{
+   unsigned int reg_count = blob_read_uint32(blob);
+   unsigned int class_count = blob_read_uint32(blob);
+
+   struct ra_regs *regs = ra_alloc_reg_set(mem_ctx, reg_count, false);
+   assert(regs->count == reg_count);
+
+   for (unsigned int r = 0; r < reg_count; r++) {
+      struct ra_reg *reg = &regs->regs[r];
+      blob_copy_bytes(blob, reg->conflicts, BITSET_WORDS(reg_count) *
+                                            sizeof(BITSET_WORD));
+   }
+
+   assert(regs->classes == NULL);
+   regs->classes = ralloc_array(regs->regs, struct ra_class *, class_count);
+   regs->class_count = class_count;
+
+   for (unsigned int c = 0; c < class_count; c++) {
+      struct ra_class *class = rzalloc(regs, struct ra_class);
+      regs->classes[c] = class;
+
+      class->regs = ralloc_array(class, BITSET_WORD, BITSET_WORDS(reg_count));
+      blob_copy_bytes(blob, class->regs, BITSET_WORDS(reg_count) *
+                                         sizeof(BITSET_WORD));
+
+      class->p = blob_read_uint32(blob);
+
+      class->q = ralloc_array(regs->classes[c], unsigned int, class_count);
+      blob_copy_bytes(blob, class->q, class_count * sizeof(*class->q));
+   }
+
+   regs->round_robin = blob_read_uint32(blob);
+
+   return regs;
+}
+
 static void
 ra_add_node_adjacency(struct ra_graph *g, unsigned int n1, unsigned int n2)
 {
index c01f4aee47af1598ee30296543d5d7ea2d058ef7..c9170e7762a82f0fc4b7bb72c6cb517d225f6a59 100644 (file)
@@ -39,6 +39,9 @@ extern "C" {
 struct ra_class;
 struct ra_regs;
 
+struct blob;
+struct blob_reader;
+
 /* @{
  * Register set setup.
  *
@@ -65,6 +68,9 @@ void ra_class_add_reg(struct ra_regs *regs, unsigned int c, unsigned int reg);
 void ra_set_num_conflicts(struct ra_regs *regs, unsigned int class_a,
                           unsigned int class_b, unsigned int num_conflicts);
 void ra_set_finalize(struct ra_regs *regs, unsigned int **conflicts);
+
+void ra_set_serialize(const struct ra_regs *regs, struct blob *blob);
+struct ra_regs *ra_set_deserialize(void *mem_ctx, struct blob_reader *blob);
 /** @} */
 
 /** @{ Interference graph setup.