nir/builder: Add a build_deref_array_imm helper
authorJason Ekstrand <jason.ekstrand@intel.com>
Thu, 7 Mar 2019 17:45:13 +0000 (11:45 -0600)
committerJason Ekstrand <jason@jlekstrand.net>
Thu, 7 Mar 2019 21:20:30 +0000 (21:20 +0000)
Unlike most of the cases in which we do this by hand, the new helper
properly handles non-32-bit pointers.

Reviewed-by: Karol Herbst <kherbst@redhat.com>
Reviewed-by: Lionel Landwerlin <lionel.g.landwerlin@intel.com>
src/compiler/nir/nir_builder.h
src/compiler/nir/nir_lower_constant_initializers.c
src/compiler/nir/nir_lower_indirect_derefs.c
src/compiler/nir/nir_lower_var_copies.c
src/compiler/nir/nir_split_vars.c
src/compiler/nir/tests/vars_tests.cpp
src/compiler/spirv/vtn_variables.c
src/intel/vulkan/anv_nir_lower_multiview.c

index 56e76ddcb3979f4424d26e96c21dac8cfc1f0c28..4b4d1eef46213a7386ac8645ac367cee8baa7bd2 100644 (file)
@@ -843,6 +843,17 @@ nir_build_deref_array(nir_builder *build, nir_deref_instr *parent,
    return deref;
 }
 
+static inline nir_deref_instr *
+nir_build_deref_array_imm(nir_builder *build, nir_deref_instr *parent,
+                          int64_t index)
+{
+   assert(parent->dest.is_ssa);
+   nir_ssa_def *idx_ssa = nir_imm_intN_t(build, index,
+                                         parent->dest.ssa.bit_size);
+
+   return nir_build_deref_array(build, parent, idx_ssa);
+}
+
 static inline nir_deref_instr *
 nir_build_deref_ptr_as_array(nir_builder *build, nir_deref_instr *parent,
                              nir_ssa_def *index)
index e09eacea033be34b6b554d626c5fbaba89fcd4ca..cadce433e2430bc554e2b355a57088a7bec13c09 100644 (file)
@@ -44,7 +44,7 @@ build_constant_load(nir_builder *b, nir_deref_instr *deref, nir_constant *c)
             nir_load_const_instr_create(b->shader, rows, bit_size);
          load->value = c->values[i];
          nir_builder_instr_insert(b, &load->instr);
-         nir_store_deref(b, nir_build_deref_array(b, deref, nir_imm_int(b, i)),
+         nir_store_deref(b, nir_build_deref_array_imm(b, deref, i),
                          &load->def, ~0);
       }
    } else if (glsl_type_is_struct_or_ifc(deref->type)) {
@@ -58,7 +58,7 @@ build_constant_load(nir_builder *b, nir_deref_instr *deref, nir_constant *c)
       unsigned len = glsl_get_length(deref->type);
       for (unsigned i = 0; i < len; i++) {
          build_constant_load(b,
-                             nir_build_deref_array(b, deref, nir_imm_int(b, i)),
+                             nir_build_deref_array_imm(b, deref, i),
                              c->elements[i]);
       }
    }
