17ec681f3Smrg/* 27ec681f3Smrg * Copyright © 2020 Intel Corporation 37ec681f3Smrg * 47ec681f3Smrg * Permission is hereby granted, free of charge, to any person obtaining a 57ec681f3Smrg * copy of this software and associated documentation files (the "Software"), 67ec681f3Smrg * to deal in the Software without restriction, including without limitation 77ec681f3Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense, 87ec681f3Smrg * and/or sell copies of the Software, and to permit persons to whom the 97ec681f3Smrg * Software is furnished to do so, subject to the following conditions: 107ec681f3Smrg * 117ec681f3Smrg * The above copyright notice and this permission notice (including the next 127ec681f3Smrg * paragraph) shall be included in all copies or substantial portions of the 137ec681f3Smrg * Software. 147ec681f3Smrg * 157ec681f3Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 167ec681f3Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 177ec681f3Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 187ec681f3Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 197ec681f3Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 207ec681f3Smrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 217ec681f3Smrg * IN THE SOFTWARE. 227ec681f3Smrg */ 237ec681f3Smrg 247ec681f3Smrg#include "nir_builder.h" 257ec681f3Smrg 267ec681f3Smrgstatic bool 277ec681f3Smrgopt_memcpy_deref_cast(nir_intrinsic_instr *cpy, nir_src *deref_src) 287ec681f3Smrg{ 297ec681f3Smrg assert(cpy->intrinsic == nir_intrinsic_memcpy_deref); 307ec681f3Smrg 317ec681f3Smrg nir_deref_instr *cast = nir_src_as_deref(*deref_src); 327ec681f3Smrg if (cast == NULL || cast->deref_type != nir_deref_type_cast) 337ec681f3Smrg return false; 347ec681f3Smrg 357ec681f3Smrg /* We always have to replace the source with a deref, not a bare uint 367ec681f3Smrg * pointer. If it's the first deref in the chain, bail. 377ec681f3Smrg */ 387ec681f3Smrg nir_deref_instr *parent = nir_src_as_deref(cast->parent); 397ec681f3Smrg if (parent == NULL) 407ec681f3Smrg return false; 417ec681f3Smrg 427ec681f3Smrg /* If it has useful alignment information, we want to keep that */ 437ec681f3Smrg if (cast->cast.align_mul > 0) 447ec681f3Smrg return false; 457ec681f3Smrg 467ec681f3Smrg /* Casts to uint8 or int8 never do us any good; get rid of them */ 477ec681f3Smrg if (cast->type == glsl_int8_t_type() || 487ec681f3Smrg cast->type == glsl_uint8_t_type()) { 497ec681f3Smrg nir_instr_rewrite_src(&cpy->instr, deref_src, 507ec681f3Smrg nir_src_for_ssa(&parent->dest.ssa)); 517ec681f3Smrg return true; 527ec681f3Smrg } 537ec681f3Smrg 547ec681f3Smrg int64_t parent_type_size = glsl_get_explicit_size(parent->type, false); 557ec681f3Smrg if (parent_type_size < 0) 567ec681f3Smrg return false; 577ec681f3Smrg 587ec681f3Smrg if (!nir_src_is_const(cpy->src[2])) 597ec681f3Smrg return false; 607ec681f3Smrg 617ec681f3Smrg /* We don't want to get rid of the cast if the resulting type would be 627ec681f3Smrg * smaller than the amount of data we're copying. 637ec681f3Smrg */ 647ec681f3Smrg if (nir_src_as_uint(cpy->src[2]) < (uint64_t)parent_type_size) 657ec681f3Smrg return false; 667ec681f3Smrg 677ec681f3Smrg nir_instr_rewrite_src(&cpy->instr, deref_src, 687ec681f3Smrg nir_src_for_ssa(&parent->dest.ssa)); 697ec681f3Smrg return true; 707ec681f3Smrg} 717ec681f3Smrg 727ec681f3Smrgstatic bool 737ec681f3Smrgtype_is_tightly_packed(const struct glsl_type *type, unsigned *size_out) 747ec681f3Smrg{ 757ec681f3Smrg unsigned size = 0; 767ec681f3Smrg if (glsl_type_is_struct_or_ifc(type)) { 777ec681f3Smrg unsigned num_fields = glsl_get_length(type); 787ec681f3Smrg for (unsigned i = 0; i < num_fields; i++) { 797ec681f3Smrg const struct glsl_struct_field *field = 807ec681f3Smrg glsl_get_struct_field_data(type, i); 817ec681f3Smrg 827ec681f3Smrg if (field->offset < 0 || field->offset != size) 837ec681f3Smrg return false; 847ec681f3Smrg 857ec681f3Smrg unsigned field_size; 867ec681f3Smrg if (!type_is_tightly_packed(field->type, &field_size)) 877ec681f3Smrg return false; 887ec681f3Smrg 897ec681f3Smrg size = field->offset + field_size; 907ec681f3Smrg } 917ec681f3Smrg } else if (glsl_type_is_array_or_matrix(type)) { 927ec681f3Smrg if (glsl_type_is_unsized_array(type)) 937ec681f3Smrg return false; 947ec681f3Smrg 957ec681f3Smrg unsigned stride = glsl_get_explicit_stride(type); 967ec681f3Smrg if (stride == 0) 977ec681f3Smrg return false; 987ec681f3Smrg 997ec681f3Smrg const struct glsl_type *elem_type = glsl_get_array_element(type); 1007ec681f3Smrg 1017ec681f3Smrg unsigned elem_size; 1027ec681f3Smrg if (!type_is_tightly_packed(elem_type, &elem_size)) 1037ec681f3Smrg return false; 1047ec681f3Smrg 1057ec681f3Smrg if (elem_size != stride) 1067ec681f3Smrg return false; 1077ec681f3Smrg 1087ec681f3Smrg size = stride * glsl_get_length(type); 1097ec681f3Smrg } else { 1107ec681f3Smrg assert(glsl_type_is_vector_or_scalar(type)); 1117ec681f3Smrg if (glsl_get_explicit_stride(type) > 0) 1127ec681f3Smrg return false; 1137ec681f3Smrg 1147ec681f3Smrg if (glsl_type_is_boolean(type)) 1157ec681f3Smrg return false; 1167ec681f3Smrg 1177ec681f3Smrg size = glsl_get_explicit_size(type, false); 1187ec681f3Smrg } 1197ec681f3Smrg 1207ec681f3Smrg if (size_out) 1217ec681f3Smrg *size_out = size; 1227ec681f3Smrg return true; 1237ec681f3Smrg} 1247ec681f3Smrg 1257ec681f3Smrgstatic bool 1267ec681f3Smrgtry_lower_memcpy(nir_builder *b, nir_intrinsic_instr *cpy) 1277ec681f3Smrg{ 1287ec681f3Smrg nir_deref_instr *dst = nir_src_as_deref(cpy->src[0]); 1297ec681f3Smrg nir_deref_instr *src = nir_src_as_deref(cpy->src[1]); 1307ec681f3Smrg 1317ec681f3Smrg /* A self-copy can always be eliminated */ 1327ec681f3Smrg if (dst == src) { 1337ec681f3Smrg nir_instr_remove(&cpy->instr); 1347ec681f3Smrg return true; 1357ec681f3Smrg } 1367ec681f3Smrg 1377ec681f3Smrg if (!nir_src_is_const(cpy->src[2])) 1387ec681f3Smrg return false; 1397ec681f3Smrg 1407ec681f3Smrg uint64_t size = nir_src_as_uint(cpy->src[2]); 1417ec681f3Smrg if (size == 0) { 1427ec681f3Smrg nir_instr_remove(&cpy->instr); 1437ec681f3Smrg return true; 1447ec681f3Smrg } 1457ec681f3Smrg 1467ec681f3Smrg if (glsl_type_is_vector_or_scalar(src->type) && 1477ec681f3Smrg glsl_type_is_vector_or_scalar(dst->type) && 1487ec681f3Smrg glsl_get_explicit_size(dst->type, false) == size && 1497ec681f3Smrg glsl_get_explicit_size(src->type, false) == size) { 1507ec681f3Smrg b->cursor = nir_instr_remove(&cpy->instr); 1517ec681f3Smrg nir_ssa_def *data = 1527ec681f3Smrg nir_load_deref_with_access(b, src, nir_intrinsic_src_access(cpy)); 1537ec681f3Smrg data = nir_bitcast_vector(b, data, glsl_get_bit_size(dst->type)); 1547ec681f3Smrg assert(data->num_components == glsl_get_vector_elements(dst->type)); 1557ec681f3Smrg nir_store_deref_with_access(b, dst, data, ~0 /* write mask */, 1567ec681f3Smrg nir_intrinsic_dst_access(cpy)); 1577ec681f3Smrg return true; 1587ec681f3Smrg } 1597ec681f3Smrg 1607ec681f3Smrg unsigned type_size; 1617ec681f3Smrg if (dst->type == src->type && 1627ec681f3Smrg type_is_tightly_packed(dst->type, &type_size) && 1637ec681f3Smrg type_size == size) { 1647ec681f3Smrg b->cursor = nir_instr_remove(&cpy->instr); 1657ec681f3Smrg nir_copy_deref_with_access(b, dst, src, 1667ec681f3Smrg nir_intrinsic_dst_access(cpy), 1677ec681f3Smrg nir_intrinsic_src_access(cpy)); 1687ec681f3Smrg return true; 1697ec681f3Smrg } 1707ec681f3Smrg 1717ec681f3Smrg return false; 1727ec681f3Smrg} 1737ec681f3Smrg 1747ec681f3Smrgstatic bool 1757ec681f3Smrgopt_memcpy_impl(nir_function_impl *impl) 1767ec681f3Smrg{ 1777ec681f3Smrg bool progress = false; 1787ec681f3Smrg 1797ec681f3Smrg nir_builder b; 1807ec681f3Smrg nir_builder_init(&b, impl); 1817ec681f3Smrg 1827ec681f3Smrg nir_foreach_block(block, impl) { 1837ec681f3Smrg nir_foreach_instr_safe(instr, block) { 1847ec681f3Smrg if (instr->type != nir_instr_type_intrinsic) 1857ec681f3Smrg continue; 1867ec681f3Smrg 1877ec681f3Smrg nir_intrinsic_instr *cpy = nir_instr_as_intrinsic(instr); 1887ec681f3Smrg if (cpy->intrinsic != nir_intrinsic_memcpy_deref) 1897ec681f3Smrg continue; 1907ec681f3Smrg 1917ec681f3Smrg while (opt_memcpy_deref_cast(cpy, &cpy->src[0])) 1927ec681f3Smrg progress = true; 1937ec681f3Smrg while (opt_memcpy_deref_cast(cpy, &cpy->src[1])) 1947ec681f3Smrg progress = true; 1957ec681f3Smrg 1967ec681f3Smrg if (try_lower_memcpy(&b, cpy)) { 1977ec681f3Smrg progress = true; 1987ec681f3Smrg continue; 1997ec681f3Smrg } 2007ec681f3Smrg } 2017ec681f3Smrg } 2027ec681f3Smrg 2037ec681f3Smrg if (progress) { 2047ec681f3Smrg nir_metadata_preserve(impl, nir_metadata_block_index | 2057ec681f3Smrg nir_metadata_dominance); 2067ec681f3Smrg } else { 2077ec681f3Smrg nir_metadata_preserve(impl, nir_metadata_all); 2087ec681f3Smrg } 2097ec681f3Smrg 2107ec681f3Smrg return progress; 2117ec681f3Smrg} 2127ec681f3Smrg 2137ec681f3Smrgbool 2147ec681f3Smrgnir_opt_memcpy(nir_shader *shader) 2157ec681f3Smrg{ 2167ec681f3Smrg bool progress = false; 2177ec681f3Smrg 2187ec681f3Smrg nir_foreach_function(function, shader) { 2197ec681f3Smrg if (function->impl && opt_memcpy_impl(function->impl)) 2207ec681f3Smrg progress = true; 2217ec681f3Smrg } 2227ec681f3Smrg 2237ec681f3Smrg return progress; 2247ec681f3Smrg} 225