nir/opt_barrier: Generalize to control barriers
authorAlyssa Rosenzweig <alyssa@rosenzweig.io>
Thu, 2 Mar 2023 18:30:47 +0000 (13:30 -0500)
committerMarge Bot <emma+marge@anholt.net>
Mon, 6 Mar 2023 22:09:27 +0000 (22:09 +0000)
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 <alyssa@rosenzweig.io>
Reviewed-by: Caio Oliveira <caio.oliveira@intel.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/21661>

src/compiler/nir/nir.h
src/compiler/nir/nir_opt_barriers.c
src/intel/compiler/brw_nir.c

index 143aaf4..37f8d68 100644 (file)
@@ -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);
 
index 18c3d79..61dcdbd 100644 (file)
@@ -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;
       }
    }
index c903ccc..aed4655 100644 (file)
@@ -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;