From: Jason Ekstrand Date: Thu, 7 Mar 2019 17:45:13 +0000 (-0600) Subject: nir/builder: Add a build_deref_array_imm helper X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=1664de5924aa6d761be21afdee411654121515d1;p=mesa.git nir/builder: Add a build_deref_array_imm helper 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 Reviewed-by: Lionel Landwerlin --- diff --git a/src/compiler/nir/nir_builder.h b/src/compiler/nir/nir_builder.h index 56e76ddcb39..4b4d1eef462 100644 --- a/src/compiler/nir/nir_builder.h +++ b/src/compiler/nir/nir_builder.h @@ -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) diff --git a/src/compiler/nir/nir_lower_constant_initializers.c b/src/compiler/nir/nir_lower_constant_initializers.c index e09eacea033..cadce433e24 100644 --- a/src/compiler/nir/nir_lower_constant_initializers.c +++ b/src/compiler/nir/nir_lower_constant_initializers.c @@ -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]); } } diff --git a/src/compiler/nir/nir_lower_indirect_derefs.c b/src/compiler/nir/nir_lower_indirect_derefs.c index cf025bb0425..1e1e84fb3b0 100644 --- a/src/compiler/nir/nir_lower_indirect_derefs.c +++ b/src/compiler/nir/nir_lower_indirect_derefs.c @@ -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; diff --git a/src/compiler/nir/nir_lower_var_copies.c b/src/compiler/nir/nir_lower_var_copies.c index 1e7ad784934..d677b4e505b 100644 --- a/src/compiler/nir/nir_lower_var_copies.c +++ b/src/compiler/nir/nir_lower_var_copies.c @@ -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 { diff --git a/src/compiler/nir/nir_split_vars.c b/src/compiler/nir/nir_split_vars.c index df059276411..394021e608e 100644 --- a/src/compiler/nir/nir_split_vars.c +++ b/src/compiler/nir/nir_split_vars.c @@ -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 */ diff --git a/src/compiler/nir/tests/vars_tests.cpp b/src/compiler/nir/tests/vars_tests.cpp index 893ce297a43..f72b6b99f62 100644 --- a/src/compiler/nir/tests/vars_tests.cpp +++ b/src/compiler/nir/tests/vars_tests.cpp @@ -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); diff --git a/src/compiler/spirv/vtn_variables.c b/src/compiler/spirv/vtn_variables.c index c3ce15aaaaf..053d6089e45 100644 --- a/src/compiler/spirv/vtn_variables.c +++ b/src/compiler/spirv/vtn_variables.c @@ -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 { diff --git a/src/intel/vulkan/anv_nir_lower_multiview.c b/src/intel/vulkan/anv_nir_lower_multiview.c index c42d9c2dfa2..ffe9e7bb972 100644 --- a/src/intel/vulkan/anv_nir_lower_multiview.c +++ b/src/intel/vulkan/anv_nir_lower_multiview.c @@ -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); }