[Mesa-dev] [PATCH 3/3] nir/lower_system_values: Refactor and use the builder.

Jason Ekstrand jason at jlekstrand.net
Mon Dec 14 19:51:51 PST 2015


Now that we have a helper in the builder for system values and a helper in
core NIR to get the intrinsic opcode, there's really no point in having
things split out into a helper function.  This commit "modernizes" this
pass to use helpers better and look more like newer passes.
---
 src/glsl/nir/nir_lower_system_values.c | 60 +++++++++++++++++-----------------
 1 file changed, 30 insertions(+), 30 deletions(-)

diff --git a/src/glsl/nir/nir_lower_system_values.c b/src/glsl/nir/nir_lower_system_values.c
index 03a9814..0f91478 100644
--- a/src/glsl/nir/nir_lower_system_values.c
+++ b/src/glsl/nir/nir_lower_system_values.c
@@ -26,44 +26,41 @@
  */
 
 #include "nir.h"
-#include "main/mtypes.h"
+#include "nir_builder.h"
+
+struct lower_system_values_state {
+   nir_builder b;
+   bool progress;
+};
 
 static bool
-convert_instr(nir_intrinsic_instr *instr)
+convert_block(nir_block *block, void *void_state)
 {
-   if (instr->intrinsic != nir_intrinsic_load_var)
-      return false;
+   struct lower_system_values_state *state = void_state;
 
-   nir_variable *var = instr->variables[0]->var;
-   if (var->data.mode != nir_var_system_value)
-      return false;
+   nir_foreach_instr_safe(block, instr) {
+      if (instr->type != nir_instr_type_intrinsic)
+         continue;
 
-   void *mem_ctx = ralloc_parent(instr);
+      nir_intrinsic_instr *load_var = nir_instr_as_intrinsic(instr);
 
-   assert(instr->dest.is_ssa);
+      if (load_var->intrinsic != nir_intrinsic_load_var)
+         continue;
 
-   nir_intrinsic_op op = nir_intrinsic_from_system_value(var->data.location);
-   nir_intrinsic_instr *new_instr = nir_intrinsic_instr_create(mem_ctx, op);
+      nir_variable *var = load_var->variables[0]->var;
+      if (var->data.mode != nir_var_system_value)
+         continue;
 
-   nir_ssa_dest_init(&new_instr->instr, &new_instr->dest,
-                     instr->dest.ssa.num_components, NULL);
-   nir_ssa_def_rewrite_uses(&instr->dest.ssa,
-                            nir_src_for_ssa(&new_instr->dest.ssa));
+      state->b.cursor = nir_after_instr(&load_var->instr);
 
-   nir_instr_insert_before(&instr->instr, &new_instr->instr);
-   nir_instr_remove(&instr->instr);
+      nir_intrinsic_op sysval_op =
+         nir_intrinsic_from_system_value(var->data.location);
+      nir_ssa_def *sysval = nir_load_system_value(&state->b, sysval_op, 0);
 
-   return true;
-}
+      nir_ssa_def_rewrite_uses(&load_var->dest.ssa, nir_src_for_ssa(sysval));
+      nir_instr_remove(&load_var->instr);
 
-static bool
-convert_block(nir_block *block, void *state)
-{
-   bool *progress = state;
-
-   nir_foreach_instr_safe(block, instr) {
-      if (instr->type == nir_instr_type_intrinsic)
-         *progress = convert_instr(nir_instr_as_intrinsic(instr)) || *progress;
+      state->progress = true;
    }
 
    return true;
@@ -72,12 +69,15 @@ convert_block(nir_block *block, void *state)
 static bool
 convert_impl(nir_function_impl *impl)
 {
-   bool progress = false;
+   struct lower_system_values_state state;
 
-   nir_foreach_block(impl, convert_block, &progress);
+   state.progress = false;
+   nir_builder_init(&state.b, impl);
+
+   nir_foreach_block(impl, convert_block, &state);
    nir_metadata_preserve(impl, nir_metadata_block_index |
                                nir_metadata_dominance);
-   return progress;
+   return state.progress;
 }
 
 bool
-- 
2.5.0.400.gff86faf



More information about the mesa-dev mailing list