mesa/src/compiler/nir/nir_lower_memcpy.c

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

194 lines
6.7 KiB
C
Raw Normal View History

/*
* Copyright © 2020 Intel Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#include "nir_builder.h"
#include <string.h>
/** Returns the type to use for a copy of the given size.
*
* The actual type doesn't matter here all that much as we're just going to do
* a load/store on it and never any arithmetic.
*/
static const struct glsl_type *
copy_type_for_byte_size(unsigned size)
{
switch (size) {
case 1:
return glsl_vector_type(GLSL_TYPE_UINT8, 1);
case 2:
return glsl_vector_type(GLSL_TYPE_UINT16, 1);
case 4:
return glsl_vector_type(GLSL_TYPE_UINT, 1);
case 8:
return glsl_vector_type(GLSL_TYPE_UINT, 2);
case 16:
return glsl_vector_type(GLSL_TYPE_UINT, 4);
default:
build: avoid redefining unreachable() which is standard in C23 In the C23 standard unreachable() is now a predefined function-like macro in <stddef.h> See https://android.googlesource.com/platform/bionic/+/HEAD/docs/c23.md#is-now-a-predefined-function_like-macro-in And this causes build errors when building for C23: ----------------------------------------------------------------------- In file included from ../src/util/log.h:30, from ../src/util/log.c:30: ../src/util/macros.h:123:9: warning: "unreachable" redefined 123 | #define unreachable(str) \ | ^~~~~~~~~~~ In file included from ../src/util/macros.h:31: /usr/lib/gcc/x86_64-linux-gnu/14/include/stddef.h:456:9: note: this is the location of the previous definition 456 | #define unreachable() (__builtin_unreachable ()) | ^~~~~~~~~~~ ----------------------------------------------------------------------- So don't redefine it with the same name, but use the name UNREACHABLE() to also signify it's a macro. Using a different name also makes sense because the behavior of the macro was extending the one of __builtin_unreachable() anyway, and it also had a different signature, accepting one argument, compared to the standard unreachable() with no arguments. This change improves the chances of building mesa with the C23 standard, which for instance is the default in recent AOSP versions. All the instances of the macro, including the definition, were updated with the following command line: git grep -l '[^_]unreachable(' -- "src/**" | sort | uniq | \ while read file; \ do \ sed -e 's/\([^_]\)unreachable(/\1UNREACHABLE(/g' -i "$file"; \ done && \ sed -e 's/#undef unreachable/#undef UNREACHABLE/g' -i src/intel/isl/isl_aux_info.c Reviewed-by: Erik Faye-Lund <erik.faye-lund@collabora.com> Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/36437>
2025-07-23 09:17:35 +02:00
UNREACHABLE("Unsupported size");
}
}
static nir_def *
memcpy_load_deref_elem(nir_builder *b, nir_deref_instr *parent,
nir_def *index)
{
nir_deref_instr *deref;
index = nir_i2iN(b, index, parent->def.bit_size);
assert(parent->deref_type == nir_deref_type_cast);
deref = nir_build_deref_ptr_as_array(b, parent, index);
return nir_load_deref(b, deref);
}
static nir_def *
memcpy_load_deref_elem_imm(nir_builder *b, nir_deref_instr *parent,
uint64_t index)
{
nir_def *idx = nir_imm_intN_t(b, index, parent->def.bit_size);
return memcpy_load_deref_elem(b, parent, idx);
}
static void
memcpy_store_deref_elem(nir_builder *b, nir_deref_instr *parent,
nir_def *index, nir_def *value)
{
nir_deref_instr *deref;
index = nir_i2iN(b, index, parent->def.bit_size);
assert(parent->deref_type == nir_deref_type_cast);
deref = nir_build_deref_ptr_as_array(b, parent, index);
nir_store_deref(b, deref, value, ~0);
}
static void
memcpy_store_deref_elem_imm(nir_builder *b, nir_deref_instr *parent,
uint64_t index, nir_def *value)
{
nir_def *idx = nir_imm_intN_t(b, index, parent->def.bit_size);
memcpy_store_deref_elem(b, parent, idx, value);
}
static bool
lower_memcpy_impl(nir_function_impl *impl)
{
nir_builder b = nir_builder_create(impl);
bool found_const_memcpy = false, found_non_const_memcpy = false;
nir_foreach_block_safe(block, impl) {
nir_foreach_instr_safe(instr, block) {
if (instr->type != nir_instr_type_intrinsic)
continue;
nir_intrinsic_instr *cpy = nir_instr_as_intrinsic(instr);
if (cpy->intrinsic != nir_intrinsic_memcpy_deref)
continue;
b.cursor = nir_instr_remove(&cpy->instr);
nir_deref_instr *dst = nir_src_as_deref(cpy->src[0]);
nir_deref_instr *src = nir_src_as_deref(cpy->src[1]);
if (nir_src_is_const(cpy->src[2])) {
found_const_memcpy = true;
uint64_t size = nir_src_as_uint(cpy->src[2]);
uint64_t offset = 0;
while (offset < size) {
uint64_t remaining = size - offset;
/* Find the largest chunk size power-of-two (MSB in remaining)
* and limit our chunk to 16B (a vec4). It's important to do as
* many 16B chunks as possible first so that the index
* computation is correct for
* memcpy_(load|store)_deref_elem_imm.
*/
unsigned copy_size = 1u << MIN2(util_last_bit64(remaining) - 1, 4);
const struct glsl_type *copy_type =
copy_type_for_byte_size(copy_size);
nir_deref_instr *copy_dst =
nir_build_deref_cast(&b, &dst->def, dst->modes,
copy_type, copy_size);
nir_deref_instr *copy_src =
nir_build_deref_cast(&b, &src->def, src->modes,
copy_type, copy_size);
uint64_t index = offset / copy_size;
nir_def *value =
memcpy_load_deref_elem_imm(&b, copy_src, index);
memcpy_store_deref_elem_imm(&b, copy_dst, index, value);
offset += copy_size;
}
} else {
found_non_const_memcpy = true;
nir_def *size = cpy->src[2].ssa;
/* In this case, we don't have any idea what the size is so we
* emit a loop which copies one byte at a time.
*/
nir_deref_instr *copy_dst =
nir_build_deref_cast(&b, &dst->def, dst->modes,
glsl_uint8_t_type(), 1);
nir_deref_instr *copy_src =
nir_build_deref_cast(&b, &src->def, src->modes,
glsl_uint8_t_type(), 1);
nir_variable *i = nir_local_variable_create(impl,
glsl_uintN_t_type(size->bit_size), NULL);
nir_store_var(&b, i, nir_imm_intN_t(&b, 0, size->bit_size), ~0);
nir_push_loop(&b);
{
nir_def *index = nir_load_var(&b, i);
nir_break_if(&b, nir_uge(&b, index, size));
nir_def *value =
memcpy_load_deref_elem(&b, copy_src, index);
memcpy_store_deref_elem(&b, copy_dst, index, value);
nir_store_var(&b, i, nir_iadd_imm(&b, index, 1), ~0);
}
nir_pop_loop(&b, NULL);
}
}
}
if (found_non_const_memcpy) {
treewide: Switch to nir_progress Via the Coccinelle patch at the end of the commit message, followed by sed -ie 's/progress = progress | /progress |=/g' $(git grep -l 'progress = prog') ninja -C ~/mesa/build clang-format cd ~/mesa/src/compiler/nir && clang-format -i *.c agxfmt @@ identifier prog; expression impl, metadata; @@ -if (prog) { -nir_metadata_preserve(impl, metadata); -} else { -nir_metadata_preserve(impl, nir_metadata_all); -} -return prog; +return nir_progress(prog, impl, metadata); @@ expression prog_expr, impl, metadata; @@ -if (prog_expr) { -nir_metadata_preserve(impl, metadata); -return true; -} else { -nir_metadata_preserve(impl, nir_metadata_all); -return false; -} +bool progress = prog_expr; +return nir_progress(progress, impl, metadata); @@ identifier prog; expression impl, metadata; @@ -nir_metadata_preserve(impl, prog ? (metadata) : nir_metadata_all); -return prog; +return nir_progress(prog, impl, metadata); @@ identifier prog; expression impl, metadata; @@ -nir_metadata_preserve(impl, prog ? (metadata) : nir_metadata_all); +nir_progress(prog, impl, metadata); @@ expression impl, metadata; @@ -nir_metadata_preserve(impl, metadata); -return true; +return nir_progress(true, impl, metadata); @@ expression impl; @@ -nir_metadata_preserve(impl, nir_metadata_all); -return false; +return nir_no_progress(impl); @@ identifier other_prog, prog; expression impl, metadata; @@ -if (prog) { -nir_metadata_preserve(impl, metadata); -} else { -nir_metadata_preserve(impl, nir_metadata_all); -} -other_prog |= prog; +other_prog = other_prog | nir_progress(prog, impl, metadata); @@ identifier prog; expression impl, metadata; @@ -if (prog) { -nir_metadata_preserve(impl, metadata); -} else { -nir_metadata_preserve(impl, nir_metadata_all); -} +nir_progress(prog, impl, metadata); @@ identifier other_prog, prog; expression impl, metadata; @@ -if (prog) { -nir_metadata_preserve(impl, metadata); -other_prog = true; -} else { -nir_metadata_preserve(impl, nir_metadata_all); -} +other_prog = other_prog | nir_progress(prog, impl, metadata); @@ expression prog_expr, impl, metadata; identifier prog; @@ -if (prog_expr) { -nir_metadata_preserve(impl, metadata); -prog = true; -} else { -nir_metadata_preserve(impl, nir_metadata_all); -} +bool impl_progress = prog_expr; +prog = prog | nir_progress(impl_progress, impl, metadata); @@ identifier other_prog, prog; expression impl, metadata; @@ -if (prog) { -other_prog = true; -nir_metadata_preserve(impl, metadata); -} else { -nir_metadata_preserve(impl, nir_metadata_all); -} +other_prog = other_prog | nir_progress(prog, impl, metadata); @@ expression prog_expr, impl, metadata; identifier prog; @@ -if (prog_expr) { -prog = true; -nir_metadata_preserve(impl, metadata); -} else { -nir_metadata_preserve(impl, nir_metadata_all); -} +bool impl_progress = prog_expr; +prog = prog | nir_progress(impl_progress, impl, metadata); @@ expression prog_expr, impl, metadata; @@ -if (prog_expr) { -nir_metadata_preserve(impl, metadata); -} else { -nir_metadata_preserve(impl, nir_metadata_all); -} +bool impl_progress = prog_expr; +nir_progress(impl_progress, impl, metadata); @@ identifier prog; expression impl, metadata; @@ -nir_metadata_preserve(impl, metadata); -prog = true; +prog = nir_progress(true, impl, metadata); @@ identifier prog; expression impl, metadata; @@ -if (prog) { -nir_metadata_preserve(impl, metadata); -} -return prog; +return nir_progress(prog, impl, metadata); @@ identifier prog; expression impl, metadata; @@ -if (prog) { -nir_metadata_preserve(impl, metadata); -} +nir_progress(prog, impl, metadata); @@ expression impl; @@ -nir_metadata_preserve(impl, nir_metadata_all); +nir_no_progress(impl); @@ expression impl, metadata; @@ -nir_metadata_preserve(impl, metadata); +nir_progress(true, impl, metadata); squashme! sed -ie 's/progress = progress | /progress |=/g' $(git grep -l 'progress = prog') Signed-off-by: Alyssa Rosenzweig <alyssa@rosenzweig.io> Reviewed-by: Georg Lehmann <dadschoorse@gmail.com> Acked-by: Faith Ekstrand <faith.ekstrand@collabora.com> Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/33722>
2025-02-24 15:10:33 -05:00
nir_progress(true, impl, nir_metadata_none);
} else
nir_progress(found_const_memcpy, impl, nir_metadata_control_flow);
return found_const_memcpy || found_non_const_memcpy;
}
bool
nir_lower_memcpy(nir_shader *shader)
{
bool progress = false;
nir_foreach_function_impl(impl, shader) {
if (lower_memcpy_impl(impl))
progress = true;
}
return progress;
}