[Mesa-dev] [PATCH 12/13] SQUASH: nir: Add a helper for moving a source and use it in texture lowering

Jason Ekstrand jason at jlekstrand.net
Mon Apr 27 21:03:47 PDT 2015


One of the side-effects of using a linked list for use/def sets is that you
can no longer simply copy them around.  There were a couple of places in
the texture lowering passes that realloced or used memmove on lists of
sources.  Instead, this commit adds a helper for moving a source and uses
it for these cases.
---
 src/glsl/nir/nir.c                     | 12 ++++++++++++
 src/glsl/nir/nir.h                     |  1 +
 src/glsl/nir/nir_lower_samplers.cpp    | 29 +++++++++++++++++++----------
 src/glsl/nir/nir_lower_tex_projector.c |  7 ++++---
 4 files changed, 36 insertions(+), 13 deletions(-)

diff --git a/src/glsl/nir/nir.c b/src/glsl/nir/nir.c
index be13c90..f03e80a 100644
--- a/src/glsl/nir/nir.c
+++ b/src/glsl/nir/nir.c
@@ -1864,6 +1864,18 @@ nir_instr_rewrite_src(nir_instr *instr, nir_src *src, nir_src new_src)
 }
 
 void
+nir_instr_move_src(nir_instr *dest_instr, nir_src *dest, nir_src *src)
+{
+   assert(!src_is_valid(dest) || dest->parent_instr == dest_instr);
+
+   src_remove_all_uses(dest);
+   src_remove_all_uses(src);
+   *dest = *src;
+   *src = NIR_SRC_INIT;
+   src_add_all_uses(dest, dest_instr, NULL);
+}
+
+void
 nir_if_rewrite_condition(nir_if *if_stmt, nir_src new_src)
 {
    nir_src *src = &if_stmt->condition;
diff --git a/src/glsl/nir/nir.h b/src/glsl/nir/nir.h
index ac027b0..697d37e 100644
--- a/src/glsl/nir/nir.h
+++ b/src/glsl/nir/nir.h
@@ -1580,6 +1580,7 @@ bool nir_foreach_src(nir_instr *instr, nir_foreach_src_cb cb, void *state);
 nir_const_value *nir_src_as_const_value(nir_src src);
 bool nir_srcs_equal(nir_src src1, nir_src src2);
 void nir_instr_rewrite_src(nir_instr *instr, nir_src *src, nir_src new_src);
+void nir_instr_move_src(nir_instr *dest_instr, nir_src *dest, nir_src *src);
 void nir_if_rewrite_condition(nir_if *if_stmt, nir_src new_src);
 
 void nir_ssa_dest_init(nir_instr *instr, nir_dest *dest,
diff --git a/src/glsl/nir/nir_lower_samplers.cpp b/src/glsl/nir/nir_lower_samplers.cpp
index e9e152f..8fc5909 100644
--- a/src/glsl/nir/nir_lower_samplers.cpp
+++ b/src/glsl/nir/nir_lower_samplers.cpp
@@ -83,21 +83,30 @@ lower_sampler(nir_tex_instr *instr, const struct gl_shader_program *shader_progr
                ralloc_asprintf_append(&name, "[%u]", deref_array->base_offset);
             break;
          case nir_deref_array_type_indirect: {
-            instr->src = reralloc(instr, instr->src, nir_tex_src,
-                                  instr->num_srcs + 1);
-            memset(&instr->src[instr->num_srcs], 0, sizeof *instr->src);
+            /* First, we have to resize the array of texture sources */
+            nir_tex_src *new_srcs = rzalloc_array(instr, nir_tex_src,
+                                                  instr->num_srcs + 1);
+
+            for (unsigned i = 0; i < instr->num_srcs; i++) {
+               new_srcs[i].src_type = instr->src[i].src_type;
+               nir_instr_move_src(&instr->instr, &new_srcs[i].src,
+                                  &instr->src[i].src);
+            }
+
+            ralloc_free(instr->src);
+            instr->src = new_srcs;
+
+            /* Now we can go ahead and move the source over to being a
+             * first-class texture source.
+             */
             instr->src[instr->num_srcs].src_type = nir_tex_src_sampler_offset;
             instr->num_srcs++;
-
-            nir_instr_rewrite_src(&instr->instr,
-                                  &instr->src[instr->num_srcs - 1].src,
-                                  deref_array->indirect);
+            nir_instr_move_src(&instr->instr,
+                               &instr->src[instr->num_srcs - 1].src,
+                               &deref_array->indirect);
 
             instr->sampler_array_size = glsl_get_length(deref->type);
 
-            nir_instr_rewrite_src(&instr->instr, &deref_array->indirect,
-                                  NIR_SRC_INIT);
-
             if (deref_array->deref.child)
                ralloc_strcat(&name, "[0]");
             break;
diff --git a/src/glsl/nir/nir_lower_tex_projector.c b/src/glsl/nir/nir_lower_tex_projector.c
index 6e2cc80..357131c 100644
--- a/src/glsl/nir/nir_lower_tex_projector.c
+++ b/src/glsl/nir/nir_lower_tex_projector.c
@@ -111,9 +111,10 @@ nir_lower_tex_projector_block(nir_block *block, void *void_state)
        */
       nir_instr_rewrite_src(&tex->instr, &tex->src[proj_index].src,
                             NIR_SRC_INIT);
-      memmove(&tex->src[proj_index],
-              &tex->src[proj_index + 1],
-              (tex->num_srcs - proj_index) * sizeof(*tex->src));
+      for (int i = proj_index + 1; i < tex->num_srcs; i++) {
+         tex->src[i-1].src_type = tex->src[i].src_type;
+         nir_instr_move_src(&tex->instr, &tex->src[i-1].src, &tex->src[i].src);
+      }
       tex->num_srcs--;
    }
 
-- 
2.3.6



More information about the mesa-dev mailing list