asahi: Don't leak shader NIR
authorAlyssa Rosenzweig <alyssa@rosenzweig.io>
Fri, 3 Feb 2023 20:32:19 +0000 (15:32 -0500)
committerMarge Bot <emma+marge@anholt.net>
Sat, 4 Feb 2023 17:10:15 +0000 (17:10 +0000)
create_shader_state passes ownership of the NIR to the driver, so we need to
free it when we destroy the shader CSO later. Use ralloc to manage this in a
uniform way between graphics and compute. Strategy from Panfrost.

Signed-off-by: Alyssa Rosenzweig <alyssa@rosenzweig.io>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/21062>

src/gallium/drivers/asahi/agx_state.c

index b5d6c6c..cca83e7 100644 (file)
@@ -1400,7 +1400,8 @@ static void *
 agx_create_shader_state(struct pipe_context *pctx,
                         const struct pipe_shader_state *cso)
 {
-   struct agx_uncompiled_shader *so = CALLOC_STRUCT(agx_uncompiled_shader);
+   struct agx_uncompiled_shader *so =
+      rzalloc(NULL, struct agx_uncompiled_shader);
    struct agx_device *dev = agx_device(pctx->screen);
 
    if (!so)
@@ -1412,6 +1413,11 @@ agx_create_shader_state(struct pipe_context *pctx,
                         ? cso->ir.nir
                         : tgsi_to_nir(cso->tokens, pctx->screen, false);
 
+   /* The driver gets ownership of the nir_shader for graphics. The NIR is
+    * ralloc'd. Free the NIR when we free the uncompiled shader.
+    */
+   ralloc_steal(so, nir);
+
    if (nir->info.stage == MESA_SHADER_VERTEX) {
       so->variants = _mesa_hash_table_create(NULL, asahi_vs_shader_key_hash,
                                              asahi_vs_shader_key_equal);
@@ -1467,7 +1473,8 @@ static void *
 agx_create_compute_state(struct pipe_context *pctx,
                          const struct pipe_compute_state *cso)
 {
-   struct agx_uncompiled_shader *so = CALLOC_STRUCT(agx_uncompiled_shader);
+   struct agx_uncompiled_shader *so =
+      rzalloc(NULL, struct agx_uncompiled_shader);
 
    if (!so)
       return NULL;
@@ -1609,7 +1616,7 @@ agx_delete_shader_state(struct pipe_context *ctx, void *cso)
 {
    struct agx_uncompiled_shader *so = cso;
    _mesa_hash_table_destroy(so->variants, agx_delete_compiled_shader);
-   free(so);
+   ralloc_free(so);
 }
 
 static uint32_t