index cf025bb04259158461005b613c679911bf0ef0f3..1e1e84fb3b0e54faaaa5524da11a270c02246ad1 100644 (file)
@@ -40,9 +40,8 @@ emit_indirect_load_store_deref(nir_builder *b, nir_intrinsic_instr *orig_instr,
 {
    assert(start < end);
    if (start == end - 1) {
-      nir_ssa_def *index = nir_imm_int(b, start);
       emit_load_store_deref(b, orig_instr,
-                            nir_build_deref_array(b, parent, index),
+                            nir_build_deref_array_imm(b, parent, start),
                             deref_arr + 1, dest, src);
    } else {
       int mid = start + (end - start) / 2;
index 1e7ad7849344bea85eecf72f3cf088493a2fceb4..d677b4e505b97b8207269a3f7db60cf856d66fe0 100644 (file)
@@ -75,11 +75,10 @@ emit_deref_copy_load_store(nir_builder *b,
       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),
+                                    nir_build_deref_array_imm(b, dst_deref, i),
                                     dst_deref_arr + 1,
-                                    nir_build_deref_array(b, src_deref, index),
+                                    nir_build_deref_array_imm(b, src_deref, i),
                                     src_deref_arr + 1);
       }
    } else {
index df0592764116177144b6a371abd235579b609220..394021e608e01449702d80c604e7140f7c6d49d4 100644 (file)
@@ -616,11 +616,10 @@ emit_split_copies(nir_builder *b,
                 glsl_get_length(src_path->path[src_level]->type));
          unsigned len = glsl_get_length(dst_path->path[dst_level]->type);
          for (unsigned i = 0; i < len; i++) {
-            nir_ssa_def *idx = nir_imm_int(b, i);
             emit_split_copies(b, dst_info, dst_path, dst_level + 1,
-                              nir_build_deref_array(b, dst, idx),
+                              nir_build_deref_array_imm(b, dst, i),
                               src_info, src_path, src_level + 1,
-                              nir_build_deref_array(b, src, idx));
+                              nir_build_deref_array_imm(b, src, i));
          }
       } else {
          /* Neither side is being split so we just keep going */
index 893ce297a43f745c497df53524d39a06122629b2..f72b6b99f626da8d44280c95c05b94ef8d883fb1 100644 (file)
@@ -476,7 +476,7 @@ TEST_F(nir_copy_prop_vars_test, load_direct_array_deref_on_vector_reuses_previou
 
    /* This load will be dropped, as vec.y (or vec[1]) is already known. */
    nir_deref_instr *deref =
-      nir_build_deref_array(b, nir_build_deref_var(b, vec), nir_imm_int(b, 1));
+      nir_build_deref_array_imm(b, nir_build_deref_var(b, vec), 1);
    nir_ssa_def *loaded_from_deref = nir_load_deref(b, deref);
 
    /* This store should use the value loaded from in1. */
@@ -509,7 +509,7 @@ TEST_F(nir_copy_prop_vars_test, load_direct_array_deref_on_vector_reuses_previou
 
    /* This load will be replaced with one from in0. */
    nir_deref_instr *deref =
-      nir_build_deref_array(b, nir_build_deref_var(b, vec), nir_imm_int(b, 1));
+      nir_build_deref_array_imm(b, nir_build_deref_var(b, vec), 1);
    nir_load_deref(b, deref);
 
    nir_validate_shader(b->shader, NULL);
@@ -532,7 +532,7 @@ TEST_F(nir_copy_prop_vars_test, load_direct_array_deref_on_vector_gets_reused)
 
    /* Loading "vec[1]" deref will save the information about vec.y. */
    nir_deref_instr *deref =
-      nir_build_deref_array(b, nir_build_deref_var(b, vec), nir_imm_int(b, 1));
+      nir_build_deref_array_imm(b, nir_build_deref_var(b, vec), 1);
    nir_load_deref(b, deref);
 
    /* Store to vec.x. */
@@ -566,16 +566,16 @@ TEST_F(nir_copy_prop_vars_test, store_load_direct_array_deref_on_vector)
 
    /* Store to "vec[1]" and "vec[0]". */
    nir_deref_instr *store_deref_y =
-      nir_build_deref_array(b, nir_build_deref_var(b, vec), nir_imm_int(b, 1));
+      nir_build_deref_array_imm(b, nir_build_deref_var(b, vec), 1);
    nir_store_deref(b, store_deref_y, nir_imm_int(b, 20), 1);
 
    nir_deref_instr *store_deref_x =
-      nir_build_deref_array(b, nir_build_deref_var(b, vec), nir_imm_int(b, 0));
+      nir_build_deref_array_imm(b, nir_build_deref_var(b, vec), 0);
    nir_store_deref(b, store_deref_x, nir_imm_int(b, 10), 1);
 
    /* Both loads below will be dropped, because the values are already known. */
    nir_deref_instr *load_deref_y =
-      nir_build_deref_array(b, nir_build_deref_var(b, vec), nir_imm_int(b, 1));
+      nir_build_deref_array_imm(b, nir_build_deref_var(b, vec), 1);
    nir_store_var(b, out0, nir_load_deref(b, load_deref_y), 1);
 
    nir_store_var(b, out1, nir_load_var(b, vec), 1);
index c3ce15aaaaf9d23f339b808aeeedc93bd51c4438..053d6089e45e22805d01c0fef609ec38a058d409 100644 (file)
@@ -602,7 +602,7 @@ _vtn_local_load_store(struct vtn_builder *b, bool load, nir_deref_instr *deref,
       unsigned elems = glsl_get_length(deref->type);
       for (unsigned i = 0; i < elems; i++) {
          nir_deref_instr *child =
-            nir_build_deref_array(&b->nb, deref, nir_imm_int(&b->nb, i));
+            nir_build_deref_array_imm(&b->nb, deref, i);
          _vtn_local_load_store(b, load, child, inout->elems[i]);
       }
    } else {
index c42d9c2dfa2533264b1c283c8c39672b9c34199b..ffe9e7bb9724cbc1c4ef4799ae433c5c902492e1 100644 (file)
@@ -136,7 +136,7 @@ build_view_index(struct lower_multiview_state *state)
 
          nir_deref_instr *deref = nir_build_deref_var(b, idx_var);
          if (glsl_type_is_array(type))
-            deref = nir_build_deref_array(b, deref, nir_imm_int(b, 0));
+            deref = nir_build_deref_array_imm(b, deref, 0);
 
          state->view_index = nir_load_deref(b, deref);
       }