From: Jason Ekstrand Date: Thu, 27 Aug 2020 16:59:54 +0000 (-0500) Subject: nir: Handle all array stride cases in nir_deref_instr_array_stride X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=0654a9e82307b854824fc1092a8b79632a2703c0;p=mesa.git nir: Handle all array stride cases in nir_deref_instr_array_stride This renames it to drop the ptr_as and makes it handle all of the stride cases. There's a bit of a tricky bit in here around Booleans but we currently use 32-bit for those always. Reviewed-by: Jesse Natalie Reviewed-by: Boris Brezillon Part-of: --- diff --git a/src/amd/llvm/ac_nir_to_llvm.c b/src/amd/llvm/ac_nir_to_llvm.c index 13f12d350dc..4f47c4d323d 100644 --- a/src/amd/llvm/ac_nir_to_llvm.c +++ b/src/amd/llvm/ac_nir_to_llvm.c @@ -5167,7 +5167,7 @@ static void visit_deref(struct ac_nir_context *ctx, break; case nir_deref_type_ptr_as_array: if (instr->mode == nir_var_mem_global) { - unsigned stride = nir_deref_instr_ptr_as_array_stride(instr); + unsigned stride = nir_deref_instr_array_stride(instr); LLVMValueRef index = get_src(ctx, instr->arr.index); if (LLVMTypeOf(index) != ctx->ac.i64) diff --git a/src/compiler/nir/nir.h b/src/compiler/nir/nir.h index 16733fd5e50..63cd06e7883 100644 --- a/src/compiler/nir/nir.h +++ b/src/compiler/nir/nir.h @@ -1501,7 +1501,7 @@ bool nir_deref_instr_has_complex_use(nir_deref_instr *instr); bool nir_deref_instr_remove_if_unused(nir_deref_instr *instr); -unsigned nir_deref_instr_ptr_as_array_stride(nir_deref_instr *instr); +unsigned nir_deref_instr_array_stride(nir_deref_instr *instr); typedef struct { nir_instr instr; diff --git a/src/compiler/nir/nir_deref.c b/src/compiler/nir/nir_deref.c index 4191b7b7599..ad0380aa28b 100644 --- a/src/compiler/nir/nir_deref.c +++ b/src/compiler/nir/nir_deref.c @@ -231,14 +231,32 @@ nir_deref_instr_has_complex_use(nir_deref_instr *deref) return false; } +static unsigned +type_scalar_size_bytes(const struct glsl_type *type) +{ + assert(glsl_type_is_vector_or_scalar(type) || + glsl_type_is_matrix(type)); + return glsl_type_is_boolean(type) ? 4 : glsl_get_bit_size(type) / 8; +} + unsigned -nir_deref_instr_ptr_as_array_stride(nir_deref_instr *deref) +nir_deref_instr_array_stride(nir_deref_instr *deref) { switch (deref->deref_type) { case nir_deref_type_array: - return glsl_get_explicit_stride(nir_deref_instr_parent(deref)->type); + case nir_deref_type_array_wildcard: { + const struct glsl_type *arr_type = nir_deref_instr_parent(deref)->type; + unsigned stride = glsl_get_explicit_stride(arr_type); + + if ((glsl_type_is_matrix(arr_type) && + glsl_matrix_type_is_row_major(arr_type)) || + (glsl_type_is_vector(arr_type) && stride == 0)) + stride = type_scalar_size_bytes(arr_type); + + return stride; + } case nir_deref_type_ptr_as_array: - return nir_deref_instr_ptr_as_array_stride(nir_deref_instr_parent(deref)); + return nir_deref_instr_array_stride(nir_deref_instr_parent(deref)); case nir_deref_type_cast: return deref->cast.ptr_stride; default: @@ -817,7 +835,7 @@ is_trivial_array_deref_cast(nir_deref_instr *cast) glsl_get_explicit_stride(nir_deref_instr_parent(parent)->type); } else if (parent->deref_type == nir_deref_type_ptr_as_array) { return cast->cast.ptr_stride == - nir_deref_instr_ptr_as_array_stride(parent); + nir_deref_instr_array_stride(parent); } else { return false; } diff --git a/src/compiler/nir/nir_lower_io.c b/src/compiler/nir/nir_lower_io.c index 651c7460cd7..28eb899114c 100644 --- a/src/compiler/nir/nir_lower_io.c +++ b/src/compiler/nir/nir_lower_io.c @@ -1250,14 +1250,7 @@ nir_explicit_io_address_from_deref(nir_builder *b, nir_deref_instr *deref, return build_addr_for_var(b, deref->var, addr_format); case nir_deref_type_array: { - nir_deref_instr *parent = nir_deref_instr_parent(deref); - - unsigned stride = glsl_get_explicit_stride(parent->type); - if ((glsl_type_is_matrix(parent->type) && - glsl_matrix_type_is_row_major(parent->type)) || - (glsl_type_is_vector(parent->type) && stride == 0)) - stride = type_scalar_size_bytes(parent->type); - + unsigned stride = nir_deref_instr_array_stride(deref); assert(stride > 0); nir_ssa_def *index = nir_ssa_for_src(b, deref->arr.index, 1); @@ -1269,7 +1262,7 @@ nir_explicit_io_address_from_deref(nir_builder *b, nir_deref_instr *deref, case nir_deref_type_ptr_as_array: { nir_ssa_def *index = nir_ssa_for_src(b, deref->arr.index, 1); index = nir_i2i(b, index, addr_get_offset_bit_size(base_addr, addr_format)); - unsigned stride = nir_deref_instr_ptr_as_array_stride(deref); + unsigned stride = nir_deref_instr_array_stride(deref); return build_addr_iadd(b, base_addr, addr_format, nir_amul_imm(b, index, stride)); } diff --git a/src/compiler/nir/nir_opt_load_store_vectorize.c b/src/compiler/nir/nir_opt_load_store_vectorize.c index 0bb5a95bbe0..224323532ac 100644 --- a/src/compiler/nir/nir_opt_load_store_vectorize.c +++ b/src/compiler/nir/nir_opt_load_store_vectorize.c @@ -331,17 +331,6 @@ type_scalar_size_bytes(const struct glsl_type *type) return glsl_type_is_boolean(type) ? 4u : glsl_get_bit_size(type) / 8u; } -static int -get_array_stride(const struct glsl_type *type) -{ - unsigned explicit_stride = glsl_get_explicit_stride(type); - if ((glsl_type_is_matrix(type) && - glsl_matrix_type_is_row_major(type)) || - (glsl_type_is_vector(type) && explicit_stride == 0)) - return type_scalar_size_bytes(type); - return explicit_stride; -} - static uint64_t mask_sign_extend(uint64_t val, unsigned bit_size) { @@ -413,11 +402,7 @@ create_entry_key_from_deref(void *mem_ctx, case nir_deref_type_ptr_as_array: { assert(parent); nir_ssa_def *index = deref->arr.index.ssa; - uint32_t stride; - if (deref->deref_type == nir_deref_type_ptr_as_array) - stride = nir_deref_instr_ptr_as_array_stride(deref); - else - stride = get_array_stride(parent->type); + uint32_t stride = nir_deref_instr_array_stride(deref); nir_ssa_def *base = index; uint64_t offset = 0, base_mul = 1; @@ -741,8 +726,8 @@ static nir_deref_instr *subtract_deref(nir_builder *b, nir_deref_instr *deref, i /* avoid adding another deref to the path */ if (deref->deref_type == nir_deref_type_ptr_as_array && nir_src_is_const(deref->arr.index) && - offset % nir_deref_instr_ptr_as_array_stride(deref) == 0) { - unsigned stride = nir_deref_instr_ptr_as_array_stride(deref); + offset % nir_deref_instr_array_stride(deref) == 0) { + unsigned stride = nir_deref_instr_array_stride(deref); nir_ssa_def *index = nir_imm_intN_t(b, nir_src_as_int(deref->arr.index) - offset / stride, deref->dest.ssa.bit_size); return nir_build_deref_ptr_as_array(b, nir_deref_instr_parent(deref), index); diff --git a/src/compiler/nir/nir_repair_ssa.c b/src/compiler/nir/nir_repair_ssa.c index 8eceadcebb0..ce5465ebf77 100644 --- a/src/compiler/nir/nir_repair_ssa.c +++ b/src/compiler/nir/nir_repair_ssa.c @@ -128,7 +128,7 @@ repair_ssa_def(nir_ssa_def *def, void *void_state) cast->mode = deref->mode; cast->type = deref->type; cast->parent = nir_src_for_ssa(block_def); - cast->cast.ptr_stride = nir_deref_instr_ptr_as_array_stride(deref); + cast->cast.ptr_stride = nir_deref_instr_array_stride(deref); nir_ssa_dest_init(&cast->instr, &cast->dest, def->num_components, def->bit_size, NULL); diff --git a/src/compiler/nir/nir_to_lcssa.c b/src/compiler/nir/nir_to_lcssa.c index 327de85d36d..de2c7b600bd 100644 --- a/src/compiler/nir/nir_to_lcssa.c +++ b/src/compiler/nir/nir_to_lcssa.c @@ -253,7 +253,7 @@ convert_loop_exit_for_ssa(nir_ssa_def *def, void *void_state) cast->mode = instr->mode; cast->type = instr->type; cast->parent = nir_src_for_ssa(&phi->dest.ssa); - cast->cast.ptr_stride = nir_deref_instr_ptr_as_array_stride(instr); + cast->cast.ptr_stride = nir_deref_instr_array_stride(instr); nir_ssa_dest_init(&cast->instr, &cast->dest, phi->dest.ssa.num_components,