nir/serialize: add specialized function serialization

with vtn_bindgen2 we only care about a single function at a time, not a whole
nir_shader, and it would be quite wasteful to serialize all the shader info
every time. add a specialized serialize just for 1 function.

Signed-off-by: Alyssa Rosenzweig <alyssa@rosenzweig.io>
Reviewed-by: Lionel Landwerlin <lionel.g.landwerlin@intel.com>
Reviewed-by: Mary Guillemard <mary.guillemard@collabora.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/33099>
This commit is contained in:
Alyssa Rosenzweig 2025-01-17 10:54:26 -05:00 committed by Marge Bot
parent 646903ed7a
commit d4ec0fc381
2 changed files with 52 additions and 1 deletions

View file

@ -2042,7 +2042,7 @@ write_function(write_ctx *ctx, const nir_function *fxn)
*/
}
static void
static nir_function *
read_function(read_ctx *ctx)
{
uint32_t flags = blob_read_uint32(ctx->blob);
@ -2092,6 +2092,7 @@ read_function(read_ctx *ctx)
fxn->dont_inline = flags & 0x20;
fxn->is_subroutine = flags & 0x40;
fxn->is_tmp_globals_wrapper = flags & 0x80;
return fxn;
}
static void
@ -2126,6 +2127,27 @@ enum nir_serialize_shader_flags {
NIR_SERIALIZE_DEBUG_INFO = 1 << 2,
};
void
nir_serialize_function(struct blob *blob, const nir_function *fxn)
{
write_ctx ctx = { 0 };
ctx.remap_table = _mesa_pointer_hash_table_create(NULL);
ctx.blob = blob;
ctx.nir = fxn->shader;
ctx.strip = true;
util_dynarray_init(&ctx.phi_fixups, NULL);
size_t idx_size_offset = blob_reserve_uint32(blob);
write_function(&ctx, fxn);
write_function_impl(&ctx, fxn->impl);
blob_overwrite_uint32(blob, idx_size_offset, ctx.next_idx);
_mesa_hash_table_destroy(ctx.remap_table, NULL);
util_dynarray_fini(&ctx.phi_fixups);
}
/**
* Serialize NIR into a binary blob.
*
@ -2264,6 +2286,27 @@ nir_deserialize(void *mem_ctx,
return ctx.nir;
}
nir_function *
nir_deserialize_function(void *mem_ctx,
const struct nir_shader_compiler_options *options,
struct blob_reader *blob)
{
read_ctx ctx = { 0 };
ctx.blob = blob;
list_inithead(&ctx.phi_srcs);
ctx.idx_table_len = blob_read_uint32(blob);
ctx.idx_table = calloc(ctx.idx_table_len, sizeof(uintptr_t));
ctx.nir = nir_shader_create(mem_ctx, 0 /* stage */, options, NULL);
nir_function *fxn = read_function(&ctx);
nir_function_set_impl(fxn, read_function_impl(&ctx));
free(ctx.idx_table);
nir_validate_shader(ctx.nir, "after deserialize");
return fxn;
}
void
nir_shader_serialize_deserialize(nir_shader *shader)
{

View file

@ -36,6 +36,14 @@ nir_shader *nir_deserialize(void *mem_ctx,
const struct nir_shader_compiler_options *options,
struct blob_reader *blob);
void
nir_serialize_function(struct blob *blob, const nir_function *fxn);
nir_function *
nir_deserialize_function(void *mem_ctx,
const struct nir_shader_compiler_options *options,
struct blob_reader *blob);
#ifdef __cplusplus
} /* extern "C" */
#endif