glsl: when NIR linker enable use it to resize uniform arrays
authorTimothy Arceri <tarceri@itsqueeze.com>
Tue, 5 May 2020 04:24:46 +0000 (14:24 +1000)
committerMarge Bot <eric+marge@anholt.net>
Wed, 3 Jun 2020 10:34:22 +0000 (10:34 +0000)
Here we turn on uniform array resizing in the NIR linker and disable
the GLSL IR resizing pass when the NIR linker is enabled.

This will potentially make uniform arrays smaller due to NIR
optimising away more uniform uses.

Shader-db results (SKL):

total instructions in shared programs: 14947192 -> 14944093 (-0.02%)
instructions in affected programs: 138088 -> 134989 (-2.24%)
helped: 822
HURT: 4

total cycles in shared programs: 324868402 -> 324794597 (-0.02%)
cycles in affected programs: 3904170 -> 3830365 (-1.89%)
helped: 2333
HURT: 1485

Reviewed-by: Kenneth Graunke <kenneth@whitecape.org>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/4910>

src/compiler/glsl/gl_nir_link_uniforms.c
src/compiler/glsl/linker.cpp

index 9806da393abd7bb8f6ee16e1d037ded06c694fde..b1187c45dc14cebdf89ac92a436ef58bebe91cef 100644 (file)
@@ -1522,6 +1522,17 @@ gl_nir_link_uniforms(struct gl_context *ctx,
       add_var_use_shader(nir, state.referenced_uniforms[stage]);
    }
 
+   /* Resize uniform arrays based on the maximum array index */
+   for (unsigned stage = 0; stage < MESA_SHADER_STAGES; stage++) {
+      struct gl_linked_shader *sh = prog->_LinkedShaders[stage];
+      if (!sh)
+         continue;
+
+      nir_shader *nir = sh->Program->nir;
+      nir_foreach_variable(var, &nir->uniforms)
+         update_array_sizes(prog, var, state.referenced_uniforms);
+   }
+
    /* Count total number of uniforms and allocate storage */
    unsigned storage_size = 0;
    if (!prog->data->spirv) {
index a674726312f73af0277ff76714e27c795e854fe9..8761c16e2fefca58acdf7871810b1a281c844ee8 100644 (file)
@@ -4444,21 +4444,20 @@ static void
 link_and_validate_uniforms(struct gl_context *ctx,
                            struct gl_shader_program *prog)
 {
-   update_array_sizes(prog);
+   assert(!ctx->Const.UseNIRGLSLLinker);
 
-   if (!ctx->Const.UseNIRGLSLLinker) {
-      link_assign_uniform_locations(prog, ctx);
+   update_array_sizes(prog);
+   link_assign_uniform_locations(prog, ctx);
 
-      if (prog->data->LinkStatus == LINKING_FAILURE)
-         return;
+   if (prog->data->LinkStatus == LINKING_FAILURE)
+      return;
 
-      link_util_calculate_subroutine_compat(prog);
-      link_util_check_uniform_resources(ctx, prog);
-      link_util_check_subroutine_resources(prog);
-      check_image_resources(ctx, prog);
-      link_assign_atomic_counter_resources(ctx, prog);
-      link_check_atomic_counter_resources(ctx, prog);
-   }
+   link_util_calculate_subroutine_compat(prog);
+   link_util_check_uniform_resources(ctx, prog);
+   link_util_check_subroutine_resources(prog);
+   check_image_resources(ctx, prog);
+   link_assign_atomic_counter_resources(ctx, prog);
+   link_check_atomic_counter_resources(ctx, prog);
 }
 
 static bool
@@ -4505,7 +4504,8 @@ link_varyings_and_uniforms(unsigned first, unsigned last,
    if (!link_varyings(prog, first, last, ctx, mem_ctx))
       return false;
 
-   link_and_validate_uniforms(ctx, prog);
+   if (!ctx->Const.UseNIRGLSLLinker)
+      link_and_validate_uniforms(ctx, prog);
 
    if (!prog->data->LinkStatus)
       return false;