Mesa (main): mesa/st: Use nir_shader_instructions_pass for st_nir_lower_builtin

GitLab Mirror gitlab-mirror at kemper.freedesktop.org
Fri May 13 23:17:31 UTC 2022


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

Author: Jason Ekstrand <jason.ekstrand at collabora.com>
Date:   Thu May 12 14:04:14 2022 -0500

mesa/st: Use nir_shader_instructions_pass for st_nir_lower_builtin

Reviewed-by: Lionel Landwerlin <lionel.g.landwerlin at intel.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/16482>

---

 src/mesa/state_tracker/st_nir_lower_builtin.c | 148 ++++++++++----------------
 1 file changed, 57 insertions(+), 91 deletions(-)

diff --git a/src/mesa/state_tracker/st_nir_lower_builtin.c b/src/mesa/state_tracker/st_nir_lower_builtin.c
index fbf672c7b00..bf978766de9 100644
--- a/src/mesa/state_tracker/st_nir_lower_builtin.c
+++ b/src/mesa/state_tracker/st_nir_lower_builtin.c
@@ -64,12 +64,6 @@
 #include "uniforms.h"
 #include "program/prog_instruction.h"
 
-typedef struct {
-   nir_shader *shader;
-   nir_builder builder;
-   void *mem_ctx;
-} lower_builtin_state;
-
 static const struct gl_builtin_uniform_element *
 get_element(const struct gl_builtin_uniform_desc *desc, nir_deref_path *path)
 {
@@ -97,10 +91,10 @@ get_element(const struct gl_builtin_uniform_desc *desc, nir_deref_path *path)
 }
 
 static nir_variable *
