Mesa (main): v3dv/pipeline: expand nir_optimize, drop st_nir_opts

GitLab Mirror gitlab-mirror at kemper.freedesktop.org
Tue Jun 14 13:37:35 UTC 2022


Module: Mesa
Branch: main
Commit: 51bdac48465186d9abcae20a57674adc2ba95cd7
URL:    http://cgit.freedesktop.org/mesa/mesa/commit/?id=51bdac48465186d9abcae20a57674adc2ba95cd7

Author: Alejandro Piñeiro <apinheiro at igalia.com>
Date:   Wed Mar  9 23:37:22 2022 +0100

v3dv/pipeline: expand nir_optimize, drop st_nir_opts

Right now we had two methods that tries to optimize the nir shader,
nir_optimize and st_nir_opts. The latter is being used when we are
linking, but again, it has basically the same purpose that
nir_optimize.

So this commit adds more lowerings to nir_optimize_nir, add some extra
comments on the method, and replaces st_nir_opts with nir_optimize.

Ideally we would like to just use the already existing
v3d_optimize_nir that we have at the backend But:
   * Using it leads to some regressions on Vulkan CTS tests, due some
     lowerings that are already there.
   * We would need to move to the backend some additional
     lowerings/optimizations that are used on the Vulkan
     frontend. That would require to check that we are not getting any
     regression or performance drop on OpenGL

So for now we are keeping a Vulkan specific nir_optimize method.

Additionally this fixes the following test:
dEQP-VK.graphicsfuzz.cov-loop-condition-clamp-vec-of-ones

Shaderdb stats, using some well known Vulkan apps (ue4 demos, Quake3e,
etc):

 total instructions in shared programs: 124974 -> 125108 (0.11%)
 instructions in affected programs: 50328 -> 50462 (0.27%)
 helped: 4
 HURT: 79

 total uniforms in shared programs: 19019 -> 19020 (<.01%)
 uniforms in affected programs: 60 -> 61 (1.67%)
 helped: 0
 HURT: 1

 total max-temps in shared programs: 13438 -> 13444 (0.04%)
 max-temps in affected programs: 85 -> 91 (7.06%)
 helped: 0
 HURT: 2

 total inst-and-stalls in shared programs: 125715 -> 125849 (0.11%)
 inst-and-stalls in affected programs: 50429 -> 50563 (0.27%)
 helped: 4
 HURT: 79

 total nops in shared programs: 8203 -> 8204 (0.01%)
 nops in affected programs: 732 -> 733 (0.14%)
 helped: 7
 HURT: 9

Reviewed-by: Iago Toral Quiroga <itoral at igalia.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/16986>

---

 src/broadcom/vulkan/v3dv_pipeline.c | 109 +++++++++++++++---------------------
 1 file changed, 46 insertions(+), 63 deletions(-)

diff --git a/src/broadcom/vulkan/v3dv_pipeline.c b/src/broadcom/vulkan/v3dv_pipeline.c
index a5d58e07899..6ab88a6d511 100644
--- a/src/broadcom/vulkan/v3dv_pipeline.c
+++ b/src/broadcom/vulkan/v3dv_pipeline.c
@@ -298,6 +298,13 @@ nir_optimize(nir_shader *nir, bool allow_copies)
           */
          OPT(nir_opt_find_array_copies);
       }
+
+      OPT(nir_remove_dead_variables,
+          (nir_variable_mode)(nir_var_function_temp |
+                              nir_var_shader_temp |
+                              nir_var_mem_shared),
+          NULL);
+
       OPT(nir_opt_copy_prop_vars);
       OPT(nir_opt_dead_write_vars);
       OPT(nir_opt_combine_stores, nir_var_all);
@@ -326,19 +333,48 @@ nir_optimize(nir_shader *nir, bool allow_copies)
       OPT(nir_opt_intrinsics);
       OPT(nir_opt_idiv_const, 32);
       OPT(nir_opt_algebraic);
+      OPT(nir_lower_alu);
       OPT(nir_opt_constant_folding);
 
       OPT(nir_opt_dead_cf);
-
-      OPT(nir_opt_if, false);
+      if (nir_opt_trivial_continues(nir)) {
+         progress = true;
+         OPT(nir_copy_prop);
+         OPT(nir_opt_dce);
+      }
       OPT(nir_opt_conditional_discard);
 
       OPT(nir_opt_remove_phis);
+      OPT(nir_opt_gcm, false);
+      OPT(nir_opt_if, false);
       OPT(nir_opt_undef);
       OPT(nir_lower_pack);
-   } while (progress);
 
