X-Git-Url: https://git.libre-soc.org/?a=blobdiff_plain;f=src%2Fcompiler%2Fnir%2Fnir_deref.c;h=f7a07c6b465d9a66a4d3d1a844b385cfb16f68c8;hb=84b3ef6a96eabc28b18e8cdf1b0d61826b1a8a67;hp=13aa10c753249c304ad5892aacae3bc564b13ac4;hpb=36734987a59f087ae499c5d0736995007f4af780;p=mesa.git diff --git a/src/compiler/nir/nir_deref.c b/src/compiler/nir/nir_deref.c index 13aa10c7532..f7a07c6b465 100644 --- a/src/compiler/nir/nir_deref.c +++ b/src/compiler/nir/nir_deref.c @@ -58,7 +58,7 @@ nir_deref_path_init(nir_deref_path *path, #ifndef NDEBUG /* Just in case someone uses short_path by accident */ for (unsigned i = 0; i < ARRAY_SIZE(path->_short_path); i++) - path->_short_path[i] = (void *)0xdeadbeef; + path->_short_path[i] = (void *)(uintptr_t)0xdeadbeef; #endif path->path = ralloc_array(mem_ctx, nir_deref_instr *, count + 1); @@ -121,20 +121,110 @@ nir_deref_instr_has_indirect(nir_deref_instr *instr) return false; } +bool +nir_deref_instr_is_known_out_of_bounds(nir_deref_instr *instr) +{ + for (; instr; instr = nir_deref_instr_parent(instr)) { + if (instr->deref_type == nir_deref_type_array && + nir_src_is_const(instr->arr.index) && + nir_src_as_uint(instr->arr.index) >= + glsl_get_length(nir_deref_instr_parent(instr)->type)) + return true; + } + + return false; +} + +bool +nir_deref_instr_has_complex_use(nir_deref_instr *deref) +{ + nir_foreach_use(use_src, &deref->dest.ssa) { + nir_instr *use_instr = use_src->parent_instr; + + switch (use_instr->type) { + case nir_instr_type_deref: { + nir_deref_instr *use_deref = nir_instr_as_deref(use_instr); + + /* A var deref has no sources */ + assert(use_deref->deref_type != nir_deref_type_var); + + /* If a deref shows up in an array index or something like that, it's + * a complex use. + */ + if (use_src != &use_deref->parent) + return true; + + /* Anything that isn't a basic struct or array deref is considered to + * be a "complex" use. In particular, we don't allow ptr_as_array + * because we assume that opt_deref will turn any non-complex + * ptr_as_array derefs into regular array derefs eventually so passes + * which only want to handle simple derefs will pick them up in a + * later pass. + */ + if (use_deref->deref_type != nir_deref_type_struct && + use_deref->deref_type != nir_deref_type_array_wildcard && + use_deref->deref_type != nir_deref_type_array) + return true; + + if (nir_deref_instr_has_complex_use(use_deref)) + return true; + + continue; + } + + case nir_instr_type_intrinsic: { + nir_intrinsic_instr *use_intrin = nir_instr_as_intrinsic(use_instr); + switch (use_intrin->intrinsic) { + case nir_intrinsic_load_deref: + assert(use_src == &use_intrin->src[0]); + continue; + + case nir_intrinsic_copy_deref: + assert(use_src == &use_intrin->src[0] || + use_src == &use_intrin->src[1]); + continue; + + case nir_intrinsic_store_deref: + /* A use in src[1] of a store means we're taking that pointer and + * writing it to a variable. Because we have no idea who will + * read that variable and what they will do with the pointer, it's + * considered a "complex" use. A use in src[0], on the other + * hand, is a simple use because we're just going to dereference + * it and write a value there. + */ + if (use_src == &use_intrin->src[0]) + continue; + return true; + + default: + return true; + } + unreachable("Switch default failed"); + } + + default: + return true; + } + } + + nir_foreach_if_use(use, &deref->dest.ssa) + return true; + + return false; +} + 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; } } @@ -152,7 +242,7 @@ struct_type_get_field_offset(const struct glsl_type *struct_type, glsl_type_size_align_func size_align, unsigned field_idx) { - assert(glsl_type_is_struct(struct_type)); + assert(glsl_type_is_struct_or_ifc(struct_type)); unsigned offset = 0; for (unsigned i = 0; i <= field_idx; i++) { unsigned elem_size, elem_align; @@ -206,16 +296,15 @@ nir_build_deref_offset(nir_builder *b, nir_deref_instr *deref, for (nir_deref_instr **p = &path.path[1]; *p; p++) { if ((*p)->deref_type == nir_deref_type_array) { nir_ssa_def *index = nir_ssa_for_src(b, (*p)->arr.index, 1); - nir_ssa_def *stride = - nir_imm_int(b, type_get_array_stride((*p)->type, size_align)); - offset = nir_iadd(b, offset, nir_imul(b, index, stride)); + 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) { /* 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); - nir_iadd(b, offset, nir_imm_int(b, field_offset)); + offset = nir_iadd_imm(b, offset, field_offset); } else { unreachable("Unsupported deref type"); } @@ -307,6 +396,28 @@ modes_may_alias(nir_variable_mode a, nir_variable_mode b) return a == b; } +static bool +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) + return true; + + for (nir_deref_instr **p = &path->path[1]; *p; p++) { + if ((*p)->deref_type != nir_deref_type_struct) + continue; + + const struct glsl_type *struct_type = (*(p - 1))->type; + const struct glsl_struct_field *field = + glsl_get_struct_field_data(struct_type, (*p)->strct.index); + if (field->memory_coherent) + return true; + } + + return false; +} + nir_deref_compare_result nir_compare_deref_paths(nir_deref_path *a_path, nir_deref_path *b_path) @@ -318,11 +429,32 @@ nir_compare_deref_paths(nir_deref_path *a_path, return nir_derefs_may_alias_bit; if (a_path->path[0]->deref_type == nir_deref_type_var) { - /* If we can chase the deref all the way back to the variable and - * they're not the same variable, we know they can't possibly alias. - */ - if (a_path->path[0]->var != b_path->path[0]->var) + if (a_path->path[0]->var != b_path->path[0]->var) { + /* Shader and function temporaries aren't backed by memory so two + * distinct variables never alias. + */ + static const nir_variable_mode temp_var_modes = + nir_var_shader_temp | nir_var_function_temp; + if ((a_path->path[0]->mode & temp_var_modes) || + (b_path->path[0]->mode & temp_var_modes)) + return nir_derefs_do_not_alias; + + /* If they are both declared coherent or have coherent somewhere in + * their path (due to a member of an interface being declared + * coherent), we have to assume we that we could have any kind of + * aliasing. Otherwise, they could still alias but the client didn't + * tell us and that's their fault. + */ + if (deref_path_contains_coherent_decoration(a_path) && + deref_path_contains_coherent_decoration(b_path)) + return nir_derefs_may_alias_bit; + + /* If we can chase the deref all the way back to the variable and + * they're not the same variable and at least one is not declared + * coherent, we know they can't possibly alias. + */ return nir_derefs_do_not_alias; + } } else { assert(a_path->path[0]->deref_type == nir_deref_type_cast); /* If they're not exactly the same cast, it's hard to compare them so we @@ -559,6 +691,8 @@ rematerialize_deref_src(nir_src *src, void *_state) * used. After this pass has been run, every use of a deref will be of a * deref in the same block as the use. Also, all unused derefs will be * deleted as a side-effect. + * + * Derefs used as sources of phi instructions are not rematerialized. */ bool nir_rematerialize_derefs_in_use_blocks_impl(nir_function_impl *impl) @@ -574,10 +708,15 @@ nir_rematerialize_derefs_in_use_blocks_impl(nir_function_impl *impl) _mesa_hash_table_clear(state.cache, NULL); nir_foreach_instr_safe(instr, block) { - if (instr->type == nir_instr_type_deref) { - nir_deref_instr_remove_if_unused(nir_instr_as_deref(instr)); + if (instr->type == nir_instr_type_deref && + nir_deref_instr_remove_if_unused(nir_instr_as_deref(instr))) + continue; + + /* If a deref is used in a phi, we can't rematerialize it, as the new + * derefs would appear before the phi, which is not valid. + */ + if (instr->type == nir_instr_type_phi) continue; - } state.builder.cursor = nir_before_instr(instr); nir_foreach_src(instr, rematerialize_deref_src, &state); @@ -633,18 +772,76 @@ is_deref_ptr_as_array(nir_instr *instr) nir_instr_as_deref(instr)->deref_type == nir_deref_type_ptr_as_array; } +/** + * Remove casts that just wrap other casts. + */ static bool -opt_deref_cast(nir_deref_instr *cast) +opt_remove_cast_cast(nir_deref_instr *cast) { - if (!is_trivial_deref_cast(cast)) + nir_deref_instr *first_cast = cast; + + while (true) { + nir_deref_instr *parent = nir_deref_instr_parent(first_cast); + if (parent == NULL || parent->deref_type != nir_deref_type_cast) + break; + first_cast = parent; + } + if (cast == first_cast) return false; + nir_instr_rewrite_src(&cast->instr, &cast->parent, + nir_src_for_ssa(first_cast->parent.ssa)); + return true; +} + +/** + * Is this casting a struct to a contained struct. + * struct a { struct b field0 }; + * ssa_5 is structa; + * deref_cast (structb *)ssa_5 (function_temp structb); + * converts to + * deref_struct &ssa_5->field0 (function_temp structb); + * This allows subsequent copy propagation to work. + */ +static bool +opt_replace_struct_wrapper_cast(nir_builder *b, nir_deref_instr *cast) +{ + nir_deref_instr *parent = nir_src_as_deref(cast->parent); + if (!parent) + return false; + + if (!glsl_type_is_struct(parent->type)) + return false; + + if (glsl_get_struct_field_offset(parent->type, 0) != 0) + return false; + + if (cast->type != glsl_get_struct_field(parent->type, 0)) + return false; + + nir_deref_instr *replace = nir_build_deref_struct(b, parent, 0); + nir_ssa_def_rewrite_uses(&cast->dest.ssa, nir_src_for_ssa(&replace->dest.ssa)); + nir_deref_instr_remove_if_unused(cast); + return true; +} + +static bool +opt_deref_cast(nir_builder *b, nir_deref_instr *cast) +{ + bool progress; + + if (opt_replace_struct_wrapper_cast(b, cast)) + return true; + + progress = opt_remove_cast_cast(cast); + if (!is_trivial_deref_cast(cast)) + return progress; + bool trivial_array_cast = is_trivial_array_deref_cast(cast); assert(cast->dest.is_ssa); assert(cast->parent.is_ssa); - bool progress = false; nir_foreach_use_safe(use_src, &cast->dest.ssa) { /* If this isn't a trivial array cast, we can't propagate into * ptr_as_array derefs. @@ -709,7 +906,7 @@ opt_deref_ptr_as_array(nir_builder *b, nir_deref_instr *deref) return true; } -static bool +bool nir_opt_deref_impl(nir_function_impl *impl) { bool progress = false; @@ -732,7 +929,7 @@ nir_opt_deref_impl(nir_function_impl *impl) break; case nir_deref_type_cast: - if (opt_deref_cast(deref)) + if (opt_deref_cast(&b, deref)) progress = true; break;