<div dir="ltr"><div><div><div>I left a few notes but have *not* reviewed this. I would feel much more comfortable landing it someone went through and verified two things by hand:<br><br></div> 1) We actually fill out all of the data structure fields (clearly things like uses and defs are filled our by core NIR automatically)<br></div> 2) We get the order of blob_read and blob_write right.<br></div><div><br></div><div>That said, most of my confidence in this comes from the next patch where we add absurdly rigorous testing of this code, turn it on, and watch it pass CI. If there's a bug and it doesn't show up with that then it doesn't matter.</div><div><div><div><div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Oct 18, 2017 at 10:31 PM, Jordan Justen <span dir="ltr"><<a href="mailto:jordan.l.justen@intel.com" target="_blank">jordan.l.justen@intel.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">From: Connor Abbott <<a href="mailto:cwabbott0@gmail.com">cwabbott0@gmail.com</a>><br>
<br>
v2 (Jason Ekstrand):<br>
- Various whitespace cleanups<br>
- Add helpers for reading/writing objects<br>
- Rework derefs<br>
- [de]serialize nir_shader::num_*<br>
- Fix uses of blob_reserve_bytes<br>
- Use a bitfield struct for packing tex_instr data<br>
<br>
v3:<br>
- Zero nir_variable struct on deserialization. (Jordan)<br>
- Allow nir_serialize.h to be included in C++. (Jordan)<br>
- Handle NULL <a href="http://info.name" rel="noreferrer" target="_blank">info.name</a>. (Jason)<br>
- Set <a href="http://info.name" rel="noreferrer" target="_blank">info.name</a> to NULL when name is NULL. (Jordan)<br>
---<br>
src/compiler/Makefile.sources | 2 +<br>
src/compiler/nir/meson.build | 2 +<br>
src/compiler/nir/nir_<wbr>serialize.c | 1201 ++++++++++++++++++++++++++++++<wbr>++++++++<br>
src/compiler/nir/nir_<wbr>serialize.h | 43 ++<br>
4 files changed, 1248 insertions(+)<br>
create mode 100644 src/compiler/nir/nir_<wbr>serialize.c<br>
create mode 100644 src/compiler/nir/nir_<wbr>serialize.h<br>
<br>
diff --git a/src/compiler/Makefile.<wbr>sources b/src/compiler/Makefile.<wbr>sources<br>
index 2724a41286..f4d4c17855 100644<br>
--- a/src/compiler/Makefile.<wbr>sources<br>
+++ b/src/compiler/Makefile.<wbr>sources<br>
@@ -276,6 +276,8 @@ NIR_FILES = \<br>
nir/nir_search.c \<br>
nir/nir_search.h \<br>
nir/nir_search_helpers.h \<br>
+ nir/nir_serialize.c \<br>
+ nir/nir_serialize.h \<br>
nir/nir_split_var_copies.c \<br>
nir/nir_sweep.c \<br>
nir/nir_to_lcssa.c \<br>
diff --git a/src/compiler/nir/meson.build b/src/compiler/nir/meson.build<br>
index 144cf01d2c..b4c733dcae 100644<br>
--- a/src/compiler/nir/meson.build<br>
+++ b/src/compiler/nir/meson.build<br>
@@ -164,6 +164,8 @@ files_libnir = files(<br>
'nir_search.c',<br>
'nir_search.h',<br>
'nir_search_helpers.h',<br>
+ 'nir_serialize.c',<br>
+ 'nir_serialize.h',<br>
'nir_split_var_copies.c',<br>
'nir_sweep.c',<br>
'nir_to_lcssa.c',<br>
diff --git a/src/compiler/nir/nir_<wbr>serialize.c b/src/compiler/nir/nir_<wbr>serialize.c<br>
new file mode 100644<br>
index 0000000000..fb6232446b<br>
--- /dev/null<br>
+++ b/src/compiler/nir/nir_<wbr>serialize.c<br>
@@ -0,0 +1,1201 @@<br>
+/*<br>
+ * Copyright © 2017 Connor Abbott<br>
+ *<br>
+ * Permission is hereby granted, free of charge, to any person obtaining a<br>
+ * copy of this software and associated documentation files (the "Software"),<br>
+ * to deal in the Software without restriction, including without limitation<br>
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,<br>
+ * and/or sell copies of the Software, and to permit persons to whom the<br>
+ * Software is furnished to do so, subject to the following conditions:<br>
+ *<br>
+ * The above copyright notice and this permission notice (including the next<br>
+ * paragraph) shall be included in all copies or substantial portions of the<br>
+ * Software.<br>
+ *<br>
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR<br>
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,<br>
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL<br>
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER<br>
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING<br>
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS<br>
+ * IN THE SOFTWARE.<br>
+ */<br>
+<br>
+#include "nir_serialize.h"<br>
+#include "nir_control_flow.h"<br>
+#include "util/u_dynarray.h"<br>
+<br>
+typedef struct {<br>
+ size_t blob_offset;<br>
+ nir_ssa_def *src;<br>
+ nir_block *block;<br>
+} write_phi_fixup;<br>
+<br>
+typedef struct {<br>
+ const nir_shader *nir;<br>
+<br>
+ struct blob *blob;<br>
+<br>
+ /* maps pointer to index */<br>
+ struct hash_table *remap_table;<br>
+<br>
+ /* the next index to assign to a NIR in-memory object */<br>
+ uintptr_t next_idx;<br>
+<br>
+ /* Array of write_phi_fixup structs representing phi sources that need to<br>
+ * be resolved in the second pass.<br>
+ */<br>
+ struct util_dynarray phi_fixups;<br>
+} write_ctx;<br>
+<br>
+typedef struct {<br>
+ nir_shader *nir;<br>
+<br>
+ struct blob_reader *blob;<br>
+<br>
+ /* the next index to assign to a NIR in-memory object */<br>
+ uintptr_t next_idx;<br>
+<br>
+ /* The length of the index -> object table */<br>
+ uintptr_t idx_table_len;<br>
+<br>
+ /* map from index to deserialized pointer */<br>
+ void **idx_table;<br>
+<br>
+ /* List of phi sources. */<br>
+ struct list_head phi_srcs;<br>
+<br>
+} read_ctx;<br>
+<br>
+static void<br>
+write_add_object(write_ctx *ctx, const void *obj)<br>
+{<br>
+ uintptr_t index = ctx->next_idx++;<br>
+ _mesa_hash_table_insert(ctx-><wbr>remap_table, obj, (void *) index);<br>
+}<br>
+<br>
+static uintptr_t<br>
+write_lookup_object(write_ctx *ctx, const void *obj)<br>
+{<br>
+ struct hash_entry *entry = _mesa_hash_table_search(ctx-><wbr>remap_table, obj);<br>
+ assert(entry);<br>
+ return (uintptr_t) entry->data;<br>
+}<br>
+<br>
+static void<br>
+write_object(write_ctx *ctx, const void *obj)<br>
+{<br>
+ blob_write_intptr(ctx->blob, write_lookup_object(ctx, obj));<br>
+}<br>
+<br>
+static void<br>
+read_add_object(read_ctx *ctx, void *obj)<br>
+{<br>
+ assert(ctx->next_idx < ctx->idx_table_len);<br>
+ ctx->idx_table[ctx->next_idx++<wbr>] = obj;<br>
+}<br>
+<br>
+static void *<br>
+read_lookup_object(read_ctx *ctx, uintptr_t idx)<br>
+{<br>
+ assert(idx < ctx->idx_table_len);<br>
+ return ctx->idx_table[idx];<br>
+}<br>
+<br>
+static void *<br>
+read_object(read_ctx *ctx)<br>
+{<br>
+ return read_lookup_object(ctx, blob_read_intptr(ctx->blob));<br>
+}<br></blockquote><div><br></div><div>I don't expect you do do anything with this now, but there are a few things I think we want to eventually do for sanity/security checks. I wanted to get it on the list so that it's written down. If we do these three things, I think we can reasonably guarantee that we at least generate a correct NIR data structure even if the code is bogus and may hang your GPU:</div><div><br></div><div> 1) Bounds-check read_add_object and read_lookup_object. We already have asserts, but we need something that will trigger a non-fatal failure in release mode.</div><div> 2) Add a nir_object_type enum and pass it as a parameter to read_add_object and read_lookup_object to ensure that passing things through void * doesn't cause us to accidentally interpret a nir_variable as a nir_ssa_def or something like that.</div><div> 3) Be careful about blob overruns, especially where pointers are concerned. Really, I think if read_object() checks for overrun between blob_read_intptr and read_lookup_object, that will take care of most of it. We would probably want some sort of read_fail_on_blob_overrun() helper and we would need to do a full audit to make sure we called it all the needed places.</div><div><br></div><div>As far as how to actually implement this goes, I think this would be a good place to use setjmp/longjmp. It can be tricky and dangerous in the wrong circumstances, but this is a case where everything is ralloc'd and the setjmp/longjmp pair will never leave this file so I think we can be reasonably certain that it's safe.<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+<br>
+static void<br>
+write_constant(write_ctx *ctx, const nir_constant *c)<br>
+{<br>
+ blob_write_bytes(ctx->blob, c->values, sizeof(c->values));<br>
+ blob_write_uint32(ctx->blob, c->num_elements);<br>
+ for (unsigned i = 0; i < c->num_elements; i++)<br>
+ write_constant(ctx, c->elements[i]);<br>
+}<br>
+<br>
+static nir_constant *<br>
+read_constant(read_ctx *ctx, nir_variable *nvar)<br>
+{<br>
+ nir_constant *c = ralloc(nvar, nir_constant);<br>
+<br>
+ blob_copy_bytes(ctx->blob, (uint8_t *)c->values, sizeof(c->values));<br>
+ c->num_elements = blob_read_uint32(ctx->blob);<br>
+ c->elements = ralloc_array(ctx->nir, nir_constant *, c->num_elements);<br>
+ for (unsigned i = 0; i < c->num_elements; i++)<br>
+ c->elements[i] = read_constant(ctx, nvar);<br>
+<br>
+ return c;<br>
+}<br>
+<br>
+static void<br>
+write_variable(write_ctx *ctx, const nir_variable *var)<br>
+{<br>
+ write_add_object(ctx, var);<br>
+ encode_type_to_blob(ctx->blob, var->type);<br>
+ blob_write_uint32(ctx->blob, !!(var->name));<br>
+ blob_write_string(ctx->blob, var->name);<br>
+ blob_write_bytes(ctx->blob, (uint8_t *) &var->data, sizeof(var->data));<br>
+ blob_write_uint32(ctx->blob, var->num_state_slots);<br>
+ blob_write_bytes(ctx->blob, (uint8_t *) var->state_slots,<br>
+ var->num_state_slots * sizeof(nir_state_slot));<br>
+ blob_write_uint32(ctx->blob, !!(var->constant_initializer))<wbr>;<br>
+ if (var->constant_initializer)<br>
+ write_constant(ctx, var->constant_initializer);<br>
+ blob_write_uint32(ctx->blob, !!(var->interface_type));<br>
+ if (var->interface_type)<br>
+ encode_type_to_blob(ctx->blob, var->interface_type);<br>
+}<br>
+<br>
+static nir_variable *<br>
+read_variable(read_ctx *ctx)<br>
+{<br>
+ nir_variable *var = rzalloc(ctx->nir, nir_variable);<br>
+ read_add_object(ctx, var);<br>
+<br>
+ var->type = decode_type_from_blob(ctx-><wbr>blob);<br>
+ bool has_name = blob_read_uint32(ctx->blob);<br>
+ if (has_name) {<br>
+ const char *name = blob_read_string(ctx->blob);<br>
+ var->name = ralloc_strdup(var, name);<br>
+ } else {<br>
+ var->name = NULL;<br>
+ }<br>
+ blob_copy_bytes(ctx->blob, (uint8_t *) &var->data, sizeof(var->data));<br>
+ var->num_state_slots = blob_read_uint32(ctx->blob);<br>
+ var->state_slots = ralloc_array(var, nir_state_slot, var->num_state_slots);<br>
+ blob_copy_bytes(ctx->blob, (uint8_t *) var->state_slots,<br>
+ var->num_state_slots * sizeof(nir_state_slot));<br>
+ bool has_const_initializer = blob_read_uint32(ctx->blob);<br>
+ if (has_const_initializer)<br>
+ var->constant_initializer = read_constant(ctx, var);<br>
+ else<br>
+ var->constant_initializer = NULL;<br>
+ bool has_interface_type = blob_read_uint32(ctx->blob);<br>
+ if (has_interface_type)<br>
+ var->interface_type = decode_type_from_blob(ctx-><wbr>blob);<br>
+ else<br>
+ var->interface_type = NULL;<br>
+<br>
+ return var;<br>
+}<br>
+<br>
+static void<br>
+write_var_list(write_ctx *ctx, const struct exec_list *src)<br>
+{<br>
+ blob_write_uint32(ctx->blob, exec_list_length(src));<br>
+ foreach_list_typed(nir_<wbr>variable, var, node, src) {<br>
+ write_variable(ctx, var);<br>
+ }<br>
+}<br>
+<br>
+static void<br>
+read_var_list(read_ctx *ctx, struct exec_list *dst)<br>
+{<br>
+ exec_list_make_empty(dst);<br>
+ unsigned num_vars = blob_read_uint32(ctx->blob);<br>
+ for (unsigned i = 0; i < num_vars; i++) {<br>
+ nir_variable *var = read_variable(ctx);<br>
+ exec_list_push_tail(dst, &var->node);<br>
+ }<br>
+}<br>
+<br>
+static void<br>
+write_register(write_ctx *ctx, const nir_register *reg)<br>
+{<br>
+ write_add_object(ctx, reg);<br>
+ blob_write_uint32(ctx->blob, reg->num_components);<br>
+ blob_write_uint32(ctx->blob, reg->bit_size);<br>
+ blob_write_uint32(ctx->blob, reg->num_array_elems);<br>
+ blob_write_uint32(ctx->blob, reg->index);<br>
+ blob_write_uint32(ctx->blob, !!(reg->name));<br>
+ if (reg->name)<br>
+ blob_write_string(ctx->blob, reg->name);<br>
+ blob_write_uint32(ctx->blob, reg->is_global << 1 | reg->is_packed);<br>
+}<br>
+<br>
+static nir_register *<br>
+read_register(read_ctx *ctx)<br>
+{<br>
+ nir_register *reg = ralloc(ctx->nir, nir_register);<br>
+ read_add_object(ctx, reg);<br>
+ reg->num_components = blob_read_uint32(ctx->blob);<br>
+ reg->bit_size = blob_read_uint32(ctx->blob);<br>
+ reg->num_array_elems = blob_read_uint32(ctx->blob);<br>
+ reg->index = blob_read_uint32(ctx->blob);<br>
+ bool has_name = blob_read_uint32(ctx->blob);<br>
+ if (has_name) {<br>
+ const char *name = blob_read_string(ctx->blob);<br>
+ reg->name = ralloc_strdup(reg, name);<br>
+ } else {<br>
+ reg->name = NULL;<br>
+ }<br>
+ unsigned flags = blob_read_uint32(ctx->blob);<br>
+ reg->is_global = flags & 0x2;<br>
+ reg->is_packed = flags & 0x1;<br>
+<br>
+ list_inithead(®->uses);<br>
+ list_inithead(®->defs);<br>
+ list_inithead(®->if_uses);<br>
+<br>
+ return reg;<br>
+}<br>
+<br>
+static void<br>
+write_reg_list(write_ctx *ctx, const struct exec_list *src)<br>
+{<br>
+ blob_write_uint32(ctx->blob, exec_list_length(src));<br>
+ foreach_list_typed(nir_<wbr>register, reg, node, src)<br>
+ write_register(ctx, reg);<br>
+}<br>
+<br>
+static void<br>
+read_reg_list(read_ctx *ctx, struct exec_list *dst)<br>
+{<br>
+ exec_list_make_empty(dst);<br>
+ unsigned num_regs = blob_read_uint32(ctx->blob);<br>
+ for (unsigned i = 0; i < num_regs; i++) {<br>
+ nir_register *reg = read_register(ctx);<br>
+ exec_list_push_tail(dst, ®->node);<br>
+ }<br>
+}<br>
+<br>
+static void<br>
+write_src(write_ctx *ctx, const nir_src *src)<br>
+{<br>
+ /* Since sources are very frequent, we try to save some space when storing<br>
+ * them. In particular, we store whether the source is a register and<br>
+ * whether the register has an indirect index in the low two bits. We can<br>
+ * assume that the high two bits of the index are zero, since otherwise our<br>
+ * address space would've been exhausted allocating the remap table!<br>
+ */<br>
+ if (src->is_ssa) {<br>
+ uintptr_t idx = write_lookup_object(ctx, src->ssa) << 2;<br>
+ idx |= 1;<br>
+ blob_write_intptr(ctx->blob, idx);<br>
+ } else {<br>
+ uintptr_t idx = write_lookup_object(ctx, src->reg.reg) << 2;<br>
+ if (src->reg.indirect)<br>
+ idx |= 2;<br>
+ blob_write_intptr(ctx->blob, idx);<br>
+ blob_write_uint32(ctx->blob, src->reg.base_offset);<br>
+ if (src->reg.indirect) {<br>
+ write_src(ctx, src->reg.indirect);<br>
+ }<br>
+ }<br>
+}<br>
+<br>
+static void<br>
+read_src(read_ctx *ctx, nir_src *src, void *mem_ctx)<br>
+{<br>
+ uintptr_t val = blob_read_intptr(ctx->blob);<br>
+ uintptr_t idx = val >> 2;<br>
+ src->is_ssa = val & 0x1;<br>
+ if (src->is_ssa) {<br>
+ src->ssa = read_lookup_object(ctx, idx);<br>
+ } else {<br>
+ bool is_indirect = val & 0x2;<br>
+ src->reg.reg = read_lookup_object(ctx, idx);<br>
+ src->reg.base_offset = blob_read_uint32(ctx->blob);<br>
+ if (is_indirect) {<br>
+ src->reg.indirect = ralloc(mem_ctx, nir_src);<br>
+ read_src(ctx, src->reg.indirect, mem_ctx);<br>
+ } else {<br>
+ src->reg.indirect = NULL;<br>
+ }<br>
+ }<br>
+}<br>
+<br>
+static void<br>
+write_dest(write_ctx *ctx, const nir_dest *dst)<br>
+{<br>
+ uint32_t val = dst->is_ssa;<br>
+ if (dst->is_ssa) {<br>
+ val |= !!(dst-><a href="http://ssa.name" rel="noreferrer" target="_blank">ssa.name</a>) << 1;<br>
+ val |= dst->ssa.num_components << 2;<br>
+ val |= dst->ssa.bit_size << 5;<br>
+ } else {<br>
+ val |= !!(dst->reg.indirect) << 1;<br>
+ }<br>
+ blob_write_uint32(ctx->blob, val);<br>
+ if (dst->is_ssa) {<br>
+ write_add_object(ctx, &dst->ssa);<br>
+ if (dst-><a href="http://ssa.name" rel="noreferrer" target="_blank">ssa.name</a>)<br>
+ blob_write_string(ctx->blob, dst-><a href="http://ssa.name" rel="noreferrer" target="_blank">ssa.name</a>);<br>
+ } else {<br>
+ blob_write_intptr(ctx->blob, write_lookup_object(ctx, dst->reg.reg));<br>
+ blob_write_uint32(ctx->blob, dst->reg.base_offset);<br>
+ if (dst->reg.indirect)<br>
+ write_src(ctx, dst->reg.indirect);<br>
+ }<br>
+}<br>
+<br>
+static void<br>
+read_dest(read_ctx *ctx, nir_dest *dst, nir_instr *instr)<br>
+{<br>
+ uint32_t val = blob_read_uint32(ctx->blob);<br>
+ bool is_ssa = val & 0x1;<br>
+ if (is_ssa) {<br>
+ bool has_name = val & 0x2;<br>
+ unsigned num_components = (val >> 2) & 0x7;<br>
+ unsigned bit_size = val >> 5;<br>
+ char *name = has_name ? blob_read_string(ctx->blob) : NULL;<br>
+ nir_ssa_dest_init(instr, dst, num_components, bit_size, name);<br>
+ read_add_object(ctx, &dst->ssa);<br>
+ } else {<br>
+ bool is_indirect = val & 0x2;<br>
+ dst->reg.reg = read_object(ctx);<br>
+ dst->reg.base_offset = blob_read_uint32(ctx->blob);<br>
+ if (is_indirect) {<br>
+ dst->reg.indirect = ralloc(instr, nir_src);<br>
+ read_src(ctx, dst->reg.indirect, instr);<br>
+ }<br>
+ }<br>
+}<br>
+<br>
+static void<br>
+write_deref_chain(write_ctx *ctx, const nir_deref_var *deref_var)<br>
+{<br>
+ write_object(ctx, deref_var->var);<br>
+<br>
+ uint32_t len = 0;<br>
+ for (const nir_deref *d = deref_var->deref.child; d; d = d->child)<br>
+ len++;<br>
+ blob_write_uint32(ctx->blob, len);<br>
+<br>
+ for (const nir_deref *d = deref_var->deref.child; d; d = d->child) {<br>
+ blob_write_uint32(ctx->blob, d->deref_type);<br>
+ switch (d->deref_type) {<br>
+ case nir_deref_type_array: {<br>
+ const nir_deref_array *deref_array = nir_deref_as_array(d);<br>
+ blob_write_uint32(ctx->blob, deref_array->deref_array_type)<wbr>;<br>
+ blob_write_uint32(ctx->blob, deref_array->base_offset);<br>
+ if (deref_array->deref_array_type == nir_deref_array_type_indirect)<br>
+ write_src(ctx, &deref_array->indirect);<br>
+ break;<br>
+ }<br>
+ case nir_deref_type_struct: {<br>
+ const nir_deref_struct *deref_struct = nir_deref_as_struct(d);<br>
+ blob_write_uint32(ctx->blob, deref_struct->index);<br>
+ break;<br>
+ }<br>
+ case nir_deref_type_var:<br>
+ unreachable("Invalid deref type");<br>
+ }<br>
+<br>
+ encode_type_to_blob(ctx->blob, d->type);<br>
+ }<br>
+}<br>
+<br>
+static nir_deref_var *<br>
+read_deref_chain(read_ctx *ctx, void *mem_ctx)<br>
+{<br>
+ nir_variable *var = read_object(ctx);<br>
+ nir_deref_var *deref_var = nir_deref_var_create(mem_ctx, var);<br>
+<br>
+ uint32_t len = blob_read_uint32(ctx->blob);<br>
+<br>
+ nir_deref *tail = &deref_var->deref;<br>
+ for (uint32_t i = 0; i < len; i++) {<br>
+ nir_deref_type deref_type = blob_read_uint32(ctx->blob);<br>
+ nir_deref *deref;<br>
+ switch (deref_type) {<br>
+ case nir_deref_type_array: {<br>
+ nir_deref_array *deref_array = nir_deref_array_create(tail);<br>
+ deref_array->deref_array_type = blob_read_uint32(ctx->blob);<br>
+ deref_array->base_offset = blob_read_uint32(ctx->blob);<br>
+ if (deref_array->deref_array_type == nir_deref_array_type_indirect)<br>
+ read_src(ctx, &deref_array->indirect, mem_ctx);<br>
+ deref = &deref_array->deref;<br>
+ break;<br>
+ }<br>
+ case nir_deref_type_struct: {<br>
+ uint32_t index = blob_read_uint32(ctx->blob);<br>
+ nir_deref_struct *deref_struct = nir_deref_struct_create(tail, index);<br>
+ deref = &deref_struct->deref;<br>
+ break;<br>
+ }<br>
+ case nir_deref_type_var:<br>
+ unreachable("Invalid deref type");<br>
+ }<br>
+<br>
+ deref->type = decode_type_from_blob(ctx-><wbr>blob);<br>
+<br>
+ tail->child = deref;<br>
+ tail = deref;<br>
+ }<br>
+<br>
+ return deref_var;<br>
+}<br>
+<br>
+static void<br>
+write_alu(write_ctx *ctx, const nir_alu_instr *alu)<br>
+{<br>
+ blob_write_uint32(ctx->blob, alu->op);<br>
+ uint32_t flags = alu->exact;<br>
+ flags |= alu->dest.saturate << 1;<br>
+ flags |= alu->dest.write_mask << 2;<br>
+ blob_write_uint32(ctx->blob, flags);<br>
+<br>
+ write_dest(ctx, &alu->dest.dest);<br>
+<br>
+ for (unsigned i = 0; i < nir_op_infos[alu->op].num_<wbr>inputs; i++) {<br>
+ write_src(ctx, &alu->src[i].src);<br>
+ flags = alu->src[i].negate;<br>
+ flags |= alu->src[i].abs << 1;<br>
+ for (unsigned j = 0; j < 4; j++)<br>
+ flags |= alu->src[i].swizzle[j] << (2 + 2 * j);<br>
+ blob_write_uint32(ctx->blob, flags);<br>
+ }<br>
+}<br>
+<br>
+static nir_alu_instr *<br>
+read_alu(read_ctx *ctx)<br>
+{<br>
+ nir_op op = blob_read_uint32(ctx->blob);<br>
+ nir_alu_instr *alu = nir_alu_instr_create(ctx->nir, op);<br>
+<br>
+ uint32_t flags = blob_read_uint32(ctx->blob);<br>
+ alu->exact = flags & 1;<br>
+ alu->dest.saturate = flags & 2;<br>
+ alu->dest.write_mask = flags >> 2;<br>
+<br>
+ read_dest(ctx, &alu->dest.dest, &alu->instr);<br>
+<br>
+ for (unsigned i = 0; i < nir_op_infos[op].num_inputs; i++) {<br>
+ read_src(ctx, &alu->src[i].src, &alu->instr);<br>
+ flags = blob_read_uint32(ctx->blob);<br>
+ alu->src[i].negate = flags & 1;<br>
+ alu->src[i].abs = flags & 2;<br>
+ for (unsigned j = 0; j < 4; j++)<br>
+ alu->src[i].swizzle[j] = (flags >> (2 * j + 2)) & 3;<br>
+ }<br>
+<br>
+ return alu;<br>
+}<br>
+<br>
+static void<br>
+write_intrinsic(write_ctx *ctx, const nir_intrinsic_instr *intrin)<br>
+{<br>
+ blob_write_uint32(ctx->blob, intrin->intrinsic);<br>
+<br>
+ unsigned num_variables = nir_intrinsic_infos[intrin-><wbr>intrinsic].num_variables;<br>
+ unsigned num_srcs = nir_intrinsic_infos[intrin-><wbr>intrinsic].num_srcs;<br>
+ unsigned num_indices = nir_intrinsic_infos[intrin-><wbr>intrinsic].num_indices;<br>
+<br>
+ blob_write_uint32(ctx->blob, intrin->num_components);<br>
+<br>
+ if (nir_intrinsic_infos[intrin-><wbr>intrinsic].has_dest)<br>
+ write_dest(ctx, &intrin->dest);<br>
+<br>
+ for (unsigned i = 0; i < num_variables; i++)<br>
+ write_deref_chain(ctx, intrin->variables[i]);<br>
+<br>
+ for (unsigned i = 0; i < num_srcs; i++)<br>
+ write_src(ctx, &intrin->src[i]);<br>
+<br>
+ for (unsigned i = 0; i < num_indices; i++)<br>
+ blob_write_uint32(ctx->blob, intrin->const_index[i]);<br>
+}<br>
+<br>
+static nir_intrinsic_instr *<br>
+read_intrinsic(read_ctx *ctx)<br>
+{<br>
+ nir_intrinsic_op op = blob_read_uint32(ctx->blob);<br>
+<br>
+ nir_intrinsic_instr *intrin = nir_intrinsic_instr_create(<wbr>ctx->nir, op);<br>
+<br>
+ unsigned num_variables = nir_intrinsic_infos[op].num_<wbr>variables;<br>
+ unsigned num_srcs = nir_intrinsic_infos[op].num_<wbr>srcs;<br>
+ unsigned num_indices = nir_intrinsic_infos[op].num_<wbr>indices;<br>
+<br>
+ intrin->num_components = blob_read_uint32(ctx->blob);<br>
+<br>
+ if (nir_intrinsic_infos[op].has_<wbr>dest)<br>
+ read_dest(ctx, &intrin->dest, &intrin->instr);<br>
+<br>
+ for (unsigned i = 0; i < num_variables; i++)<br>
+ intrin->variables[i] = read_deref_chain(ctx, &intrin->instr);<br>
+<br>
+ for (unsigned i = 0; i < num_srcs; i++)<br>
+ read_src(ctx, &intrin->src[i], &intrin->instr);<br>
+<br>
+ for (unsigned i = 0; i < num_indices; i++)<br>
+ intrin->const_index[i] = blob_read_uint32(ctx->blob);<br>
+<br>
+ return intrin;<br>
+}<br>
+<br>
+static void<br>
+write_load_const(write_ctx *ctx, const nir_load_const_instr *lc)<br>
+{<br>
+ uint32_t val = lc->def.num_components;<br>
+ val |= lc->def.bit_size << 3;<br>
+ blob_write_uint32(ctx->blob, val);<br>
+ blob_write_bytes(ctx->blob, (uint8_t *) &lc->value, sizeof(lc->value));<br>
+ write_add_object(ctx, &lc->def);<br>
+}<br>
+<br>
+static nir_load_const_instr *<br>
+read_load_const(read_ctx *ctx)<br>
+{<br>
+ uint32_t val = blob_read_uint32(ctx->blob);<br>
+<br>
+ nir_load_const_instr *lc =<br>
+ nir_load_const_instr_create(<wbr>ctx->nir, val & 0x7, val >> 3);<br>
+<br>
+ blob_copy_bytes(ctx->blob, (uint8_t *) &lc->value, sizeof(lc->value));<br>
+ read_add_object(ctx, &lc->def);<br>
+ return lc;<br>
+}<br>
+<br>
+static void<br>
+write_ssa_undef(write_ctx *ctx, const nir_ssa_undef_instr *undef)<br>
+{<br>
+ uint32_t val = undef->def.num_components;<br>
+ val |= undef->def.bit_size << 3;<br>
+ blob_write_uint32(ctx->blob, val);<br>
+ write_add_object(ctx, &undef->def);<br>
+}<br>
+<br>
+static nir_ssa_undef_instr *<br>
+read_ssa_undef(read_ctx *ctx)<br>
+{<br>
+ uint32_t val = blob_read_uint32(ctx->blob);<br>
+<br>
+ nir_ssa_undef_instr *undef =<br>
+ nir_ssa_undef_instr_create(<wbr>ctx->nir, val & 0x7, val >> 3);<br>
+<br>
+ read_add_object(ctx, &undef->def);<br>
+ return undef;<br>
+}<br>
+<br>
+union packed_tex_data {<br>
+ uint32_t u32;<br>
+ struct {<br>
+ enum glsl_sampler_dim sampler_dim:4;<br>
+ nir_alu_type dest_type:8;<br>
+ unsigned coord_components:3;<br>
+ unsigned is_array:1;<br>
+ unsigned is_shadow:1;<br>
+ unsigned is_new_style_shadow:1;<br>
+ unsigned component:2;<br>
+ unsigned has_texture_deref:1;<br>
+ unsigned has_sampler_deref:1;<br>
+ };<br>
+};<br>
+<br>
+static void<br>
+write_tex(write_ctx *ctx, const nir_tex_instr *tex)<br>
+{<br>
+ blob_write_uint32(ctx->blob, tex->num_srcs);<br>
+ blob_write_uint32(ctx->blob, tex->op);<br>
+ blob_write_uint32(ctx->blob, tex->texture_index);<br>
+ blob_write_uint32(ctx->blob, tex->texture_array_size);<br>
+ blob_write_uint32(ctx->blob, tex->sampler_index);<br>
+<br>
+ STATIC_ASSERT(sizeof(union packed_tex_data) == sizeof(uint32_t));<br>
+ union packed_tex_data packed = {<br>
+ .sampler_dim = tex->sampler_dim,<br>
+ .dest_type = tex->dest_type,<br>
+ .coord_components = tex->coord_components,<br>
+ .is_array = tex->is_array,<br>
+ .is_shadow = tex->is_shadow,<br>
+ .is_new_style_shadow = tex->is_new_style_shadow,<br>
+ .component = tex->component,<br>
+ .has_texture_deref = tex->texture != NULL,<br>
+ .has_sampler_deref = tex->sampler != NULL,<br>
+ };<br>
+ blob_write_uint32(ctx->blob, packed.u32);<br>
+<br>
+ write_dest(ctx, &tex->dest);<br>
+ for (unsigned i = 0; i < tex->num_srcs; i++) {<br>
+ blob_write_uint32(ctx->blob, tex->src[i].src_type);<br>
+ write_src(ctx, &tex->src[i].src);<br>
+ }<br>
+<br>
+ if (tex->texture)<br>
+ write_deref_chain(ctx, tex->texture);<br>
+ if (tex->sampler)<br>
+ write_deref_chain(ctx, tex->sampler);<br>
+}<br>
+<br>
+static nir_tex_instr *<br>
+read_tex(read_ctx *ctx)<br>
+{<br>
+ unsigned num_srcs = blob_read_uint32(ctx->blob);<br>
+ nir_tex_instr *tex = nir_tex_instr_create(ctx->nir, num_srcs);<br>
+<br>
+ tex->op = blob_read_uint32(ctx->blob);<br>
+ tex->texture_index = blob_read_uint32(ctx->blob);<br>
+ tex->texture_array_size = blob_read_uint32(ctx->blob);<br>
+ tex->sampler_index = blob_read_uint32(ctx->blob);<br>
+<br>
+ union packed_tex_data packed;<br>
+ packed.u32 = blob_read_uint32(ctx->blob);<br>
+ tex->sampler_dim = packed.sampler_dim;<br>
+ tex->dest_type = packed.dest_type;<br>
+ tex->coord_components = packed.coord_components;<br>
+ tex->is_array = packed.is_array;<br>
+ tex->is_shadow = packed.is_shadow;<br>
+ tex->is_new_style_shadow = packed.is_new_style_shadow;<br>
+ tex->component = packed.component;<br>
+<br>
+ read_dest(ctx, &tex->dest, &tex->instr);<br>
+ for (unsigned i = 0; i < tex->num_srcs; i++) {<br>
+ tex->src[i].src_type = blob_read_uint32(ctx->blob);<br>
+ read_src(ctx, &tex->src[i].src, &tex->instr);<br>
+ }<br>
+<br>
+ tex->texture = packed.has_texture_deref ?<br>
+ read_deref_chain(ctx, &tex->instr) : NULL;<br>
+ tex->sampler = packed.has_sampler_deref ?<br>
+ read_deref_chain(ctx, &tex->instr) : NULL;<br>
+<br>
+ return tex;<br>
+}<br>
+<br>
+static void<br>
+write_phi(write_ctx *ctx, const nir_phi_instr *phi)<br>
+{<br>
+ /* Phi nodes are special, since they may reference SSA definitions and<br>
+ * basic blocks that don't exist yet. We leave two empty uintptr_t's here,<br>
+ * and then store enough information so that a later fixup pass can fill<br>
+ * them in correctly.<br>
+ */<br>
+ write_dest(ctx, &phi->dest);<br>
+<br>
+ blob_write_uint32(ctx->blob, exec_list_length(&phi->srcs));<br>
+<br>
+ nir_foreach_phi_src(src, phi) {<br>
+ assert(src->src.is_ssa);<br>
+ size_t blob_offset = blob_reserve_intptr(ctx->blob)<wbr>;<br>
+ size_t blob_offset2 = blob_reserve_intptr(ctx->blob)<wbr>;<br></blockquote><div><br></div><div>These should probably be intptr_t.<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+ assert(blob_offset + sizeof(uintptr_t) == blob_offset2);<br>
+ write_phi_fixup fixup = {<br>
+ .blob_offset = blob_offset,<br>
+ .src = src->src.ssa,<br>
+ .block = src->pred,<br>
+ };<br>
+ util_dynarray_append(&ctx-><wbr>phi_fixups, write_phi_fixup, fixup);<br>
+ }<br>
+}<br>
+<br>
+static void<br>
+write_fixup_phis(write_ctx *ctx)<br>
+{<br>
+ util_dynarray_foreach(&ctx-><wbr>phi_fixups, write_phi_fixup, fixup) {<br>
+ uintptr_t *blob_ptr = (uintptr_t *)(ctx->blob->data + fixup->blob_offset);<br>
+ blob_ptr[0] = write_lookup_object(ctx, fixup->src);<br>
+ blob_ptr[1] = write_lookup_object(ctx, fixup->block);<br></blockquote><div><br></div><div>We could use blob_overwrite_intptr here instead but this is fine.<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+ }<br>
+<br>
+ util_dynarray_clear(&ctx->phi_<wbr>fixups);<br>
+}<br>
+<br>
+static nir_phi_instr *<br>
+read_phi(read_ctx *ctx, nir_block *blk)<br>
+{<br>
+ nir_phi_instr *phi = nir_phi_instr_create(ctx->nir)<wbr>;<br>
+<br>
+ read_dest(ctx, &phi->dest, &phi->instr);<br>
+<br>
+ unsigned num_srcs = blob_read_uint32(ctx->blob);<br>
+<br>
+ /* For similar reasons as before, we just store the index directly into the<br>
+ * pointer, and let a later pass resolve the phi sources.<br>
+ *<br>
+ * In order to ensure that the copied sources (which are just the indices<br>
+ * from the blob for now) don't get inserted into the old shader's use-def<br>
+ * lists, we have to add the phi instruction *before* we set up its<br>
+ * sources.<br>
+ */<br>
+ nir_instr_insert_after_block(<wbr>blk, &phi->instr);<br>
+<br>
+ for (unsigned i = 0; i < num_srcs; i++) {<br>
+ nir_phi_src *src = ralloc(phi, nir_phi_src);<br>
+<br>
+ src->src.is_ssa = true;<br>
+ src->src.ssa = (nir_ssa_def *) blob_read_intptr(ctx->blob);<br>
+ src->pred = (nir_block *) blob_read_intptr(ctx->blob);<br>
+<br>
+ /* Since we're not letting nir_insert_instr handle use/def stuff for us,<br>
+ * we have to set the parent_instr manually. It doesn't really matter<br>
+ * when we do it, so we might as well do it here.<br>
+ */<br>
+ src->src.parent_instr = &phi->instr;<br>
+<br>
+ /* Stash it in the list of phi sources. We'll walk this list and fix up<br>
+ * sources at the very end of read_function_impl.<br>
+ */<br>
+ list_add(&src->src.use_link, &ctx->phi_srcs);<br>
+<br>
+ exec_list_push_tail(&phi-><wbr>srcs, &src->node);<br>
+ }<br>
+<br>
+ return phi;<br>
+}<br>
+<br>
+static void<br>
+read_fixup_phis(read_ctx *ctx)<br>
+{<br>
+ list_for_each_entry_safe(nir_<wbr>phi_src, src, &ctx->phi_srcs, src.use_link) {<br>
+ src->pred = read_lookup_object(ctx, (uintptr_t)src->pred);<br>
+ src->src.ssa = read_lookup_object(ctx, (uintptr_t)src->src.ssa);<br>
+<br>
+ /* Remove from this list */<br>
+ list_del(&src->src.use_link);<br>
+<br>
+ list_addtail(&src->src.use_<wbr>link, &src->src.ssa->uses);<br>
+ }<br>
+ assert(list_empty(&ctx->phi_<wbr>srcs));<br>
+}<br>
+<br>
+static void<br>
+write_jump(write_ctx *ctx, const nir_jump_instr *jmp)<br>
+{<br>
+ blob_write_uint32(ctx->blob, jmp->type);<br>
+}<br>
+<br>
+static nir_jump_instr *<br>
+read_jump(read_ctx *ctx)<br>
+{<br>
+ nir_jump_type type = blob_read_uint32(ctx->blob);<br>
+ nir_jump_instr *jmp = nir_jump_instr_create(ctx-><wbr>nir, type);<br>
+ return jmp;<br>
+}<br>
+<br>
+static void<br>
+write_call(write_ctx *ctx, const nir_call_instr *call)<br>
+{<br>
+ blob_write_intptr(ctx->blob, write_lookup_object(ctx, call->callee));<br>
+<br>
+ for (unsigned i = 0; i < call->num_params; i++)<br>
+ write_deref_chain(ctx, call->params[i]);<br>
+<br>
+ write_deref_chain(ctx, call->return_deref);<br>
+}<br>
+<br>
+static nir_call_instr *<br>
+read_call(read_ctx *ctx)<br>
+{<br>
+ nir_function *callee = read_object(ctx);<br>
+ nir_call_instr *call = nir_call_instr_create(ctx-><wbr>nir, callee);<br>
+<br>
+ for (unsigned i = 0; i < call->num_params; i++)<br>
+ call->params[i] = read_deref_chain(ctx, &call->instr);<br>
+<br>
+ call->return_deref = read_deref_chain(ctx, &call->instr);<br>
+<br>
+ return call;<br>
+}<br>
+<br>
+static void<br>
+write_instr(write_ctx *ctx, const nir_instr *instr)<br>
+{<br>
+ blob_write_uint32(ctx->blob, instr->type);<br>
+ switch (instr->type) {<br>
+ case nir_instr_type_alu:<br>
+ write_alu(ctx, nir_instr_as_alu(instr));<br>
+ break;<br>
+ case nir_instr_type_intrinsic:<br>
+ write_intrinsic(ctx, nir_instr_as_intrinsic(instr))<wbr>;<br>
+ break;<br>
+ case nir_instr_type_load_const:<br>
+ write_load_const(ctx, nir_instr_as_load_const(instr)<wbr>);<br>
+ break;<br>
+ case nir_instr_type_ssa_undef:<br>
+ write_ssa_undef(ctx, nir_instr_as_ssa_undef(instr))<wbr>;<br>
+ break;<br>
+ case nir_instr_type_tex:<br>
+ write_tex(ctx, nir_instr_as_tex(instr));<br>
+ break;<br>
+ case nir_instr_type_phi:<br>
+ write_phi(ctx, nir_instr_as_phi(instr));<br>
+ break;<br>
+ case nir_instr_type_jump:<br>
+ write_jump(ctx, nir_instr_as_jump(instr));<br>
+ break;<br>
+ case nir_instr_type_call:<br>
+ write_call(ctx, nir_instr_as_call(instr));<br>
+ break;<br>
+ case nir_instr_type_parallel_copy:<br>
+ unreachable("Cannot write parallel copies");<br>
+ default:<br>
+ unreachable("bad instr type");<br>
+ }<br>
+}<br>
+<br>
+static void<br>
+read_instr(read_ctx *ctx, nir_block *block)<br>
+{<br>
+ nir_instr_type type = blob_read_uint32(ctx->blob);<br>
+ nir_instr *instr;<br>
+ switch (type) {<br>
+ case nir_instr_type_alu:<br>
+ instr = &read_alu(ctx)->instr;<br>
+ break;<br>
+ case nir_instr_type_intrinsic:<br>
+ instr = &read_intrinsic(ctx)->instr;<br>
+ break;<br>
+ case nir_instr_type_load_const:<br>
+ instr = &read_load_const(ctx)->instr;<br>
+ break;<br>
+ case nir_instr_type_ssa_undef:<br>
+ instr = &read_ssa_undef(ctx)->instr;<br>
+ break;<br>
+ case nir_instr_type_tex:<br>
+ instr = &read_tex(ctx)->instr;<br>
+ break;<br>
+ case nir_instr_type_phi:<br>
+ /* Phi instructions are a bit of a special case when readin because we<br>
+ * don't want inserting the instruction to automatically handle use/defs<br>
+ * for us. Instead, we need to wait until all the blocks/instructions<br>
+ * are read so that we can set their sources up.<br>
+ */<br>
+ read_phi(ctx, block);<br>
+ return;<br>
+ case nir_instr_type_jump:<br>
+ instr = &read_jump(ctx)->instr;<br>
+ break;<br>
+ case nir_instr_type_call:<br>
+ instr = &read_call(ctx)->instr;<br>
+ break;<br>
+ case nir_instr_type_parallel_copy:<br>
+ unreachable("Cannot read parallel copies");<br>
+ default:<br>
+ unreachable("bad instr type");<br>
+ }<br>
+<br>
+ nir_instr_insert_after_block(<wbr>block, instr);<br>
+}<br>
+<br>
+static void<br>
+write_block(write_ctx *ctx, const nir_block *block)<br>
+{<br>
+ write_add_object(ctx, block);<br>
+ blob_write_uint32(ctx->blob, exec_list_length(&block-><wbr>instr_list));<br>
+ nir_foreach_instr(instr, block)<br>
+ write_instr(ctx, instr);<br>
+}<br>
+<br>
+static void<br>
+read_block(read_ctx *ctx, struct exec_list *cf_list)<br>
+{<br>
+ /* Don't actually create a new block. Just use the one from the tail of<br>
+ * the list. NIR guarantees that the tail of the list is a block and that<br>
+ * no two blocks are side-by-side in the IR; It should be empty.<br>
+ */<br>
+ nir_block *block =<br>
+ exec_node_data(nir_block, exec_list_get_tail(cf_list), cf_node.node);<br>
+<br>
+ read_add_object(ctx, block);<br>
+ unsigned num_instrs = blob_read_uint32(ctx->blob);<br>
+ for (unsigned i = 0; i < num_instrs; i++) {<br>
+ read_instr(ctx, block);<br>
+ }<br>
+}<br>
+<br>
+static void<br>
+write_cf_list(write_ctx *ctx, const struct exec_list *cf_list);<br>
+<br>
+static void<br>
+read_cf_list(read_ctx *ctx, struct exec_list *cf_list);<br>
+<br>
+static void<br>
+write_if(write_ctx *ctx, nir_if *nif)<br>
+{<br>
+ write_src(ctx, &nif->condition);<br>
+<br>
+ write_cf_list(ctx, &nif->then_list);<br>
+ write_cf_list(ctx, &nif->else_list);<br>
+}<br>
+<br>
+static void<br>
+read_if(read_ctx *ctx, struct exec_list *cf_list)<br>
+{<br>
+ nir_if *nif = nir_if_create(ctx->nir);<br>
+<br>
+ read_src(ctx, &nif->condition, nif);<br>
+<br>
+ nir_cf_node_insert_end(cf_<wbr>list, &nif->cf_node);<br>
+<br>
+ read_cf_list(ctx, &nif->then_list);<br>
+ read_cf_list(ctx, &nif->else_list);<br>
+}<br>
+<br>
+static void<br>
+write_loop(write_ctx *ctx, nir_loop *loop)<br>
+{<br>
+ write_cf_list(ctx, &loop->body);<br>
+}<br>
+<br>
+static void<br>
+read_loop(read_ctx *ctx, struct exec_list *cf_list)<br>
+{<br>
+ nir_loop *loop = nir_loop_create(ctx->nir);<br>
+<br>
+ nir_cf_node_insert_end(cf_<wbr>list, &loop->cf_node);<br>
+<br>
+ read_cf_list(ctx, &loop->body);<br>
+}<br>
+<br>
+static void<br>
+write_cf_node(write_ctx *ctx, nir_cf_node *cf)<br>
+{<br>
+ blob_write_uint32(ctx->blob, cf->type);<br>
+<br>
+ switch (cf->type) {<br>
+ case nir_cf_node_block:<br>
+ write_block(ctx, nir_cf_node_as_block(cf));<br>
+ break;<br>
+ case nir_cf_node_if:<br>
+ write_if(ctx, nir_cf_node_as_if(cf));<br>
+ break;<br>
+ case nir_cf_node_loop:<br>
+ write_loop(ctx, nir_cf_node_as_loop(cf));<br>
+ break;<br>
+ default:<br>
+ unreachable("bad cf type");<br>
+ }<br>
+}<br>
+<br>
+static void<br>
+read_cf_node(read_ctx *ctx, struct exec_list *list)<br>
+{<br>
+ nir_cf_node_type type = blob_read_uint32(ctx->blob);<br>
+<br>
+ switch (type) {<br>
+ case nir_cf_node_block:<br>
+ read_block(ctx, list);<br>
+ break;<br>
+ case nir_cf_node_if:<br>
+ read_if(ctx, list);<br>
+ break;<br>
+ case nir_cf_node_loop:<br>
+ read_loop(ctx, list);<br>
+ break;<br>
+ default:<br>
+ unreachable("bad cf type");<br>
+ }<br>
+}<br>
+<br>
+static void<br>
+write_cf_list(write_ctx *ctx, const struct exec_list *cf_list)<br>
+{<br>
+ blob_write_uint32(ctx->blob, exec_list_length(cf_list));<br>
+ foreach_list_typed(nir_cf_<wbr>node, cf, node, cf_list) {<br>
+ write_cf_node(ctx, cf);<br>
+ }<br>
+}<br>
+<br>
+static void<br>
+read_cf_list(read_ctx *ctx, struct exec_list *cf_list)<br>
+{<br>
+ uint32_t num_cf_nodes = blob_read_uint32(ctx->blob);<br>
+ for (unsigned i = 0; i < num_cf_nodes; i++)<br>
+ read_cf_node(ctx, cf_list);<br>
+}<br>
+<br>
+static void<br>
+write_function_impl(write_ctx *ctx, const nir_function_impl *fi)<br>
+{<br>
+ write_var_list(ctx, &fi->locals);<br>
+ write_reg_list(ctx, &fi->registers);<br>
+ blob_write_uint32(ctx->blob, fi->reg_alloc);<br>
+<br>
+ blob_write_uint32(ctx->blob, fi->num_params);<br>
+ for (unsigned i = 0; i < fi->num_params; i++) {<br>
+ write_variable(ctx, fi->params[i]);<br>
+ }<br>
+<br>
+ blob_write_uint32(ctx->blob, !!(fi->return_var));<br>
+ if (fi->return_var)<br>
+ write_variable(ctx, fi->return_var);<br>
+<br>
+ write_cf_list(ctx, &fi->body);<br>
+ write_fixup_phis(ctx);<br>
+}<br>
+<br>
+static nir_function_impl *<br>
+read_function_impl(read_ctx *ctx, nir_function *fxn)<br>
+{<br>
+ nir_function_impl *fi = nir_function_impl_create_bare(<wbr>ctx->nir);<br>
+ fi->function = fxn;<br>
+<br>
+ read_var_list(ctx, &fi->locals);<br>
+ read_reg_list(ctx, &fi->registers);<br>
+ fi->reg_alloc = blob_read_uint32(ctx->blob);<br>
+<br>
+ fi->num_params = blob_read_uint32(ctx->blob);<br>
+ for (unsigned i = 0; i < fi->num_params; i++) {<br>
+ fi->params[i] = read_variable(ctx);<br>
+ }<br>
+<br>
+ bool has_return = blob_read_uint32(ctx->blob);<br>
+ if (has_return)<br>
+ fi->return_var = read_variable(ctx);<br>
+ else<br>
+ fi->return_var = NULL;<br>
+<br>
+ read_cf_list(ctx, &fi->body);<br>
+ read_fixup_phis(ctx);<br>
+<br>
+ fi->valid_metadata = 0;<br>
+<br>
+ return fi;<br>
+}<br>
+<br>
+static void<br>
+write_function(write_ctx *ctx, const nir_function *fxn)<br>
+{<br>
+ blob_write_uint32(ctx->blob, !!(fxn->name));<br>
+ if (fxn->name)<br>
+ blob_write_string(ctx->blob, fxn->name);<br>
+<br>
+ write_add_object(ctx, fxn);<br>
+<br>
+ blob_write_uint32(ctx->blob, fxn->num_params);<br>
+ for (unsigned i = 0; i < fxn->num_params; i++) {<br>
+ blob_write_uint32(ctx->blob, fxn->params[i].param_type);<br>
+ encode_type_to_blob(ctx->blob, fxn->params[i].type);<br>
+ }<br>
+<br>
+ encode_type_to_blob(ctx->blob, fxn->return_type);<br>
+<br>
+ /* At first glance, it looks like we should write the function_impl here.<br>
+ * However, call instructions need to be able to reference at least the<br>
+ * function and those will get processed as we write the function_impls.<br>
+ * We stop here and write function_impls as a second pass.<br>
+ */<br>
+}<br>
+<br>
+static void<br>
+read_function(read_ctx *ctx)<br>
+{<br>
+ bool has_name = blob_read_uint32(ctx->blob);<br>
+ char *name = has_name ? blob_read_string(ctx->blob) : NULL;<br>
+<br>
+ nir_function *fxn = nir_function_create(ctx->nir, name);<br>
+<br>
+ read_add_object(ctx, fxn);<br>
+<br>
+ fxn->num_params = blob_read_uint32(ctx->blob);<br>
+ for (unsigned i = 0; i < fxn->num_params; i++) {<br>
+ fxn->params[i].param_type = blob_read_uint32(ctx->blob);<br>
+ fxn->params[i].type = decode_type_from_blob(ctx-><wbr>blob);<br>
+ }<br>
+<br>
+ fxn->return_type = decode_type_from_blob(ctx-><wbr>blob);<br>
+}<br>
+<br>
+void<br>
+nir_serialize(struct blob *blob, const nir_shader *nir)<br>
+{<br>
+ write_ctx ctx;<br>
+ ctx.remap_table = _mesa_hash_table_create(NULL, _mesa_hash_pointer,<br>
+ _mesa_key_pointer_equal);<br>
+ ctx.next_idx = 0;<br>
+ ctx.blob = blob;<br>
+ ctx.nir = nir;<br>
+ util_dynarray_init(&ctx.phi_<wbr>fixups, NULL);<br>
+<br>
+ size_t idx_size_offset = blob_reserve_intptr(blob);<br>
+<br>
+ struct shader_info info = nir->info;<br>
+ uint32_t strings = 0;<br>
+ if (<a href="http://info.name" rel="noreferrer" target="_blank">info.name</a>)<br>
+ strings |= 0x1;<br>
+ if (info.label)<br>
+ strings |= 0x2;<br>
+ blob_write_uint32(blob, strings);<br>
+ if (<a href="http://info.name" rel="noreferrer" target="_blank">info.name</a>)<br>
+ blob_write_string(blob, <a href="http://info.name" rel="noreferrer" target="_blank">info.name</a>);<br>
+ if (info.label)<br>
+ blob_write_string(blob, info.label);<br>
+ <a href="http://info.name" rel="noreferrer" target="_blank">info.name</a> = info.label = NULL;<br>
+ blob_write_bytes(blob, (uint8_t *) &info, sizeof(info));<br>
+<br>
+ write_var_list(&ctx, &nir->uniforms);<br>
+ write_var_list(&ctx, &nir->inputs);<br>
+ write_var_list(&ctx, &nir->outputs);<br>
+ write_var_list(&ctx, &nir->shared);<br>
+ write_var_list(&ctx, &nir->globals);<br>
+ write_var_list(&ctx, &nir->system_values);<br>
+<br>
+ write_reg_list(&ctx, &nir->registers);<br>
+ blob_write_uint32(blob, nir->reg_alloc);<br>
+ blob_write_uint32(blob, nir->num_inputs);<br>
+ blob_write_uint32(blob, nir->num_uniforms);<br>
+ blob_write_uint32(blob, nir->num_outputs);<br>
+ blob_write_uint32(blob, nir->num_shared);<br>
+<br>
+ blob_write_uint32(blob, exec_list_length(&nir-><wbr>functions));<br>
+ nir_foreach_function(fxn, nir) {<br>
+ write_function(&ctx, fxn);<br>
+ }<br>
+<br>
+ nir_foreach_function(fxn, nir) {<br>
+ write_function_impl(&ctx, fxn->impl);<br>
+ }<br>
+<br>
+ *(uintptr_t *)(blob->data + idx_size_offset) = ctx.next_idx;<br>
+<br>
+ _mesa_hash_table_destroy(ctx.<wbr>remap_table, NULL);<br>
+ util_dynarray_fini(&ctx.phi_<wbr>fixups);<br>
+}<br>
+<br>
+nir_shader *<br>
+nir_deserialize(void *mem_ctx,<br>
+ const struct nir_shader_compiler_options *options,<br>
+ struct blob_reader *blob)<br>
+{<br>
+ read_ctx ctx;<br>
+ ctx.blob = blob;<br>
+ list_inithead(&ctx.phi_srcs);<br>
+ ctx.idx_table_len = blob_read_intptr(blob);<br>
+ ctx.idx_table = calloc(ctx.idx_table_len, sizeof(uintptr_t));<br>
+ ctx.next_idx = 0;<br>
+<br>
+ uint32_t strings = blob_read_uint32(blob);<br>
+ char *name = (strings & 0x1) ? blob_read_string(blob) : NULL;<br>
+ char *label = (strings & 0x2) ? blob_read_string(blob) : NULL;<br>
+<br>
+ struct shader_info info;<br>
+ blob_copy_bytes(blob, (uint8_t *) &info, sizeof(info));<br>
+<br>
+ ctx.nir = nir_shader_create(mem_ctx, info.stage, options, NULL);<br>
+<br>
+ <a href="http://info.name" rel="noreferrer" target="_blank">info.name</a> = name ? ralloc_strdup(ctx.nir, name) : NULL;<br>
+ info.label = label ? ralloc_strdup(ctx.nir, label) : NULL;<br>
+<br>
+ ctx.nir->info = info;<br>
+<br>
+ read_var_list(&ctx, &ctx.nir->uniforms);<br>
+ read_var_list(&ctx, &ctx.nir->inputs);<br>
+ read_var_list(&ctx, &ctx.nir->outputs);<br>
+ read_var_list(&ctx, &ctx.nir->shared);<br>
+ read_var_list(&ctx, &ctx.nir->globals);<br>
+ read_var_list(&ctx, &ctx.nir->system_values);<br>
+<br>
+ read_reg_list(&ctx, &ctx.nir->registers);<br>
+ ctx.nir->reg_alloc = blob_read_uint32(blob);<br>
+ ctx.nir->num_inputs = blob_read_uint32(blob);<br>
+ ctx.nir->num_uniforms = blob_read_uint32(blob);<br>
+ ctx.nir->num_outputs = blob_read_uint32(blob);<br>
+ ctx.nir->num_shared = blob_read_uint32(blob);<br>
+<br>
+ unsigned num_functions = blob_read_uint32(blob);<br>
+ for (unsigned i = 0; i < num_functions; i++)<br>
+ read_function(&ctx);<br>
+<br>
+ nir_foreach_function(fxn, ctx.nir)<br>
+ fxn->impl = read_function_impl(&ctx, fxn);<br>
+<br>
+ free(ctx.idx_table);<br>
+<br>
+ return ctx.nir;<br>
+}<br>
diff --git a/src/compiler/nir/nir_<wbr>serialize.h b/src/compiler/nir/nir_<wbr>serialize.h<br>
new file mode 100644<br>
index 0000000000..f77d8e367f<br>
--- /dev/null<br>
+++ b/src/compiler/nir/nir_<wbr>serialize.h<br>
@@ -0,0 +1,43 @@<br>
+/*<br>
+ * Copyright © 2017 Connor Abbott<br>
+ *<br>
+ * Permission is hereby granted, free of charge, to any person obtaining a<br>
+ * copy of this software and associated documentation files (the "Software"),<br>
+ * to deal in the Software without restriction, including without limitation<br>
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,<br>
+ * and/or sell copies of the Software, and to permit persons to whom the<br>
+ * Software is furnished to do so, subject to the following conditions:<br>
+ *<br>
+ * The above copyright notice and this permission notice (including the next<br>
+ * paragraph) shall be included in all copies or substantial portions of the<br>
+ * Software.<br>
+ *<br>
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR<br>
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,<br>
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL<br>
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER<br>
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING<br>
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS<br>
+ * IN THE SOFTWARE.<br>
+ */<br>
+<br>
+#ifndef _NIR_SERIALIZE_H<br>
+#define _NIR_SERIALIZE_H<br>
+<br>
+#include "nir.h"<br>
+#include "compiler/blob.h"<br>
+<br>
+#ifdef __cplusplus<br>
+extern "C" {<br>
+#endif<br>
+<br>
+void nir_serialize(struct blob *blob, const nir_shader *nir);<br>
+nir_shader *nir_deserialize(void *mem_ctx,<br>
+ const struct nir_shader_compiler_options *options,<br>
+ struct blob_reader *blob);<br>
+<br>
+#ifdef __cplusplus<br>
+} /* extern "C" */<br>
+#endif<br>
+<br>
+#endif<br>
<span class="HOEnZb"><font color="#888888">--<br>
2.15.0.rc0<br>
<br>
______________________________<wbr>_________________<br>
mesa-dev mailing list<br>
<a href="mailto:mesa-dev@lists.freedesktop.org">mesa-dev@lists.freedesktop.org</a><br>
<a href="https://lists.freedesktop.org/mailman/listinfo/mesa-dev" rel="noreferrer" target="_blank">https://lists.freedesktop.org/<wbr>mailman/listinfo/mesa-dev</a><br>
</font></span></blockquote></div><br></div></div></div></div></div></div>