[Mesa-dev] [PATCH 07/12] nir: Move get_const_initializer_load from vars_to_ssa to NIR core

Jason Ekstrand jason at jlekstrand.net
Fri Apr 10 17:48:50 PDT 2015


---
 src/glsl/nir/nir.c                   | 60 +++++++++++++++++++++++++++++++++
 src/glsl/nir/nir.h                   |  3 ++
 src/glsl/nir/nir_lower_vars_to_ssa.c | 64 ++----------------------------------
 3 files changed, 65 insertions(+), 62 deletions(-)

diff --git a/src/glsl/nir/nir.c b/src/glsl/nir/nir.c
index c6e5361..a7ee361 100644
--- a/src/glsl/nir/nir.c
+++ b/src/glsl/nir/nir.c
@@ -589,6 +589,66 @@ nir_copy_deref(void *mem_ctx, nir_deref *deref)
    return NULL;
 }
 
+/* Returns a load_const instruction that represents the constant
+ * initializer for the given deref chain.  The caller is responsible for
+ * ensuring that there actually is a constant initializer.
+ */
+nir_load_const_instr *
+nir_deref_get_const_initializer_load(nir_shader *shader, nir_deref_var *deref)
+{
+   nir_constant *constant = deref->var->constant_initializer;
+   assert(constant);
+
+   const nir_deref *tail = &deref->deref;
+   unsigned matrix_offset = 0;
+   while (tail->child) {
+      switch (tail->child->deref_type) {
+      case nir_deref_type_array: {
+         nir_deref_array *arr = nir_deref_as_array(tail->child);
+         assert(arr->deref_array_type == nir_deref_array_type_direct);
+         if (glsl_type_is_matrix(tail->type)) {
+            assert(arr->deref.child == NULL);
+            matrix_offset = arr->base_offset;
+         } else {
+            constant = constant->elements[arr->base_offset];
+         }
+         break;
+      }
+
+      case nir_deref_type_struct: {
+         constant = constant->elements[nir_deref_as_struct(tail->child)->index];
+         break;
+      }
+
+      default:
+         unreachable("Invalid deref child type");
+      }
+
+      tail = tail->child;
+   }
+
+   nir_load_const_instr *load =
+      nir_load_const_instr_create(shader, glsl_get_vector_elements(tail->type));
+
+   matrix_offset *= load->def.num_components;
+   for (unsigned i = 0; i < load->def.num_components; i++) {
+      switch (glsl_get_base_type(tail->type)) {
+      case GLSL_TYPE_FLOAT:
+      case GLSL_TYPE_INT:
+      case GLSL_TYPE_UINT:
+         load->value.u[i] = constant->value.u[matrix_offset + i];
+         break;
+      case GLSL_TYPE_BOOL:
+         load->value.u[i] = constant->value.b[matrix_offset + i] ?
+                             NIR_TRUE : NIR_FALSE;
+         break;
+      default:
+         unreachable("Invalid immediate type");
+      }
+   }
+
+   return load;
+}
 
 /**
  * \name Control flow modification
diff --git a/src/glsl/nir/nir.h b/src/glsl/nir/nir.h
index 5a7f581..0d8d026 100644
--- a/src/glsl/nir/nir.h
+++ b/src/glsl/nir/nir.h
@@ -1514,6 +1514,9 @@ nir_deref_struct *nir_deref_struct_create(void *mem_ctx, unsigned field_index);
 
 nir_deref *nir_copy_deref(void *mem_ctx, nir_deref *deref);
 
+nir_load_const_instr *
+nir_deref_get_const_initializer_load(nir_shader *shader, nir_deref_var *deref);
+
 void nir_instr_insert_before(nir_instr *instr, nir_instr *before);
 void nir_instr_insert_after(nir_instr *instr, nir_instr *after);
 
diff --git a/src/glsl/nir/nir_lower_vars_to_ssa.c b/src/glsl/nir/nir_lower_vars_to_ssa.c
index 1afa9b7..6aa3a79 100644
--- a/src/glsl/nir/nir_lower_vars_to_ssa.c
+++ b/src/glsl/nir/nir_lower_vars_to_ssa.c
@@ -489,67 +489,6 @@ lower_copies_to_load_store(struct deref_node *node,
    return true;
 }
 
-/* Returns a load_const instruction that represents the constant
- * initializer for the given deref chain.  The caller is responsible for
- * ensuring that there actually is a constant initializer.
- */
-static nir_load_const_instr *
-get_const_initializer_load(const nir_deref_var *deref,
-                           struct lower_variables_state *state)
-{
-   nir_constant *constant = deref->var->constant_initializer;
-   const nir_deref *tail = &deref->deref;
-   unsigned matrix_offset = 0;
-   while (tail->child) {
-      switch (tail->child->deref_type) {
-      case nir_deref_type_array: {
-         nir_deref_array *arr = nir_deref_as_array(tail->child);
-         assert(arr->deref_array_type == nir_deref_array_type_direct);
-         if (glsl_type_is_matrix(tail->type)) {
-            assert(arr->deref.child == NULL);
-            matrix_offset = arr->base_offset;
-         } else {
-            constant = constant->elements[arr->base_offset];
-         }
-         break;
-      }
-
-      case nir_deref_type_struct: {
-         constant = constant->elements[nir_deref_as_struct(tail->child)->index];
-         break;
-      }
-
-      default:
-         unreachable("Invalid deref child type");
-      }
-
-      tail = tail->child;
-   }
-
-   nir_load_const_instr *load =
-      nir_load_const_instr_create(state->shader,
-                                  glsl_get_vector_elements(tail->type));
-
-   matrix_offset *= load->def.num_components;
-   for (unsigned i = 0; i < load->def.num_components; i++) {
-      switch (glsl_get_base_type(tail->type)) {
-      case GLSL_TYPE_FLOAT:
-      case GLSL_TYPE_INT:
-      case GLSL_TYPE_UINT:
-         load->value.u[i] = constant->value.u[matrix_offset + i];
-         break;
-      case GLSL_TYPE_BOOL:
-         load->value.u[i] = constant->value.b[matrix_offset + i] ?
-                             NIR_TRUE : NIR_FALSE;
-         break;
-      default:
-         unreachable("Invalid immediate type");
-      }
-   }
-
-   return load;
-}
-
 /** Pushes an SSA def onto the def stack for the given node
  *
  * Each node is potentially associated with a stack of SSA definitions.
@@ -987,7 +926,8 @@ nir_lower_vars_to_ssa_impl(nir_function_impl *impl)
       progress = true;
 
       if (deref->var->constant_initializer) {
-         nir_load_const_instr *load = get_const_initializer_load(deref, &state);
+         nir_load_const_instr *load =
+            nir_deref_get_const_initializer_load(state.shader, deref);
          nir_ssa_def_init(&load->instr, &load->def,
                           glsl_get_vector_elements(node->type), NULL);
          nir_instr_insert_before_cf_list(&impl->body, &load->instr);
-- 
2.3.5



More information about the mesa-dev mailing list