From 952bd63d6d8289425c78c103d65bf3a4882b4b4e Mon Sep 17 00:00:00 2001 From: Alyssa Rosenzweig Date: Thu, 2 Mar 2023 13:30:47 -0500 Subject: [PATCH] nir/opt_barrier: Generalize to control barriers For GLSL, we want to optimize code like memoryBarrierBuffer(); controlBarrier(); into a single scoped_barrier intrinsic for the backend to consume. Now that backends can get scoped_barriers everywhere, what's left is enabling backends to combine these barriers together. We already have an Intel-specific pass for combining memory barriers; it just needs a teensy bit of generalization to allow combining all sorts of barriers together. This avoids code quality regression on Asahi when switching to purely scoped barriers. It's probably useful for other backends too. Signed-off-by: Alyssa Rosenzweig Reviewed-by: Caio Oliveira Part-of: --- src/compiler/nir/nir.h | 8 ++++---- src/compiler/nir/nir_opt_barriers.c | 15 +++++++-------- src/intel/compiler/brw_nir.c | 13 +++++++++---- 3 files changed, 20 insertions(+), 16 deletions(-) diff --git a/src/compiler/nir/nir.h b/src/compiler/nir/nir.h index 143aaf41724..37f8d68d45a 100644 --- a/src/compiler/nir/nir.h +++ b/src/compiler/nir/nir.h @@ -5720,12 +5720,12 @@ bool nir_opt_constant_folding(nir_shader *shader); * which will result in b being removed by the pass. Return false if * combination wasn't possible. */ -typedef bool (*nir_combine_memory_barrier_cb)( +typedef bool (*nir_combine_barrier_cb)( nir_intrinsic_instr *a, nir_intrinsic_instr *b, void *data); -bool nir_opt_combine_memory_barriers(nir_shader *shader, - nir_combine_memory_barrier_cb combine_cb, - void *data); +bool nir_opt_combine_barriers(nir_shader *shader, + nir_combine_barrier_cb combine_cb, + void *data); bool nir_opt_combine_stores(nir_shader *shader, nir_variable_mode modes); diff --git a/src/compiler/nir/nir_opt_barriers.c b/src/compiler/nir/nir_opt_barriers.c index 18c3d790eb9..61dcdbdf3ef 100644 --- a/src/compiler/nir/nir_opt_barriers.c +++ b/src/compiler/nir/nir_opt_barriers.c @@ -24,8 +24,8 @@ #include "nir.h" static bool -nir_opt_combine_memory_barriers_impl( - nir_function_impl *impl, nir_combine_memory_barrier_cb combine_cb, void *data) +nir_opt_combine_barriers_impl( + nir_function_impl *impl, nir_combine_barrier_cb combine_cb, void *data) { bool progress = false; @@ -39,8 +39,7 @@ nir_opt_combine_memory_barriers_impl( } nir_intrinsic_instr *current = nir_instr_as_intrinsic(instr); - if (current->intrinsic != nir_intrinsic_scoped_barrier || - nir_intrinsic_execution_scope(current) != NIR_SCOPE_NONE) { + if (current->intrinsic != nir_intrinsic_scoped_barrier) { prev = NULL; continue; } @@ -65,10 +64,10 @@ nir_opt_combine_memory_barriers_impl( return progress; } -/* Combine adjacent scoped memory barriers. */ +/* Combine adjacent scoped barriers. */ bool -nir_opt_combine_memory_barriers( - nir_shader *shader, nir_combine_memory_barrier_cb combine_cb, void *data) +nir_opt_combine_barriers( + nir_shader *shader, nir_combine_barrier_cb combine_cb, void *data) { assert(combine_cb); @@ -76,7 +75,7 @@ nir_opt_combine_memory_barriers( nir_foreach_function(function, shader) { if (function->impl && - nir_opt_combine_memory_barriers_impl(function->impl, combine_cb, data)) { + nir_opt_combine_barriers_impl(function->impl, combine_cb, data)) { progress = true; } } diff --git a/src/intel/compiler/brw_nir.c b/src/intel/compiler/brw_nir.c index c903cccb09d..aed46553f95 100644 --- a/src/intel/compiler/brw_nir.c +++ b/src/intel/compiler/brw_nir.c @@ -1243,10 +1243,15 @@ brw_nir_should_vectorize_mem(unsigned align_mul, unsigned align_offset, } static -bool combine_all_barriers(nir_intrinsic_instr *a, - nir_intrinsic_instr *b, - void *data) +bool combine_all_memory_barriers(nir_intrinsic_instr *a, + nir_intrinsic_instr *b, + void *data) { + /* Only combine pure memory barriers */ + if ((nir_intrinsic_execution_scope(a) != NIR_SCOPE_NONE) || + (nir_intrinsic_execution_scope(b) != NIR_SCOPE_NONE)) + return false; + /* Translation to backend IR will get rid of modes we don't care about, so * no harm in always combining them. * @@ -1418,7 +1423,7 @@ brw_postprocess_nir(nir_shader *nir, const struct brw_compiler *compiler, OPT(nir_lower_bit_size, lower_bit_size_callback, (void *)compiler); OPT(brw_nir_lower_scoped_barriers); - OPT(nir_opt_combine_memory_barriers, combine_all_barriers, NULL); + OPT(nir_opt_combine_barriers, combine_all_memory_barriers, NULL); do { progress = false;