From a9f80892403aaa07d134898c5a2931b26ff40702 Mon Sep 17 00:00:00 2001 From: Georg Lehmann Date: Fri, 6 Sep 2024 14:01:30 +0200 Subject: [PATCH] nir: replace nir_opt_remove_phis_block with a single source version MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This is what callers actually want, and it simplifies nir_opt_remove_phis because we can assume dominance meta data is valid. Reviewed-by: Daniel Schürmann Part-of: --- .../aco_instruction_selection_setup.cpp | 2 +- src/compiler/nir/nir.h | 2 +- src/compiler/nir/nir_opt_if.c | 2 +- src/compiler/nir/nir_opt_loop.c | 6 ++--- src/compiler/nir/nir_opt_remove_phis.c | 23 ++++++++++++++++--- 5 files changed, 26 insertions(+), 9 deletions(-) diff --git a/src/amd/compiler/aco_instruction_selection_setup.cpp b/src/amd/compiler/aco_instruction_selection_setup.cpp index 7f865437794..f3c6548f9d2 100644 --- a/src/amd/compiler/aco_instruction_selection_setup.cpp +++ b/src/amd/compiler/aco_instruction_selection_setup.cpp @@ -88,7 +88,7 @@ sanitize_if(nir_function_impl* impl, nir_if* nif) * or dead control-flow passes and are perfectly legal. Run a quick phi * removal on the block after the if to clean up any such phis. */ - nir_opt_remove_phis_block(nir_cf_node_as_block(nir_cf_node_next(&nif->cf_node))); + nir_remove_single_src_phis_block(nir_cf_node_as_block(nir_cf_node_next(&nif->cf_node))); /* Finally, move the continue from branch after the if-statement. */ nir_block* last_continue_from_blk = then_jump ? else_block : then_block; diff --git a/src/compiler/nir/nir.h b/src/compiler/nir/nir.h index 98a433275f3..2854d514ea5 100644 --- a/src/compiler/nir/nir.h +++ b/src/compiler/nir/nir.h @@ -6817,7 +6817,7 @@ bool nir_opt_reassociate_bfi(nir_shader *shader); bool nir_opt_rematerialize_compares(nir_shader *shader); bool nir_opt_remove_phis(nir_shader *shader); -bool nir_opt_remove_phis_block(nir_block *block); +bool nir_remove_single_src_phis_block(nir_block *block); bool nir_opt_phi_precision(nir_shader *shader); diff --git a/src/compiler/nir/nir_opt_if.c b/src/compiler/nir/nir_opt_if.c index 85206c7e92e..0cfb4911341 100644 --- a/src/compiler/nir/nir_opt_if.c +++ b/src/compiler/nir/nir_opt_if.c @@ -766,7 +766,7 @@ opt_if_simplification(nir_builder *b, nir_if *nif) */ nir_block *const next_block = nir_cf_node_as_block(nir_cf_node_next(&nif->cf_node)); - nir_opt_remove_phis_block(next_block); + nir_remove_single_src_phis_block(next_block); } rewrite_phi_predecessor_blocks(nif, then_block, else_block, else_block, diff --git a/src/compiler/nir/nir_opt_loop.c b/src/compiler/nir/nir_opt_loop.c index 1588c13756b..2b1d4287652 100644 --- a/src/compiler/nir/nir_opt_loop.c +++ b/src/compiler/nir/nir_opt_loop.c @@ -162,7 +162,7 @@ opt_loop_terminator(nir_if *nif) * or dead control-flow passes and are perfectly legal. Run a quick phi * removal on the block after the if to clean up any such phis. */ - nir_opt_remove_phis_block(nir_cf_node_as_block(nir_cf_node_next(&nif->cf_node))); + nir_remove_single_src_phis_block(nir_cf_node_as_block(nir_cf_node_next(&nif->cf_node))); /* Finally, move the continue from branch after the if-statement. */ nir_cf_list tmp; @@ -266,7 +266,7 @@ opt_loop_last_block(nir_block *block, bool is_trivial_continue, bool is_trivial_ continue; /* If there are single-source phis after the IF, get rid of them first */ - nir_opt_remove_phis_block(nir_cf_node_cf_tree_next(prev)); + nir_remove_single_src_phis_block(nir_cf_node_cf_tree_next(prev)); /* We are about to remove one predecessor. */ nir_lower_phis_to_regs_block(block->successors[0]); @@ -405,7 +405,7 @@ opt_loop_peel_initial_break(nir_loop *loop) * or dead control-flow passes and are perfectly legal. Run a quick phi * removal on the block after the if to clean up any such phis. */ - nir_opt_remove_phis_block(nir_cf_node_cf_tree_next(if_node)); + nir_remove_single_src_phis_block(nir_cf_node_cf_tree_next(if_node)); /* We need LCSSA because we are going to wrap the loop into an IF. */ nir_convert_loop_to_lcssa(loop); diff --git a/src/compiler/nir/nir_opt_remove_phis.c b/src/compiler/nir/nir_opt_remove_phis.c index c91249e52b7..bf467bf116c 100644 --- a/src/compiler/nir/nir_opt_remove_phis.c +++ b/src/compiler/nir/nir_opt_remove_phis.c @@ -133,10 +133,27 @@ remove_phis_block(nir_block *block, nir_builder *b) } bool -nir_opt_remove_phis_block(nir_block *block) +nir_remove_single_src_phis_block(nir_block *block) { - nir_builder b = nir_builder_create(nir_cf_node_get_function(&block->cf_node)); - return remove_phis_block(block, &b); + assert(block->predecessors->entries <= 1); + bool progress = false; + nir_foreach_phi_safe(phi, block) { + nir_def *def = NULL; + nir_foreach_phi_src(src, phi) { + def = src->src.ssa; + break; + } + + if (!def) { + nir_builder b = nir_builder_create(nir_cf_node_get_function(&block->cf_node)); + b.cursor = nir_after_phis(block); + def = nir_undef(&b, phi->def.num_components, phi->def.bit_size); + } + + nir_def_replace(&phi->def, def); + progress = true; + } + return progress; } static bool