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

Author: Faith Ekstrand <faith.ekstr...@collabora.com>
Date:   Tue Nov 14 11:48:52 2023 -0600

nvk: Move the optimization loop to the nvk_codegen.c

We also call it from nak_preprocess_nir and lower var copies there.  NAK
should already be doing this for us.

Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/26197>

---

 src/nouveau/vulkan/nvk_codegen.c | 59 ++++++++++++++++++++++++++++++++++++
 src/nouveau/vulkan/nvk_shader.c  | 64 +---------------------------------------
 src/nouveau/vulkan/nvk_shader.h  |  1 +
 3 files changed, 61 insertions(+), 63 deletions(-)

diff --git a/src/nouveau/vulkan/nvk_codegen.c b/src/nouveau/vulkan/nvk_codegen.c
index d5a5c6f744a..6b416514152 100644
--- a/src/nouveau/vulkan/nvk_codegen.c
+++ b/src/nouveau/vulkan/nvk_codegen.c
@@ -134,4 +134,63 @@ nvk_cg_preprocess_nir(nir_shader *nir)
       NIR_PASS(_, nir, nir_shader_instructions_pass, lower_fragcoord_instr,
                nir_metadata_block_index | nir_metadata_dominance, NULL);
    }
+
+   nvk_cg_optimize_nir(nir);
+
+   NIR_PASS(_, nir, nir_lower_var_copies);
+}
+
+void
+nvk_cg_optimize_nir(nir_shader *nir)
+{
+   bool progress;
+
+   do {
+      progress = false;
+
+      NIR_PASS(progress, nir, nir_split_array_vars, nir_var_function_temp);
+      NIR_PASS(progress, nir, nir_shrink_vec_array_vars, 
nir_var_function_temp);
+
+      if (!nir->info.var_copies_lowered) {
+         /* Only run this pass if nir_lower_var_copies was not called
+          * yet. That would lower away any copy_deref instructions and we
+          * don't want to introduce any more.
+          */
+         NIR_PASS(progress, nir, nir_opt_find_array_copies);
+      }
+      NIR_PASS(progress, nir, nir_opt_copy_prop_vars);
+      NIR_PASS(progress, nir, nir_opt_dead_write_vars);
+      NIR_PASS(progress, nir, nir_lower_vars_to_ssa);
+      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_remove_phis);
+         NIR_PASS(progress, nir, nir_opt_dce);
+      }
+      NIR_PASS(progress, nir, nir_opt_if,
+               nir_opt_if_aggressive_last_continue | 
nir_opt_if_optimize_phi_true_false);
+      NIR_PASS(progress, nir, nir_opt_dead_cf);
+      NIR_PASS(progress, nir, nir_opt_cse);
+      /*
+       * this should be fine, likely a backend problem,
+       * but a bunch of tessellation shaders blow up.
+       * we should revisit this when NAK is merged.
+       */
+      NIR_PASS(progress, nir, nir_opt_peephole_select, 2, true, true);
+      NIR_PASS(progress, nir, nir_opt_constant_folding);
+      NIR_PASS(progress, nir, nir_opt_algebraic);
+
+      NIR_PASS(progress, nir, nir_opt_undef);
+
+      if (nir->options->max_unroll_iterations) {
+         NIR_PASS(progress, nir, nir_opt_loop_unroll);
+      }
+   } while (progress);
+
+   NIR_PASS(progress, nir, nir_opt_shrink_vectors);
+   NIR_PASS(progress, nir, nir_remove_dead_variables,
+            nir_var_function_temp | nir_var_shader_in | nir_var_shader_out, 
NULL);
 }
diff --git a/src/nouveau/vulkan/nvk_shader.c b/src/nouveau/vulkan/nvk_shader.c
index 3aef8506c80..99551587e4e 100644
--- a/src/nouveau/vulkan/nvk_shader.c
+++ b/src/nouveau/vulkan/nvk_shader.c
@@ -312,61 +312,6 @@ lookup_ycbcr_conversion(const void *_layout, uint32_t set,
           &sampler->vk.ycbcr_conversion->state : NULL;
 }
 
