diff --git a/src/amd/vulkan/radv_shader.c b/src/amd/vulkan/radv_shader.c index cfa497ceea1..d691cd13fe0 100644 --- a/src/amd/vulkan/radv_shader.c +++ b/src/amd/vulkan/radv_shader.c @@ -208,7 +208,11 @@ radv_optimize_nir(struct nir_shader *shader, bool optimize_conservatively) nir_var_function_temp | nir_var_shader_in | nir_var_shader_out | nir_var_mem_shared, NULL); if (shader->info.stage == MESA_SHADER_FRAGMENT && shader->info.fs.uses_discard) { - NIR_PASS(progress, shader, nir_opt_conditional_discard); + nir_opt_peephole_select_options peephole_select_options = { + .limit = 0, + .discard_ok = true, + }; + NIR_PASS(progress, shader, nir_opt_peephole_select, &peephole_select_options); NIR_PASS(progress, shader, nir_opt_move_discards_to_top); } diff --git a/src/asahi/vulkan/hk_shader.c b/src/asahi/vulkan/hk_shader.c index 48eaafcdfb5..47316450497 100644 --- a/src/asahi/vulkan/hk_shader.c +++ b/src/asahi/vulkan/hk_shader.c @@ -811,7 +811,12 @@ hk_lower_nir(struct hk_device *dev, nir_shader *nir, NIR_PASS(_, nir, agx_nir_lower_multisampled_image_store); agx_preprocess_nir(nir); - NIR_PASS(_, nir, nir_opt_conditional_discard); + + nir_opt_peephole_select_options peephole_select_options = { + .limit = 0, + .discard_ok = true, + }; + NIR_PASS(_, nir, nir_opt_peephole_select, &peephole_select_options); NIR_PASS(_, nir, nir_opt_if, nir_opt_if_optimize_phi_true_false | nir_opt_if_avoid_64bit_phis); } diff --git a/src/broadcom/compiler/nir_to_vir.c b/src/broadcom/compiler/nir_to_vir.c index 4c7acf69b60..4dbe81f7e34 100644 --- a/src/broadcom/compiler/nir_to_vir.c +++ b/src/broadcom/compiler/nir_to_vir.c @@ -2188,7 +2188,11 @@ v3d_optimize_nir(struct v3d_compile *c, struct nir_shader *s) NIR_PASS(progress, s, nir_opt_dce); } - NIR_PASS(progress, s, nir_opt_conditional_discard); + peephole_select_options = (nir_opt_peephole_select_options){ + .limit = 0, + .discard_ok = true, + }; + NIR_PASS(progress, s, nir_opt_peephole_select, &peephole_select_options); NIR_PASS(progress, s, nir_opt_remove_phis); NIR_PASS(progress, s, nir_opt_if, false); diff --git a/src/compiler/glsl/gl_nir_linker.c b/src/compiler/glsl/gl_nir_linker.c index fc4958df6a9..89b6cd8fd42 100644 --- a/src/compiler/glsl/gl_nir_linker.c +++ b/src/compiler/glsl/gl_nir_linker.c @@ -128,7 +128,12 @@ gl_nir_opts(nir_shader *nir) } NIR_PASS(progress, nir, nir_opt_undef); - NIR_PASS(progress, nir, nir_opt_conditional_discard); + + peephole_select_options = (nir_opt_peephole_select_options){ + .limit = 0, + .discard_ok = true, + }; + NIR_PASS(progress, nir, nir_opt_peephole_select, &peephole_select_options); if (nir->options->max_unroll_iterations || (nir->options->max_unroll_iterations_fp64 && (nir->options->lower_doubles_options & nir_lower_fp64_full_software))) { diff --git a/src/compiler/nir/meson.build b/src/compiler/nir/meson.build index 0e0895bd7e5..b4ab113974a 100644 --- a/src/compiler/nir/meson.build +++ b/src/compiler/nir/meson.build @@ -227,7 +227,6 @@ files_libnir = files( 'nir_opt_clip_cull_const.c', 'nir_opt_combine_stores.c', 'nir_opt_comparison_pre.c', - 'nir_opt_conditional_discard.c', 'nir_opt_constant_folding.c', 'nir_opt_copy_prop_vars.c', 'nir_opt_copy_propagate.c', diff --git a/src/compiler/nir/nir.h b/src/compiler/nir/nir.h index 4a0dd80c3c5..e4754106541 100644 --- a/src/compiler/nir/nir.h +++ b/src/compiler/nir/nir.h @@ -6094,7 +6094,6 @@ bool nir_opt_vectorize(nir_shader *shader, nir_vectorize_cb filter, void *data); bool nir_opt_vectorize_io(nir_shader *shader, nir_variable_mode modes); -bool nir_opt_conditional_discard(nir_shader *shader); bool nir_opt_move_discards_to_top(nir_shader *shader); bool nir_opt_ray_queries(nir_shader *shader); diff --git a/src/compiler/nir/nir_opt_conditional_discard.c b/src/compiler/nir/nir_opt_conditional_discard.c deleted file mode 100644 index aa4bdd35e31..00000000000 --- a/src/compiler/nir/nir_opt_conditional_discard.c +++ /dev/null @@ -1,136 +0,0 @@ -/* - * Copyright © 2016 Red Hat - * - * 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. - */ - -#include "nir.h" -#include "nir_builder.h" - -/** @file nir_opt_conditional_discard.c - * - * Handles optimization of lowering of - * - if (cond) discard to discard_if(cond) and - * - if (cond) demote to demote_if(cond) - * - if (cond) terminate to terminate_if(cond) - */ - -static bool -nir_opt_conditional_discard_block(nir_builder *b, nir_block *block) -{ - if (nir_cf_node_is_first(&block->cf_node)) - return false; - - nir_cf_node *prev_node = nir_cf_node_prev(&block->cf_node); - if (prev_node->type != nir_cf_node_if) - return false; - - nir_if *if_stmt = nir_cf_node_as_if(prev_node); - nir_block *then_block = nir_if_first_then_block(if_stmt); - nir_block *else_block = nir_if_first_else_block(if_stmt); - - /* check there is only one else block and it is empty */ - if (nir_if_last_else_block(if_stmt) != else_block) - return false; - if (!exec_list_is_empty(&else_block->instr_list)) - return false; - - /* check there is only one then block and it has only one instruction in it */ - if (nir_if_last_then_block(if_stmt) != then_block) - return false; - if (exec_list_is_empty(&then_block->instr_list)) - return false; - if (exec_list_length(&then_block->instr_list) > 1) - return false; - /* - * make sure no subsequent phi nodes point at this if. - */ - nir_block *after = nir_cf_node_as_block(nir_cf_node_next(&if_stmt->cf_node)); - nir_foreach_phi_safe(phi, after) { - nir_foreach_phi_src(phi_src, phi) { - if (phi_src->pred == then_block || - phi_src->pred == else_block) - return false; - } - } - - /* Get the first instruction in the then block and confirm it is - * a discard or a demote instruction. - */ - nir_instr *instr = nir_block_first_instr(then_block); - if (instr->type != nir_instr_type_intrinsic) - return false; - - nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr); - nir_intrinsic_op op = intrin->intrinsic; - nir_def *cond = if_stmt->condition.ssa; - b->cursor = nir_before_cf_node(prev_node); - - switch (intrin->intrinsic) { - case nir_intrinsic_demote: - op = nir_intrinsic_demote_if; - break; - case nir_intrinsic_terminate: - op = nir_intrinsic_terminate_if; - break; - case nir_intrinsic_demote_if: - case nir_intrinsic_terminate_if: - cond = nir_iand(b, cond, intrin->src[0].ssa); - break; - default: - return false; - } - - nir_intrinsic_instr *discard_if = - nir_intrinsic_instr_create(b->shader, op); - discard_if->src[0] = nir_src_for_ssa(cond); - - nir_instr_insert_before_cf(prev_node, &discard_if->instr); - nir_instr_remove(&intrin->instr); - nir_cf_node_remove(&if_stmt->cf_node); - - return true; -} - -bool -nir_opt_conditional_discard(nir_shader *shader) -{ - bool progress = false; - - nir_builder builder; - - nir_foreach_function_impl(impl, shader) { - builder = nir_builder_create(impl); - - bool impl_progress = false; - nir_foreach_block_safe(block, impl) { - if (nir_opt_conditional_discard_block(&builder, block)) - impl_progress = true; - } - - if (impl_progress) { - nir_metadata_preserve(impl, nir_metadata_none); - progress = true; - } else { - nir_metadata_preserve(impl, nir_metadata_all); - } - } - return progress; -} diff --git a/src/compiler/nir/nir_opt_move_discards_to_top.c b/src/compiler/nir/nir_opt_move_discards_to_top.c index ea046f487e6..5f9f8e7ea5a 100644 --- a/src/compiler/nir/nir_opt_move_discards_to_top.c +++ b/src/compiler/nir/nir_opt_move_discards_to_top.c @@ -344,8 +344,8 @@ break_all: return progress; } -/* This optimization only operates on discard_if/demoe_if so - * nir_opt_conditional_discard and nir_lower_discard_or_demote +/* This optimization only operates on terminate_if/demote_if so + * nir_opt_peephole_select and nir_lower_discard_or_demote * should have been called before. */ bool diff --git a/src/freedreno/vulkan/tu_shader.cc b/src/freedreno/vulkan/tu_shader.cc index d52e53c83dc..2d93bdc4bf6 100644 --- a/src/freedreno/vulkan/tu_shader.cc +++ b/src/freedreno/vulkan/tu_shader.cc @@ -135,7 +135,11 @@ tu_spirv_to_nir(struct tu_device *dev, init_ir3_nir_options(&options, key); ir3_optimize_loop(dev->compiler, &options, nir); - NIR_PASS_V(nir, nir_opt_conditional_discard); + nir_opt_peephole_select_options peephole_select_options = { + .limit = 0, + .discard_ok = true, + }; + NIR_PASS_V(nir, nir_opt_peephole_select, &peephole_select_options); return nir; } diff --git a/src/gallium/auxiliary/nir/nir_to_tgsi.c b/src/gallium/auxiliary/nir/nir_to_tgsi.c index eea4af85c50..7fe935d651d 100644 --- a/src/gallium/auxiliary/nir/nir_to_tgsi.c +++ b/src/gallium/auxiliary/nir/nir_to_tgsi.c @@ -3330,7 +3330,12 @@ ntt_optimize_nir(struct nir_shader *s, struct pipe_screen *screen, NIR_PASS(progress, s, nir_opt_algebraic); NIR_PASS(progress, s, nir_opt_constant_folding); NIR_PASS(progress, s, nir_opt_remove_phis); - NIR_PASS(progress, s, nir_opt_conditional_discard); + + nir_opt_peephole_select_options peephole_discard_options = { + .limit = 0, + .discard_ok = true, + }; + NIR_PASS(progress, s, nir_opt_peephole_select, &peephole_discard_options); NIR_PASS(progress, s, nir_opt_dce); NIR_PASS(progress, s, nir_opt_dead_cf); NIR_PASS(progress, s, nir_opt_cse); diff --git a/src/gallium/auxiliary/nir/tgsi_to_nir.c b/src/gallium/auxiliary/nir/tgsi_to_nir.c index 2d71393135c..c59d99ff7f2 100644 --- a/src/gallium/auxiliary/nir/tgsi_to_nir.c +++ b/src/gallium/auxiliary/nir/tgsi_to_nir.c @@ -2454,7 +2454,12 @@ ttn_optimize_nir(nir_shader *nir) } NIR_PASS(progress, nir, nir_opt_undef); - NIR_PASS(progress, nir, nir_opt_conditional_discard); + + nir_opt_peephole_select_options peephole_discard_options = { + .limit = 0, + .discard_ok = true, + }; + NIR_PASS(progress, nir, nir_opt_peephole_select, &peephole_discard_options); if (nir->options->max_unroll_iterations) { NIR_PASS(progress, nir, nir_opt_loop_unroll); } diff --git a/src/gallium/drivers/i915/i915_screen.c b/src/gallium/drivers/i915/i915_screen.c index ce11213b825..2f68f1eb5d6 100644 --- a/src/gallium/drivers/i915/i915_screen.c +++ b/src/gallium/drivers/i915/i915_screen.c @@ -200,7 +200,12 @@ i915_optimize_nir(struct nir_shader *s) NIR_PASS(progress, s, nir_opt_algebraic); NIR_PASS(progress, s, nir_opt_constant_folding); NIR_PASS(progress, s, nir_opt_remove_phis); - NIR_PASS(progress, s, nir_opt_conditional_discard); + + nir_opt_peephole_select_options peephole_discard_options = { + .limit = 0, + .discard_ok = true, + }; + NIR_PASS(progress, s, nir_opt_peephole_select, &peephole_discard_options); NIR_PASS(progress, s, nir_opt_dce); NIR_PASS(progress, s, nir_opt_dead_cf); NIR_PASS(progress, s, nir_opt_cse); diff --git a/src/gallium/drivers/r300/compiler/r300_nir.c b/src/gallium/drivers/r300/compiler/r300_nir.c index 58f4e9b0fea..d3ddd497f32 100644 --- a/src/gallium/drivers/r300/compiler/r300_nir.c +++ b/src/gallium/drivers/r300/compiler/r300_nir.c @@ -179,7 +179,12 @@ r300_optimize_nir(struct nir_shader *s, struct pipe_screen *screen) } NIR_PASS(progress, s, nir_opt_constant_folding); NIR_PASS(progress, s, nir_opt_remove_phis); - NIR_PASS(progress, s, nir_opt_conditional_discard); + + nir_opt_peephole_select_options peephole_discard_options = { + .limit = 0, + .discard_ok = true, + }; + NIR_PASS(progress, s, nir_opt_peephole_select, &peephole_discard_options); NIR_PASS(progress, s, nir_opt_dce); NIR_PASS(progress, s, nir_opt_dead_cf); NIR_PASS(progress, s, nir_opt_cse); diff --git a/src/gallium/drivers/r600/sfn/sfn_nir.cpp b/src/gallium/drivers/r600/sfn/sfn_nir.cpp index 2e2885c421d..85ca58ac78a 100644 --- a/src/gallium/drivers/r600/sfn/sfn_nir.cpp +++ b/src/gallium/drivers/r600/sfn/sfn_nir.cpp @@ -646,7 +646,11 @@ optimize_once(nir_shader *shader) }; NIR_PASS(progress, shader, nir_opt_peephole_select, &peephole_select_options); - NIR_PASS(progress, shader, nir_opt_conditional_discard); + nir_opt_peephole_select_options peephole_discard_options = { + .limit = 0, + .discard_ok = true, + }; + NIR_PASS(progress, shader, nir_opt_peephole_select, &peephole_discard_options); NIR_PASS(progress, shader, nir_opt_dce); NIR_PASS(progress, shader, nir_opt_undef); NIR_PASS(progress, shader, nir_opt_loop_unroll); diff --git a/src/gallium/drivers/radeonsi/si_shader_nir.c b/src/gallium/drivers/radeonsi/si_shader_nir.c index aaf1c618971..524bc8db8c9 100644 --- a/src/gallium/drivers/radeonsi/si_shader_nir.c +++ b/src/gallium/drivers/radeonsi/si_shader_nir.c @@ -130,7 +130,12 @@ void si_nir_opts(struct si_screen *sscreen, struct nir_shader *nir, bool has_arr } NIR_PASS(progress, nir, nir_opt_undef); - NIR_PASS(progress, nir, nir_opt_conditional_discard); + + nir_opt_peephole_select_options peephole_discard_options = { + .limit = 0, + .discard_ok = true, + }; + NIR_PASS(progress, nir, nir_opt_peephole_select, &peephole_discard_options); if (nir->options->max_unroll_iterations) { NIR_PASS(progress, nir, nir_opt_loop_unroll); } diff --git a/src/gallium/frontends/lavapipe/lvp_pipeline.c b/src/gallium/frontends/lavapipe/lvp_pipeline.c index b028927e82a..c0b17179309 100644 --- a/src/gallium/frontends/lavapipe/lvp_pipeline.c +++ b/src/gallium/frontends/lavapipe/lvp_pipeline.c @@ -256,7 +256,12 @@ optimize(nir_shader *nir) } NIR_PASS(progress, nir, nir_opt_if, nir_opt_if_optimize_phi_true_false); NIR_PASS(progress, nir, nir_opt_dead_cf); - NIR_PASS(progress, nir, nir_opt_conditional_discard); + + nir_opt_peephole_select_options peephole_discard_options = { + .limit = 0, + .discard_ok = true, + }; + NIR_PASS(progress, nir, nir_opt_peephole_select, &peephole_discard_options); NIR_PASS(progress, nir, nir_opt_remove_phis); NIR_PASS(progress, nir, nir_opt_cse); NIR_PASS(progress, nir, nir_opt_undef); diff --git a/src/intel/compiler/brw_nir.c b/src/intel/compiler/brw_nir.c index 76ea6cf12d9..95a1daeddb0 100644 --- a/src/intel/compiler/brw_nir.c +++ b/src/intel/compiler/brw_nir.c @@ -920,7 +920,12 @@ brw_nir_optimize(nir_shader *nir, LOOP_OPT(nir_opt_dce); } LOOP_OPT_NOT_IDEMPOTENT(nir_opt_if, nir_opt_if_optimize_phi_true_false); - LOOP_OPT(nir_opt_conditional_discard); + + nir_opt_peephole_select_options peephole_discard_options = { + .limit = 0, + .discard_ok = true, + }; + LOOP_OPT(nir_opt_peephole_select, &peephole_discard_options); if (nir->options->max_unroll_iterations != 0) { LOOP_OPT_NOT_IDEMPOTENT(nir_opt_loop_unroll); } diff --git a/src/intel/compiler/elk/elk_nir.c b/src/intel/compiler/elk/elk_nir.c index c3586a08390..c1756c9706d 100644 --- a/src/intel/compiler/elk/elk_nir.c +++ b/src/intel/compiler/elk/elk_nir.c @@ -710,7 +710,12 @@ elk_nir_optimize(nir_shader *nir, bool is_scalar, OPT(nir_opt_dce); } OPT(nir_opt_if, nir_opt_if_optimize_phi_true_false); - OPT(nir_opt_conditional_discard); + + nir_opt_peephole_select_options peephole_discard_options = { + .limit = 0, + .discard_ok = true, + }; + OPT(nir_opt_peephole_select, &peephole_discard_options); if (nir->options->max_unroll_iterations != 0) { OPT(nir_opt_loop_unroll); } diff --git a/src/nouveau/compiler/nak_nir.c b/src/nouveau/compiler/nak_nir.c index cc4ba992033..33a63533d6b 100644 --- a/src/nouveau/compiler/nak_nir.c +++ b/src/nouveau/compiler/nak_nir.c @@ -138,6 +138,7 @@ optimize_nir(nir_shader *nir, const struct nak_compiler *nak, bool allow_copies) nir_opt_peephole_select_options peephole_select_options = { .limit = 0, + .discard_ok = true, }; OPT(nir, nir_opt_peephole_select, &peephole_select_options); OPT(nir, nir_opt_intrinsics); @@ -163,7 +164,6 @@ optimize_nir(nir_shader *nir, const struct nak_compiler *nak, bool allow_copies) OPT(nir, nir_opt_dce); } OPT(nir, nir_opt_if, nir_opt_if_optimize_phi_true_false); - OPT(nir, nir_opt_conditional_discard); if (nir->options->max_unroll_iterations != 0) { OPT(nir, nir_opt_loop_unroll); }