nir/load_libclc: run some opt passes for everybody
authorKarol Herbst <kherbst@redhat.com>
Sat, 16 Apr 2022 22:06:54 +0000 (00:06 +0200)
committerMarge Bot <emma+marge@anholt.net>
Thu, 22 Jun 2023 21:02:57 +0000 (21:02 +0000)
Cuts down serialized size from 2850288 to 1377780 bytes.

Reduces clinfo with Rusticl time by 40% for debug builds.

(Old data, but the point stands)

Signed-off-by: Karol Herbst <kherbst@redhat.com>
Reviewed-by: Faith Ekstrand <faith.ekstrand@collabora.com>
Reviewed-by: Alyssa Rosenzweig <alyssa@rosenzweig.io>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/15996>

src/compiler/clc/clc.c
src/compiler/clc/nir_clc_helpers.h
src/compiler/clc/nir_load_libclc.c
src/gallium/frontends/clover/nir/invocation.cpp
src/gallium/frontends/rusticl/mesa/compiler/clc/spirv.rs
src/intel/compiler/brw_kernel.c

index 29d207d..4d2c39e 100644 (file)
@@ -88,31 +88,6 @@ clc_print_kernels_info(const struct clc_parsed_spirv *obj)
    }
 }
 
-static void
-clc_libclc_optimize(nir_shader *s)
-{
-   bool progress;
-   do {
-      progress = false;
-      NIR_PASS(progress, s, nir_split_var_copies);
-      NIR_PASS(progress, s, nir_opt_copy_prop_vars);
-      NIR_PASS(progress, s, nir_lower_var_copies);
-      NIR_PASS(progress, s, nir_lower_vars_to_ssa);
-      NIR_PASS(progress, s, nir_copy_prop);
-      NIR_PASS(progress, s, nir_opt_remove_phis);
-      NIR_PASS(progress, s, nir_opt_dce);
-      NIR_PASS(progress, s, nir_opt_if, nir_opt_if_aggressive_last_continue | nir_opt_if_optimize_phi_true_false);
-      NIR_PASS(progress, s, nir_opt_dead_cf);
-      NIR_PASS(progress, s, nir_opt_cse);
-      NIR_PASS(progress, s, nir_opt_peephole_select, 8, true, true);
-      NIR_PASS(progress, s, nir_opt_algebraic);
-      NIR_PASS(progress, s, nir_opt_constant_folding);
-      NIR_PASS(progress, s, nir_opt_undef);
-      NIR_PASS(progress, s, nir_lower_undef_to_zero);
-      NIR_PASS(progress, s, nir_opt_deref);
-   } while (progress);
-}
-
 struct clc_libclc {
    const nir_shader *libclc_nir;
 };
@@ -145,16 +120,15 @@ clc_libclc_new(const struct clc_logger *logger, const struct clc_libclc_options
    };
 
    glsl_type_singleton_init_or_ref();
-   nir_shader *s = nir_load_libclc_shader(64, NULL, &libclc_spirv_options, options->nir_options);
+   bool optimize = options && options->optimize;
+   nir_shader *s =
+      nir_load_libclc_shader(64, NULL, &libclc_spirv_options, options->nir_options, optimize);
    if (!s) {
       clc_error(logger, "D3D12: spirv_to_nir failed on libclc blob");
       ralloc_free(ctx);
       return NULL;
    }
 
-   if (options && options->optimize)
-      clc_libclc_optimize(s);
-
    ralloc_steal(ctx, s);
    ctx->libclc_nir = s;
 
index 02a69ae..9224560 100644 (file)
@@ -21,7 +21,8 @@ nir_shader *
 nir_load_libclc_shader(unsigned ptr_bit_size,
                        struct disk_cache *disk_cache,
                        const struct spirv_to_nir_options *spirv_options,
-                       const nir_shader_compiler_options *nir_options);
+                       const nir_shader_compiler_options *nir_options,
+                       bool optimize);
 
 bool nir_lower_libclc(nir_shader *shader, const nir_shader *clc_shader);
 