-static void
-nvk_optimize_nir(nir_shader *nir)
-{
-   bool progress;
-
-   do {
-      progress = false;
-
-      NIR_PASS(progress, nir, nir_split_array_vars, nir_var_function_temp);
-      NIR_PASS(progress, nir, nir_shrink_vec_array_vars, 
nir_var_function_temp);
-
-      if (!nir->info.var_copies_lowered) {
-         /* Only run this pass if nir_lower_var_copies was not called
-          * yet. That would lower away any copy_deref instructions and we
-          * don't want to introduce any more.
-          */
-         NIR_PASS(progress, nir, nir_opt_find_array_copies);
-      }
-      NIR_PASS(progress, nir, nir_opt_copy_prop_vars);
-      NIR_PASS(progress, nir, nir_opt_dead_write_vars);
-      NIR_PASS(progress, nir, nir_lower_vars_to_ssa);
-      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_remove_phis);
-         NIR_PASS(progress, nir, nir_opt_dce);
-      }
-      NIR_PASS(progress, nir, nir_opt_if,
-               nir_opt_if_aggressive_last_continue | 
nir_opt_if_optimize_phi_true_false);
-      NIR_PASS(progress, nir, nir_opt_dead_cf);
-      NIR_PASS(progress, nir, nir_opt_cse);
-      /*
-       * this should be fine, likely a backend problem,
-       * but a bunch of tessellation shaders blow up.
-       * we should revisit this when NAK is merged.
-       */
-      NIR_PASS(progress, nir, nir_opt_peephole_select, 2, true, true);
-      NIR_PASS(progress, nir, nir_opt_constant_folding);
-      NIR_PASS(progress, nir, nir_opt_algebraic);
-
-      NIR_PASS(progress, nir, nir_opt_undef);
-
-      if (nir->options->max_unroll_iterations) {
-         NIR_PASS(progress, nir, nir_opt_loop_unroll);
-      }
-   } while (progress);
-
-   NIR_PASS(progress, nir, nir_opt_shrink_vectors);
-   NIR_PASS(progress, nir, nir_remove_dead_variables,
-            nir_var_function_temp | nir_var_shader_in | nir_var_shader_out, 
NULL);
-}
-
 VkResult
 nvk_shader_stage_to_nir(struct nvk_device *dev,
                         const VkPipelineShaderStageCreateInfo *sinfo,
@@ -453,13 +398,6 @@ nvk_lower_nir(struct nvk_device *dev, nir_shader *nir,
    };
    NIR_PASS(_, nir, nir_lower_compute_system_values, &csv_options);
 
-   /* Vulkan uses the separate-shader linking model */
-   nir->info.separate_shader = true;
-
-   nvk_optimize_nir(nir);
-
-   NIR_PASS(_, nir, nir_lower_var_copies);
-
    /* Lower push constants before lower_descriptors */
    NIR_PASS(_, nir, nir_lower_explicit_io, nir_var_mem_push_const,
             nir_address_format_32bit_offset);
@@ -518,7 +456,7 @@ nvk_lower_nir(struct nvk_device *dev, nir_shader *nir,
 
    NIR_PASS(_, nir, nir_lower_indirect_derefs, indirect_mask, 16);
 
-   nvk_optimize_nir(nir);
+   nvk_cg_optimize_nir(nir);
    if (nir->info.stage != MESA_SHADER_COMPUTE)
       assign_io_locations(nir);
 
diff --git a/src/nouveau/vulkan/nvk_shader.h b/src/nouveau/vulkan/nvk_shader.h
index c2635fe3913..29bf3d2ae17 100644
--- a/src/nouveau/vulkan/nvk_shader.h
+++ b/src/nouveau/vulkan/nvk_shader.h
@@ -166,5 +166,6 @@ nvk_cg_nir_options(const struct nvk_physical_device *pdev,
                    gl_shader_stage stage);
 
 void nvk_cg_preprocess_nir(nir_shader *nir);
+void nvk_cg_optimize_nir(nir_shader *nir);
 
 #endif

Reply via email to