nir: Add hooks for testing serialization
authorJason Ekstrand <jason.ekstrand@intel.com>
Thu, 14 Sep 2017 23:49:53 +0000 (16:49 -0700)
committerJordan Justen <jordan.l.justen@intel.com>
Wed, 1 Nov 2017 06:36:53 +0000 (23:36 -0700)
Reviewed-by: Timothy Arceri <tarceri@itsqueeze.com>
Reviewed-by: Jordan Justen <jordan.l.justen@intel.com>
src/compiler/nir/nir.h
src/compiler/nir/nir_serialize.c

index dd833cf18319b59de8dbeda3e6093eb48230a5ab..87c725625d748da9c2eb1a48e2a7253d8e59fd28 100644 (file)
@@ -2312,6 +2312,8 @@ nir_variable *nir_variable_clone(const nir_variable *c, nir_shader *shader);
 nir_deref *nir_deref_clone(const nir_deref *deref, void *mem_ctx);
 nir_deref_var *nir_deref_var_clone(const nir_deref_var *deref, void *mem_ctx);
 
+nir_shader *nir_shader_serialize_deserialize(void *mem_ctx, nir_shader *s);
+
 #ifdef DEBUG
 void nir_validate_shader(nir_shader *shader);
 void nir_metadata_set_validation_flag(nir_shader *shader);
@@ -2327,6 +2329,16 @@ should_clone_nir(void)
    return should_clone;
 }
 
+static inline bool
+should_serialize_deserialize_nir(void)
+{
+   static int test_serialize = -1;
+   if (test_serialize < 0)
+      test_serialize = env_var_as_boolean("NIR_TEST_SERIALIZE", false);
+
+   return test_serialize;
+}
+
 static inline bool
 should_print_nir(void)
 {
@@ -2341,6 +2353,7 @@ static inline void nir_validate_shader(nir_shader *shader) { (void) shader; }
 static inline void nir_metadata_set_validation_flag(nir_shader *shader) { (void) shader; }
 static inline void nir_metadata_check_validation_flag(nir_shader *shader) { (void) shader; }
 static inline bool should_clone_nir(void) { return false; }
+static inline bool should_serialize_deserialize_nir(void) { return false; }
 static inline bool should_print_nir(void) { return false; }
 #endif /* DEBUG */
 
@@ -2352,6 +2365,10 @@ static inline bool should_print_nir(void) { return false; }
       ralloc_free(nir);                                              \
       nir = clone;                                                   \
    }                                                                 \
+   if (should_serialize_deserialize_nir()) {                         \
+      void *mem_ctx = ralloc_parent(nir);                            \
+      nir = nir_shader_serialize_deserialize(mem_ctx, nir);          \
+   }                                                                 \
 } while (0)
 
 #define NIR_PASS(progress, nir, pass, ...) _PASS(nir,                \
index bb6a5d86f29014808b5098ad0a6d525a3916f34d..7951258a2024db0a6b32c845acb421a36ef7a829 100644 (file)
@@ -1199,3 +1199,22 @@ nir_deserialize(void *mem_ctx,
 
    return ctx.nir;
 }
+
+nir_shader *
+nir_shader_serialize_deserialize(void *mem_ctx, nir_shader *s)
+{
+   const struct nir_shader_compiler_options *options = s->options;
+
+   struct blob writer;
+   blob_init(&writer);
+   nir_serialize(&writer, s);
+   ralloc_free(s);
+
+   struct blob_reader reader;
+   blob_reader_init(&reader, writer.data, writer.size);
+   nir_shader *ns = nir_deserialize(mem_ctx, options, &reader);
+
+   blob_finish(&writer);
+
+   return ns;
+}