index c146931..4472f74 100644 (file)
@@ -304,7 +304,8 @@ nir_shader *
 nir_load_libclc_shader(unsigned ptr_bit_size,
                        struct disk_cache *disk_cache,
                        const struct spirv_to_nir_options *spirv_options,
-                       const nir_shader_compiler_options *nir_options)
+                       const nir_shader_compiler_options *nir_options,
+                       bool optimize)
 {
    assert(ptr_bit_size ==
           nir_address_format_bit_size(spirv_options->global_addr_format));
@@ -356,9 +357,36 @@ nir_load_libclc_shader(unsigned ptr_bit_size,
 
    NIR_PASS_V(nir, libclc_add_generic_variants);
 
-   /* TODO: One day, we may want to run some optimizations on the libclc
-    * shader once and cache them to save time in each shader call.
+   /* Run some optimization passes. Those used here should be considered safe
+    * for all use cases and drivers.
     */
+   if (optimize) {
+      NIR_PASS_V(nir, nir_split_var_copies);
+
+      bool progress;
+      do {
+         progress = false;
+         NIR_PASS(progress, nir, nir_opt_copy_prop_vars);
+         NIR_PASS(progress, nir, nir_lower_var_copies);
+         NIR_PASS(progress, nir, nir_lower_vars_to_ssa);
+         NIR_PASS(progress, nir, nir_copy_prop);
+         NIR_PASS(progress, nir, nir_opt_remove_phis);
+         NIR_PASS(progress, nir, nir_opt_dce);
+         NIR_PASS(progress, nir, nir_opt_if, false);
+         NIR_PASS(progress, nir, nir_opt_dead_cf);
+         NIR_PASS(progress, nir, nir_opt_cse);
+         /* drivers run this pass, so don't be too aggressive. More aggressive
+          * values only increase effectiveness by <5%
+          */
+         NIR_PASS(progress, nir, nir_opt_peephole_select, 0, false, false);
+         NIR_PASS(progress, nir, nir_opt_algebraic);
+         NIR_PASS(progress, nir, nir_opt_constant_folding);
+         NIR_PASS(progress, nir, nir_opt_undef);
+         NIR_PASS(progress, nir, nir_opt_deref);
+      } while(progress);
+
+      nir_sweep(nir);
+   }
 
 #ifdef ENABLE_SHADER_CACHE
    if (disk_cache) {
index 9b62529..f3bcc98 100644 (file)
@@ -257,7 +257,8 @@ nir_shader *clover::nir::load_libclc_nir(const device &dev, std::string &r_log)
    auto *compiler_options = dev_get_nir_compiler_options(dev);
 
    return nir_load_libclc_shader(dev.address_bits(), dev.clc_cache,
-                                &spirv_options, compiler_options);
+                                 &spirv_options, compiler_options,
+                                 dev.clc_cache != nullptr);
 }
 
 static bool
index 802d712..43656dc 100644 (file)
@@ -379,7 +379,13 @@ impl SPIRVBin {
         let shader_cache = DiskCacheBorrowed::as_ptr(&screen.shader_cache());
 
         NirShader::new(unsafe {
-            nir_load_libclc_shader(address_bits, shader_cache, &spirv_options, nir_options)
+            nir_load_libclc_shader(
+                address_bits,
+                shader_cache,
+                &spirv_options,
+                nir_options,
+                true,
+            )
         })
     }
 
index 3945ad3..13e8bee 100644 (file)
@@ -39,7 +39,8 @@ load_clc_shader(struct brw_compiler *compiler, struct disk_cache *disk_cache,
       return compiler->clc_shader;
 
    nir_shader *nir =  nir_load_libclc_shader(64, disk_cache,
-                                             spirv_options, nir_options);
+                                             spirv_options, nir_options,
+                                             disk_cache != NULL);
    if (nir == NULL)
       return NULL;