-get_variable(lower_builtin_state *state, nir_deref_path *path,
+get_variable(nir_builder *b, nir_deref_path *path,
              const struct gl_builtin_uniform_element *element)
 {
-   nir_shader *shader = state->shader;
+   nir_shader *shader = b->shader;
    gl_state_index16 tokens[STATE_LENGTH];
    int idx = 1;
 
@@ -163,111 +157,83 @@ get_variable(lower_builtin_state *state, nir_deref_path *path,
 }
 
 static bool
-lower_builtin_block(lower_builtin_state *state, nir_block *block)
+lower_builtin_instr(nir_builder *b, nir_instr *instr, UNUSED void *_data)
 {
-   nir_builder *b = &state->builder;
-   bool progress = false;
-
-   nir_foreach_instr_safe(instr, block) {
-      if (instr->type != nir_instr_type_intrinsic)
-         continue;
-
-      nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
-
-      if (intrin->intrinsic != nir_intrinsic_load_deref)
-         continue;
+   if (instr->type != nir_instr_type_intrinsic)
+      return false;
 
-      nir_variable *var =
-         nir_deref_instr_get_variable(nir_src_as_deref(intrin->src[0]));
-      if (var->data.mode != nir_var_uniform)
-         continue;
+   nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
+   if (intrin->intrinsic != nir_intrinsic_load_deref)
+      return false;
 
-      /* built-in's will always start with "gl_" */
-      if (strncmp(var->name, "gl_", 3) != 0)
-         continue;
-
-      const struct gl_builtin_uniform_desc *desc =
-         _mesa_glsl_get_builtin_uniform_desc(var->name);
+   nir_variable *var =
+      nir_deref_instr_get_variable(nir_src_as_deref(intrin->src[0]));
+   if (var->data.mode != nir_var_uniform)
+      return false;
 
-      /* if no descriptor, it isn't something we need to handle specially: */
-      if (!desc)
-         continue;
+   /* built-in's will always start with "gl_" */
+   if (strncmp(var->name, "gl_", 3) != 0)
+      return false;
 
-      nir_deref_path path;
-      nir_deref_path_init(&path, nir_src_as_deref(intrin->src[0]), NULL);
+   const struct gl_builtin_uniform_desc *desc =
+      _mesa_glsl_get_builtin_uniform_desc(var->name);
 
-      const struct gl_builtin_uniform_element *element = get_element(desc, &path);
+   /* if no descriptor, it isn't something we need to handle specially: */
+   if (!desc)
+      return false;
 
-      /* matrix elements (array_deref) do not need special handling: */
-      if (!element) {
-         nir_deref_path_finish(&path);
-         continue;
-      }
+   nir_deref_path path;
+   nir_deref_path_init(&path, nir_src_as_deref(intrin->src[0]), NULL);
 
-      /* remove existing var from uniform list: */
-      exec_node_remove(&var->node);
-      /* the _self_link() ensures we can remove multiple times, rather than
-       * trying to keep track of what we have already removed:
-       */
-      exec_node_self_link(&var->node);
+   const struct gl_builtin_uniform_element *element = get_element(desc, &path);
 
-      nir_variable *new_var = get_variable(state, &path, element);
+   /* matrix elements (array_deref) do not need special handling: */
+   if (!element) {
       nir_deref_path_finish(&path);
+      return false;
+   }
 
-      b->cursor = nir_before_instr(instr);
-
-      nir_ssa_def *def = nir_load_var(b, new_var);
+   /* remove existing var from uniform list: */
+   exec_node_remove(&var->node);
+   /* the _self_link() ensures we can remove multiple times, rather than
+    * trying to keep track of what we have already removed:
+    */
+   exec_node_self_link(&var->node);
 
-      /* swizzle the result: */
-      unsigned swiz[NIR_MAX_VEC_COMPONENTS] = {0};
-      for (unsigned i = 0; i < 4; i++) {
-         swiz[i] = GET_SWZ(element->swizzle, i);
-         assert(swiz[i] <= SWIZZLE_W);
-      }
-      def = nir_swizzle(b, def, swiz, intrin->num_components);
+   nir_variable *new_var = get_variable(b, &path, element);
+   nir_deref_path_finish(&path);
 
-      /* and rewrite uses of original instruction: */
-      assert(intrin->dest.is_ssa);
-      nir_ssa_def_rewrite_uses(&intrin->dest.ssa, def);
+   b->cursor = nir_before_instr(instr);
 
-      /* at this point intrin should be unused.  We need to remove it
-       * (rather than waiting for DCE pass) to avoid dangling reference
-       * to remove'd var.  And we have to remove the original uniform
-       * var since we don't want it to get uniform space allocated.
-       */
-      nir_instr_remove(&intrin->instr);
+   nir_ssa_def *def = nir_load_var(b, new_var);
 
-      progress = true;
+   /* swizzle the result: */
+   unsigned swiz[NIR_MAX_VEC_COMPONENTS] = {0};
+   for (unsigned i = 0; i < 4; i++) {
+      swiz[i] = GET_SWZ(element->swizzle, i);
+      assert(swiz[i] <= SWIZZLE_W);
    }
+   def = nir_swizzle(b, def, swiz, intrin->num_components);
 
-   return progress;
-}
-
-static void
-lower_builtin_impl(lower_builtin_state *state, nir_function_impl *impl)
-{
-   nir_builder_init(&state->builder, impl);
-   state->mem_ctx = ralloc_parent(impl);
-
-   bool progress = false;
-   nir_foreach_block(block, impl) {
-      progress |= lower_builtin_block(state, block);
-   }
+   /* and rewrite uses of original instruction: */
+   assert(intrin->dest.is_ssa);
+   nir_ssa_def_rewrite_uses(&intrin->dest.ssa, def);
 
-   if (progress)
-      nir_remove_dead_derefs_impl(impl);
+   /* at this point intrin should be unused.  We need to remove it
+    * (rather than waiting for DCE pass) to avoid dangling reference
+    * to remove'd var.  And we have to remove the original uniform
+    * var since we don't want it to get uniform space allocated.
+    */
+   nir_instr_remove(&intrin->instr);
 
-   nir_metadata_preserve(impl, nir_metadata_block_index |
-                               nir_metadata_dominance);
+   return true;
 }
 
 void
 st_nir_lower_builtin(nir_shader *shader)
 {
-   lower_builtin_state state;
-   state.shader = shader;
-   nir_foreach_function(function, shader) {
-      if (function->impl)
-         lower_builtin_impl(&state, function->impl);
-   }
+   if (nir_shader_instructions_pass(shader, lower_builtin_instr,
+                                    nir_metadata_block_index |
+                                    nir_metadata_dominance, NULL))
+      nir_remove_dead_derefs(shader);
 }



More information about the mesa-commit mailing list