mesa/src/compiler/nir/nir_lower_memory_model.c

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

254 lines
8 KiB
C
Raw Normal View History

/*
* Copyright © 2020 Valve 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.
*
*/
/*
* Replaces make availability/visible semantics on barriers with
* ACCESS_COHERENT on memory loads/stores
*/
#include "nir/nir.h"
#include "shader_enums.h"
static bool
get_intrinsic_info(nir_intrinsic_instr *intrin, nir_variable_mode *modes,
bool *reads, bool *writes)
{
switch (intrin->intrinsic) {
case nir_intrinsic_image_deref_load:
case nir_intrinsic_image_deref_sparse_load:
*modes = nir_src_as_deref(intrin->src[0])->modes;
*reads = true;
break;
case nir_intrinsic_image_deref_store:
*modes = nir_src_as_deref(intrin->src[0])->modes;
*writes = true;
break;
case nir_intrinsic_image_deref_atomic:
case nir_intrinsic_image_deref_atomic_swap:
*modes = nir_src_as_deref(intrin->src[0])->modes;
*reads = true;
*writes = true;
break;
case nir_intrinsic_load_ssbo:
*modes = nir_var_mem_ssbo;
*reads = true;
break;
case nir_intrinsic_store_ssbo:
*modes = nir_var_mem_ssbo;
*writes = true;
break;
case nir_intrinsic_ssbo_atomic:
case nir_intrinsic_ssbo_atomic_swap:
*modes = nir_var_mem_ssbo;
*reads = true;
*writes = true;
break;
case nir_intrinsic_load_global:
*modes = nir_var_mem_global;
*reads = true;
break;
case nir_intrinsic_store_global:
*modes = nir_var_mem_global;
*writes = true;
break;
case nir_intrinsic_global_atomic:
case nir_intrinsic_global_atomic_swap:
*modes = nir_var_mem_global;
*reads = true;
*writes = true;
break;
case nir_intrinsic_load_deref:
*modes = nir_src_as_deref(intrin->src[0])->modes;
*reads = true;
break;
case nir_intrinsic_store_deref:
*modes = nir_src_as_deref(intrin->src[0])->modes;
*writes = true;
break;
case nir_intrinsic_deref_atomic:
case nir_intrinsic_deref_atomic_swap:
*modes = nir_src_as_deref(intrin->src[0])->modes;
*reads = true;
*writes = true;
break;
default:
return false;
}
return true;
}
static bool
visit_instr(nir_instr *instr, uint32_t *cur_modes, unsigned vis_avail_sem)
{
if (instr->type != nir_instr_type_intrinsic)
return false;
nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
if (intrin->intrinsic == nir_intrinsic_barrier &&
(nir_intrinsic_memory_semantics(intrin) & vis_avail_sem)) {
*cur_modes |= nir_intrinsic_memory_modes(intrin);
unsigned semantics = nir_intrinsic_memory_semantics(intrin);
nir_intrinsic_set_memory_semantics(
intrin, semantics & ~vis_avail_sem);
if (nir_intrinsic_memory_semantics(intrin) == 0 &&
nir_intrinsic_execution_scope(intrin) <= SCOPE_INVOCATION)
nir_instr_remove(instr);
return true;
}
if (!*cur_modes)
return false; /* early exit */
nir_variable_mode modes;
bool reads = false, writes = false;
if (!get_intrinsic_info(intrin, &modes, &reads, &writes))
return false;
if (!reads && vis_avail_sem == NIR_MEMORY_MAKE_VISIBLE)
return false;
if (!writes && vis_avail_sem == NIR_MEMORY_MAKE_AVAILABLE)
return false;
if (!nir_intrinsic_has_access(intrin))
return false;
unsigned access = nir_intrinsic_access(intrin);
if (access & (ACCESS_NON_READABLE | ACCESS_NON_WRITEABLE | ACCESS_CAN_REORDER | ACCESS_COHERENT))
return false;
if (*cur_modes & modes) {
nir_intrinsic_set_access(intrin, access | ACCESS_COHERENT);
return true;
}
return false;
}
static bool
lower_make_visible(nir_cf_node *cf_node, uint32_t *cur_modes)
{
bool progress = false;
switch (cf_node->type) {
case nir_cf_node_block: {
nir_block *block = nir_cf_node_as_block(cf_node);
nir_foreach_instr_safe(instr, block)
progress |= visit_instr(instr, cur_modes, NIR_MEMORY_MAKE_VISIBLE);
break;
}
case nir_cf_node_if: {
nir_if *nif = nir_cf_node_as_if(cf_node);
uint32_t cur_modes_then = *cur_modes;
uint32_t cur_modes_else = *cur_modes;
foreach_list_typed(nir_cf_node, if_node, node, &nif->then_list)
progress |= lower_make_visible(if_node, &cur_modes_then);
foreach_list_typed(nir_cf_node, if_node, node, &nif->else_list)
progress |= lower_make_visible(if_node, &cur_modes_else);
*cur_modes |= cur_modes_then | cur_modes_else;
break;
}
case nir_cf_node_loop: {
nir_loop *loop = nir_cf_node_as_loop(cf_node);
assert(!nir_loop_has_continue_construct(loop));
bool loop_progress;
do {
loop_progress = false;
foreach_list_typed(nir_cf_node, loop_node, node, &loop->body)
loop_progress |= lower_make_visible(loop_node, cur_modes);
progress |= loop_progress;
} while (loop_progress);
break;
}
case nir_cf_node_function:
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("Invalid cf type");
}
return progress;
}
static bool
lower_make_available(nir_cf_node *cf_node, uint32_t *cur_modes)
{
bool progress = false;
switch (cf_node->type) {
case nir_cf_node_block: {
nir_block *block = nir_cf_node_as_block(cf_node);
nir_foreach_instr_reverse_safe(instr, block)
progress |= visit_instr(instr, cur_modes, NIR_MEMORY_MAKE_AVAILABLE);
break;
}
case nir_cf_node_if: {
nir_if *nif = nir_cf_node_as_if(cf_node);
uint32_t cur_modes_then = *cur_modes;
uint32_t cur_modes_else = *cur_modes;
foreach_list_typed_reverse(nir_cf_node, if_node, node, &nif->then_list)
progress |= lower_make_available(if_node, &cur_modes_then);
foreach_list_typed_reverse(nir_cf_node, if_node, node, &nif->else_list)
progress |= lower_make_available(if_node, &cur_modes_else);
*cur_modes |= cur_modes_then | cur_modes_else;
break;
}
case nir_cf_node_loop: {
nir_loop *loop = nir_cf_node_as_loop(cf_node);
assert(!nir_loop_has_continue_construct(loop));
bool loop_progress;
do {
loop_progress = false;
foreach_list_typed_reverse(nir_cf_node, loop_node, node, &loop->body)
loop_progress |= lower_make_available(loop_node, cur_modes);
progress |= loop_progress;
} while (loop_progress);
break;
}
case nir_cf_node_function:
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("Invalid cf type");
}
return progress;
}
bool
nir_lower_memory_model(nir_shader *shader)
{
bool progress = false;
nir_foreach_function_impl(impl, shader) {
bool impl_progress = false;
struct exec_list *cf_list = &impl->body;
uint32_t modes = 0;
foreach_list_typed(nir_cf_node, cf_node, node, cf_list)
impl_progress |= lower_make_visible(cf_node, &modes);
modes = 0;
foreach_list_typed_reverse(nir_cf_node, cf_node, node, cf_list)
impl_progress |= lower_make_available(cf_node, &modes);
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
progress |= nir_progress(impl_progress, impl,
nir_metadata_control_flow);
}
return progress;
}