X-Git-Url: https://git.libre-soc.org/?a=blobdiff_plain;f=src%2Fcompiler%2Fnir%2Fnir_deref.c;h=4191b7b75992102cd0b25b978b8edf12da5c53c2;hb=30fca3b2e65c6125c24894ed11b15e09a52b42e6;hp=da66c5f04879880a4630d22b44d78677ebb15b07;hpb=c646cd4bac0999547cb61a04f8e80f828d373232;p=mesa.git diff --git a/src/compiler/nir/nir_deref.c b/src/compiler/nir/nir_deref.c index da66c5f0487..4191b7b7599 100644 --- a/src/compiler/nir/nir_deref.c +++ b/src/compiler/nir/nir_deref.c @@ -26,6 +26,19 @@ #include "nir_deref.h" #include "util/hash_table.h" +static bool +is_trivial_deref_cast(nir_deref_instr *cast) +{ + nir_deref_instr *parent = nir_src_as_deref(cast->parent); + if (!parent) + return false; + + return cast->mode == parent->mode && + cast->type == parent->type && + cast->dest.ssa.num_components == parent->dest.ssa.num_components && + cast->dest.ssa.bit_size == parent->dest.ssa.bit_size; +} + void nir_deref_path_init(nir_deref_path *path, nir_deref_instr *deref, void *mem_ctx) @@ -44,6 +57,8 @@ nir_deref_path_init(nir_deref_path *path, *tail = NULL; for (nir_deref_instr *d = deref; d; d = nir_deref_instr_parent(d)) { + if (d->deref_type == nir_deref_type_cast && is_trivial_deref_cast(d)) + continue; count++; if (count <= max_short_path_len) *(--head) = d; @@ -64,8 +79,11 @@ nir_deref_path_init(nir_deref_path *path, path->path = ralloc_array(mem_ctx, nir_deref_instr *, count + 1); head = tail = path->path + count; *tail = NULL; - for (nir_deref_instr *d = deref; d; d = nir_deref_instr_parent(d)) + for (nir_deref_instr *d = deref; d; d = nir_deref_instr_parent(d)) { + if (d->deref_type == nir_deref_type_cast && is_trivial_deref_cast(d)) + continue; *(--head) = d; + } done: assert(head == path->path); @@ -92,7 +110,7 @@ nir_deref_instr_remove_if_unused(nir_deref_instr *instr) for (nir_deref_instr *d = instr; d; d = nir_deref_instr_parent(d)) { /* If anyone is using this deref, leave it alone */ assert(d->dest.is_ssa); - if (!list_empty(&d->dest.ssa.uses)) + if (!list_is_empty(&d->dest.ssa.uses)) break; nir_instr_remove(&d->instr); @@ -216,17 +234,15 @@ nir_deref_instr_has_complex_use(nir_deref_instr *deref) unsigned nir_deref_instr_ptr_as_array_stride(nir_deref_instr *deref) { - assert(deref->deref_type == nir_deref_type_ptr_as_array); - nir_deref_instr *parent = nir_deref_instr_parent(deref); - switch (parent->deref_type) { + switch (deref->deref_type) { case nir_deref_type_array: - return glsl_get_explicit_stride(nir_deref_instr_parent(parent)->type); + return glsl_get_explicit_stride(nir_deref_instr_parent(deref)->type); case nir_deref_type_ptr_as_array: - return nir_deref_instr_ptr_as_array_stride(parent); + return nir_deref_instr_ptr_as_array_stride(nir_deref_instr_parent(deref)); case nir_deref_type_cast: - return parent->cast.ptr_stride; + return deref->cast.ptr_stride; default: - unreachable("Invalid parent for ptr_as_array deref"); + return 0; } } @@ -263,19 +279,24 @@ nir_deref_instr_get_const_offset(nir_deref_instr *deref, nir_deref_path path; nir_deref_path_init(&path, deref, NULL); - assert(path.path[0]->deref_type == nir_deref_type_var); - unsigned offset = 0; for (nir_deref_instr **p = &path.path[1]; *p; p++) { - if ((*p)->deref_type == nir_deref_type_array) { + switch ((*p)->deref_type) { + case nir_deref_type_array: offset += nir_src_as_uint((*p)->arr.index) * type_get_array_stride((*p)->type, size_align); - } else if ((*p)->deref_type == nir_deref_type_struct) { + break; + case nir_deref_type_struct: { /* p starts at path[1], so this is safe */ nir_deref_instr *parent = *(p - 1); offset += struct_type_get_field_offset(parent->type, size_align, (*p)->strct.index); - } else { + break; + } + case nir_deref_type_cast: + /* A cast doesn't contribute to the offset */ + break; + default: unreachable("Unsupported deref type"); } } @@ -292,22 +313,28 @@ nir_build_deref_offset(nir_builder *b, nir_deref_instr *deref, nir_deref_path path; nir_deref_path_init(&path, deref, NULL); - assert(path.path[0]->deref_type == nir_deref_type_var); - - nir_ssa_def *offset = nir_imm_int(b, 0); + nir_ssa_def *offset = nir_imm_intN_t(b, 0, deref->dest.ssa.bit_size); for (nir_deref_instr **p = &path.path[1]; *p; p++) { - if ((*p)->deref_type == nir_deref_type_array) { + switch ((*p)->deref_type) { + case nir_deref_type_array: { nir_ssa_def *index = nir_ssa_for_src(b, (*p)->arr.index, 1); int stride = type_get_array_stride((*p)->type, size_align); - offset = nir_iadd(b, offset, nir_imul_imm(b, index, stride)); - } else if ((*p)->deref_type == nir_deref_type_struct) { + offset = nir_iadd(b, offset, nir_amul_imm(b, index, stride)); + break; + } + case nir_deref_type_struct: { /* p starts at path[1], so this is safe */ nir_deref_instr *parent = *(p - 1); unsigned field_offset = struct_type_get_field_offset(parent->type, size_align, (*p)->strct.index); offset = nir_iadd_imm(b, offset, field_offset); - } else { + break; + } + case nir_deref_type_cast: + /* A cast doesn't contribute to the offset */ + break; + default: unreachable("Unsupported deref type"); } } @@ -403,7 +430,7 @@ deref_path_contains_coherent_decoration(nir_deref_path *path) { assert(path->path[0]->deref_type == nir_deref_type_var); - if (path->path[0]->var->data.image.access & ACCESS_COHERENT) + if (path->path[0]->var->data.access & ACCESS_COHERENT) return true; for (nir_deref_instr **p = &path->path[1]; *p; p++) { @@ -489,8 +516,8 @@ nir_compare_deref_paths(nir_deref_path *a_path, } /* We're at either the tail or the divergence point between the two deref - * paths. Look to see if either contains a ptr_as_array deref. It it - * does we don't know how to safely make any inferences. Hopefully, + * paths. Look to see if either contains cast or a ptr_as_array deref. If + * it does we don't know how to safely make any inferences. Hopefully, * nir_opt_deref will clean most of these up and we can start inferring * things again. * @@ -500,11 +527,13 @@ nir_compare_deref_paths(nir_deref_path *a_path, * different constant indices. */ for (nir_deref_instr **t_p = a_p; *t_p; t_p++) { - if ((*t_p)->deref_type == nir_deref_type_ptr_as_array) + if ((*t_p)->deref_type == nir_deref_type_cast || + (*t_p)->deref_type == nir_deref_type_ptr_as_array) return nir_derefs_may_alias_bit; } for (nir_deref_instr **t_p = b_p; *t_p; t_p++) { - if ((*t_p)->deref_type == nir_deref_type_ptr_as_array) + if ((*t_p)->deref_type == nir_deref_type_cast || + (*t_p)->deref_type == nir_deref_type_ptr_as_array) return nir_derefs_may_alias_bit; } @@ -641,11 +670,15 @@ rematerialize_deref_in_block(nir_deref_instr *deref, switch (deref->deref_type) { case nir_deref_type_var: case nir_deref_type_array_wildcard: - case nir_deref_type_cast: /* Nothing more to do */ break; + case nir_deref_type_cast: + new_deref->cast.ptr_stride = deref->cast.ptr_stride; + break; + case nir_deref_type_array: + case nir_deref_type_ptr_as_array: assert(!nir_src_as_deref(deref->arr.index)); nir_src_copy(&new_deref->arr.index, &deref->arr.index, new_deref); break; @@ -702,7 +735,7 @@ nir_rematerialize_derefs_in_use_blocks_impl(nir_function_impl *impl) struct rematerialize_deref_state state = { 0 }; nir_builder_init(&state.builder, impl); - nir_foreach_block(block, impl) { + nir_foreach_block_unstructured(block, impl) { state.block = block; /* Start each block with a fresh cache */ @@ -736,17 +769,40 @@ nir_rematerialize_derefs_in_use_blocks_impl(nir_function_impl *impl) return state.progress; } -static bool -is_trivial_deref_cast(nir_deref_instr *cast) +static void +nir_deref_instr_fixup_child_types(nir_deref_instr *parent) { - nir_deref_instr *parent = nir_src_as_deref(cast->parent); - if (!parent) - return false; + nir_foreach_use(use, &parent->dest.ssa) { + if (use->parent_instr->type != nir_instr_type_deref) + continue; - return cast->mode == parent->mode && - cast->type == parent->type && - cast->dest.ssa.num_components == parent->dest.ssa.num_components && - cast->dest.ssa.bit_size == parent->dest.ssa.bit_size; + nir_deref_instr *child = nir_instr_as_deref(use->parent_instr); + switch (child->deref_type) { + case nir_deref_type_var: + unreachable("nir_deref_type_var cannot be a child"); + + case nir_deref_type_array: + case nir_deref_type_array_wildcard: + child->type = glsl_get_array_element(parent->type); + break; + + case nir_deref_type_ptr_as_array: + child->type = parent->type; + break; + + case nir_deref_type_struct: + child->type = glsl_get_struct_field(parent->type, + child->strct.index); + break; + + case nir_deref_type_cast: + /* We stop the recursion here */ + continue; + } + + /* Recurse into children */ + nir_deref_instr_fixup_child_types(child); + } } static bool @@ -796,6 +852,44 @@ opt_remove_cast_cast(nir_deref_instr *cast) return true; } +static bool +opt_remove_sampler_cast(nir_deref_instr *cast) +{ + assert(cast->deref_type == nir_deref_type_cast); + nir_deref_instr *parent = nir_src_as_deref(cast->parent); + if (parent == NULL) + return false; + + /* Strip both types down to their non-array type and bail if there are any + * discrepancies in array lengths. + */ + const struct glsl_type *parent_type = parent->type; + const struct glsl_type *cast_type = cast->type; + while (glsl_type_is_array(parent_type) && glsl_type_is_array(cast_type)) { + if (glsl_get_length(parent_type) != glsl_get_length(cast_type)) + return false; + parent_type = glsl_get_array_element(parent_type); + cast_type = glsl_get_array_element(cast_type); + } + + if (glsl_type_is_array(parent_type) || glsl_type_is_array(cast_type)) + return false; + + if (!glsl_type_is_sampler(parent_type) || + cast_type != glsl_bare_sampler_type()) + return false; + + /* We're a cast from a more detailed sampler type to a bare sampler */ + nir_ssa_def_rewrite_uses(&cast->dest.ssa, + nir_src_for_ssa(&parent->dest.ssa)); + nir_instr_remove(&cast->instr); + + /* Recursively crawl the deref tree and clean up types */ + nir_deref_instr_fixup_child_types(parent); + + return true; +} + /** * Is this casting a struct to a contained struct. * struct a { struct b field0 }; @@ -835,6 +929,9 @@ opt_deref_cast(nir_builder *b, nir_deref_instr *cast) if (opt_replace_struct_wrapper_cast(b, cast)) return true; + if (opt_remove_sampler_cast(cast)) + return true; + progress = opt_remove_cast_cast(cast); if (!is_trivial_deref_cast(cast)) return progress; @@ -857,9 +954,11 @@ opt_deref_cast(nir_builder *b, nir_deref_instr *cast) } /* If uses would be a bit crazy */ - assert(list_empty(&cast->dest.ssa.if_uses)); + assert(list_is_empty(&cast->dest.ssa.if_uses)); + + if (nir_deref_instr_remove_if_unused(cast)) + progress = true; - nir_deref_instr_remove_if_unused(cast); return progress; } @@ -946,9 +1045,7 @@ nir_opt_deref_impl(nir_function_impl *impl) nir_metadata_preserve(impl, nir_metadata_block_index | nir_metadata_dominance); } else { -#ifndef NDEBUG - impl->valid_metadata &= ~nir_metadata_not_properly_reset; -#endif + nir_metadata_preserve(impl, nir_metadata_all); } return progress;