-   OPT(nir_remove_dead_variables, nir_var_function_temp, NULL);
+      /* There are two optimizations that we don't do here, and we rely on the
+       * backend:
+       *
+       * nir_lower_flrp only needs to be called once, as nothing should
+       * rematerialize any flrps. As we are already calling it on the backend
+       * compiler, we don't call it again.
+       *
+       * nir_opt_loop_unroll: as the backend includes custom strategies in
+       * order to get the lowest spill/fills possible, and some of them
+       * include disable loop unrolling.
+       *
+       * FIXME: ideally we would like to just remove this method and
+       * v3d_optimize_nir. But:
+       *
+       *   * Using it leads to some regressions on Vulkan CTS tests, due to
+       *     some lowering use there
+       *   * We would need to move to the backend some additional
+       *     lowerings/optimizations that are used on the Vulkan
+       *     frontend. That would require to check that we are not getting any
+       *     regression or performance drop on OpenGL
+       *
+       * For now we would keep this Vulkan fronted nir_optimize
+       */
+
+   } while (progress);
 }
 
 static void
@@ -1698,59 +1734,6 @@ pipeline_compile_shader_variant(struct v3dv_pipeline_stage *p_stage,
    return variant;
 }
 
-/* FIXME: C&P from st, common place? */
-static void
-st_nir_opts(nir_shader *nir)
-{
-   bool progress;
-
-   do {
-      progress = false;
-
-      NIR_PASS_V(nir, nir_lower_vars_to_ssa);
-
-      /* Linking deals with unused inputs/outputs, but here we can remove
-       * things local to the shader in the hopes that we can cleanup other
-       * things. This pass will also remove variables with only stores, so we
-       * might be able to make progress after it.
-       */
-      NIR_PASS(progress, nir, nir_remove_dead_variables,
-               (nir_variable_mode)(nir_var_function_temp |
-                                   nir_var_shader_temp |
-                                   nir_var_mem_shared),
-               NULL);
-
-      NIR_PASS(progress, nir, nir_opt_copy_prop_vars);
-      NIR_PASS(progress, nir, nir_opt_dead_write_vars);
-
-      if (nir->options->lower_to_scalar) {
-         NIR_PASS_V(nir, nir_lower_alu_to_scalar, NULL, NULL);
-         NIR_PASS_V(nir, nir_lower_phis_to_scalar, false);
-      }
-
-      NIR_PASS_V(nir, nir_lower_alu);
-      NIR_PASS_V(nir, nir_lower_pack);
-      NIR_PASS(progress, nir, nir_copy_prop);
-      NIR_PASS(progress, nir, nir_opt_remove_phis);
-      NIR_PASS(progress, nir, nir_opt_dce);
-      if (nir_opt_trivial_continues(nir)) {
-         progress = true;
-         NIR_PASS(progress, nir, nir_copy_prop);
-         NIR_PASS(progress, nir, nir_opt_dce);
-      }
-      NIR_PASS(progress, nir, nir_opt_if, false);
-      NIR_PASS(progress, nir, nir_opt_dead_cf);
-      NIR_PASS(progress, nir, nir_opt_cse);
-      NIR_PASS(progress, nir, nir_opt_peephole_select, 8, true, true);
-
-      NIR_PASS(progress, nir, nir_opt_algebraic);
-      NIR_PASS(progress, nir, nir_opt_constant_folding);
-
-      NIR_PASS(progress, nir, nir_opt_undef);
-      NIR_PASS(progress, nir, nir_opt_conditional_discard);
-   } while (progress);
-}
-
 static void
 link_shaders(nir_shader *producer, nir_shader *consumer)
 {
@@ -1764,11 +1747,11 @@ link_shaders(nir_shader *producer, nir_shader *consumer)
 
    nir_lower_io_arrays_to_elements(producer, consumer);
 
-   st_nir_opts(producer);
-   st_nir_opts(consumer);
+   nir_optimize(producer, false);
+   nir_optimize(consumer, false);
 
    if (nir_link_opt_varyings(producer, consumer))
-      st_nir_opts(consumer);
+      nir_optimize(consumer, false);
 
    NIR_PASS_V(producer, nir_remove_dead_variables, nir_var_shader_out, NULL);
    NIR_PASS_V(consumer, nir_remove_dead_variables, nir_var_shader_in, NULL);
@@ -1777,8 +1760,8 @@ link_shaders(nir_shader *producer, nir_shader *consumer)
       NIR_PASS_V(producer, nir_lower_global_vars_to_local);
       NIR_PASS_V(consumer, nir_lower_global_vars_to_local);
 
-      st_nir_opts(producer);
-      st_nir_opts(consumer);
+      nir_optimize(producer, false);
+      nir_optimize(consumer, false);
 
       /* Optimizations can cause varyings to become unused.
        * nir_compact_varyings() depends on all dead varyings being removed so
@@ -3280,7 +3263,7 @@ pipeline_compile_compute(struct v3dv_pipeline *pipeline,
    p_stage->nir = pipeline_stage_get_nir(p_stage, pipeline, cache);
    assert(p_stage->nir);
 
-   st_nir_opts(p_stage->nir);
+   nir_optimize(p_stage->nir, false);
    pipeline_lower_nir(pipeline, p_stage, pipeline->layout);
    lower_cs_shared(p_stage->nir);
 



More information about the mesa-commit mailing list