nir: Support deref instructions in lower_var_copies
[mesa.git] / src / compiler / nir / nir_lower_var_copies.c
index e7b2bd5a236ac47ec1ec022d4d3e76fda092f45c..227d46747f12e72f919c468cab269c03e49c5f46 100644 (file)
@@ -26,6 +26,8 @@
  */
 
 #include "nir.h"
+#include "nir_builder.h"
+#include "nir_deref.h"
 #include "compiler/nir_types.h"
 
 /*
@@ -154,24 +156,111 @@ nir_lower_var_copy_instr(nir_intrinsic_instr *copy, nir_shader *shader)
                         &copy->variables[1]->deref, shader);
 }
 
+static nir_deref_instr *
+build_deref_to_next_wildcard(nir_builder *b,
+                             nir_deref_instr *parent,
+                             nir_deref_instr ***deref_arr)
+{
+   for (; **deref_arr; (*deref_arr)++) {
+      if ((**deref_arr)->deref_type == nir_deref_type_array_wildcard)
+         return parent;
+
+      parent = nir_build_deref_follower(b, parent, **deref_arr);
+   }
+
+   assert(**deref_arr == NULL);
+   *deref_arr = NULL;
+   return parent;
+}
+
+static void
+emit_deref_copy_load_store(nir_builder *b,
+                           nir_deref_instr *dst_deref,
+                           nir_deref_instr **dst_deref_arr,
+                           nir_deref_instr *src_deref,
+                           nir_deref_instr **src_deref_arr)
+{
+   if (dst_deref_arr || src_deref_arr) {
+      assert(dst_deref_arr && src_deref_arr);
+      dst_deref = build_deref_to_next_wildcard(b, dst_deref, &dst_deref_arr);
+      src_deref = build_deref_to_next_wildcard(b, src_deref, &src_deref_arr);
+   }
+
+   if (dst_deref_arr || src_deref_arr) {
+      assert(dst_deref_arr && src_deref_arr);
+      assert((*dst_deref_arr)->deref_type == nir_deref_type_array_wildcard);
+      assert((*src_deref_arr)->deref_type == nir_deref_type_array_wildcard);
+
+      unsigned length = glsl_get_length(src_deref->type);
+      /* The wildcards should represent the same number of elements */
+      assert(length == glsl_get_length(dst_deref->type));
+      assert(length > 0);
+
+      for (unsigned i = 0; i < length; i++) {
+         nir_ssa_def *index = nir_imm_int(b, i);
+         emit_deref_copy_load_store(b,
+                                    nir_build_deref_array(b, dst_deref, index),
+                                    dst_deref_arr + 1,
+                                    nir_build_deref_array(b, src_deref, index),
+                                    src_deref_arr + 1);
+      }
+   } else {
+      assert(dst_deref->type == src_deref->type);
+      assert(glsl_type_is_vector_or_scalar(dst_deref->type));
+
+      nir_store_deref(b, dst_deref, nir_load_deref(b, src_deref), ~0);
+   }
+}
+
+void
+nir_lower_deref_copy_instr(nir_builder *b, nir_intrinsic_instr *copy)
+{
+   /* Unfortunately, there's just no good way to handle wildcards except to
+    * flip the chain around and walk the list from variable to final pointer.
+    */
+   assert(copy->src[0].is_ssa && copy->src[1].is_ssa);
+   nir_deref_instr *dst = nir_instr_as_deref(copy->src[0].ssa->parent_instr);
+   nir_deref_instr *src = nir_instr_as_deref(copy->src[1].ssa->parent_instr);
+
+   nir_deref_path dst_path, src_path;
+   nir_deref_path_init(&dst_path, dst, NULL);
+   nir_deref_path_init(&src_path, src, NULL);
+
+   b->cursor = nir_before_instr(&copy->instr);
+   emit_deref_copy_load_store(b, dst_path.path[0], &dst_path.path[1],
+                                 src_path.path[0], &src_path.path[1]);
+
+   nir_deref_path_finish(&dst_path);
+   nir_deref_path_finish(&src_path);
+}
+
 static bool
 lower_var_copies_impl(nir_function_impl *impl)
 {
    nir_shader *shader = impl->function->shader;
    bool progress = false;
 
+   nir_builder b;
+   nir_builder_init(&b, impl);
+
    nir_foreach_block(block, impl) {
       nir_foreach_instr_safe(instr, block) {
          if (instr->type != nir_instr_type_intrinsic)
             continue;
 
          nir_intrinsic_instr *copy = nir_instr_as_intrinsic(instr);
-         if (copy->intrinsic != nir_intrinsic_copy_var)
+         if (copy->intrinsic == nir_intrinsic_copy_var)
+            nir_lower_var_copy_instr(copy, shader);
+         else if (copy->intrinsic == nir_intrinsic_copy_deref)
+            nir_lower_deref_copy_instr(&b, copy);
+         else
             continue;
 
-         nir_lower_var_copy_instr(copy, shader);
-
          nir_instr_remove(&copy->instr);
+         if (copy->intrinsic == nir_intrinsic_copy_deref) {
+            nir_deref_instr_remove_if_unused(nir_src_as_deref(copy->src[0]));
+            nir_deref_instr_remove_if_unused(nir_src_as_deref(copy->src[1]));
+         }
          progress = true;
          ralloc_free(copy);
       }
@@ -192,8 +281,6 @@ nir_lower_var_copies(nir_shader *shader)
 {
    bool progress = false;
 
-   nir_assert_lowered_derefs(shader, nir_lower_load_store_derefs);
-
    nir_foreach_function(function, shader) {
       if (function->impl)
          progress |= lower_var_copies_impl(function->impl);