From a9d9f9b3c50b1d108dcb19372a57b663592bc85a Mon Sep 17 00:00:00 2001 From: Karol Herbst Date: Sun, 17 Apr 2022 00:06:54 +0200 Subject: [PATCH] nir/load_libclc: run some opt passes for everybody nir_lower_var_copies got dropped as nir_lower_vars_to_ssa already does what it needs. And it had no impact on generated libclc shader beyond that anyway. cuts down serialized size from 2850288 to 1377780 bytes. Reduces clinfo with clover time by 15%/25% for release/debug builds. Signed-off-by: Karol Herbst Reviewed-by: Jason Ekstrand Part-of: --- src/compiler/clc/clc.c | 32 ++---------------- src/compiler/spirv/nir_load_libclc.c | 33 +++++++++++++++++-- src/compiler/spirv/nir_spirv.h | 3 +- .../drivers/llvmpipe/ci/llvmpipe-cl.txt | 2 -- .../frontends/clover/nir/invocation.cpp | 3 +- src/intel/compiler/brw_kernel.c | 3 +- 6 files changed, 39 insertions(+), 37 deletions(-) diff --git a/src/compiler/clc/clc.c b/src/compiler/clc/clc.c index 3d4e9ca857d8..0f9139a3dfa6 100644 --- a/src/compiler/clc/clc.c +++ b/src/compiler/clc/clc.c @@ -87,31 +87,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, true); - 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; }; @@ -144,16 +119,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; diff --git a/src/compiler/spirv/nir_load_libclc.c b/src/compiler/spirv/nir_load_libclc.c index db785b133480..16a19b6b0d38 100644 --- a/src/compiler/spirv/nir_load_libclc.c +++ b/src/compiler/spirv/nir_load_libclc.c @@ -300,7 +300,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)); @@ -352,9 +353,35 @@ 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_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_undef); + NIR_PASS(progress, nir, nir_opt_constant_folding); + NIR_PASS(progress, nir, nir_opt_deref); + } while(progress); + + nir_sweep(nir); + } #ifdef ENABLE_SHADER_CACHE if (disk_cache) { diff --git a/src/compiler/spirv/nir_spirv.h b/src/compiler/spirv/nir_spirv.h index 8899eae76233..3615ee96438d 100644 --- a/src/compiler/spirv/nir_spirv.h +++ b/src/compiler/spirv/nir_spirv.h @@ -126,7 +126,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); diff --git a/src/gallium/drivers/llvmpipe/ci/llvmpipe-cl.txt b/src/gallium/drivers/llvmpipe/ci/llvmpipe-cl.txt index bd4759050c16..9c346ba390c8 100644 --- a/src/gallium/drivers/llvmpipe/ci/llvmpipe-cl.txt +++ b/src/gallium/drivers/llvmpipe/ci/llvmpipe-cl.txt @@ -52,9 +52,7 @@ program/execute/atomic_int64_xor-global: skip program/execute/atomic_int64_xor-global-return: skip program/execute/atomic_int64_xor-local: skip program/execute/builtin/builtin-char-popcount-1.2.generated: skip -program/execute/builtin/builtin-float-cos-1.0.generated: timeout program/execute/builtin/builtin-float-ldexp-1.0.generated: fail -program/execute/builtin/builtin-float-sin-1.0.generated: timeout program/execute/builtin/builtin-float-sincos-1.0.generated: timeout program/execute/builtin/builtin-float-tan-1.0.generated: timeout program/execute/builtin/builtin-int-popcount-1.2.generated: skip diff --git a/src/gallium/frontends/clover/nir/invocation.cpp b/src/gallium/frontends/clover/nir/invocation.cpp index b741a0a184c2..cd2801f1f36b 100644 --- a/src/gallium/frontends/clover/nir/invocation.cpp +++ b/src/gallium/frontends/clover/nir/invocation.cpp @@ -422,7 +422,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 diff --git a/src/intel/compiler/brw_kernel.c b/src/intel/compiler/brw_kernel.c index f3d112e816bd..6d9953b78de4 100644 --- a/src/intel/compiler/brw_kernel.c +++ b/src/intel/compiler/brw_kernel.c @@ -38,7 +38,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; -- GitLab