From 3e0c9ad3169c0a2c4eff4730f0b1c0fb53a1cf22 Mon Sep 17 00:00:00 2001 From: Ian Romanick Date: Fri, 7 Nov 2025 09:56:55 -0800 Subject: [PATCH] nir/opt_if: Conditionally do not propagate constants through bcsel In some cases propagating through a bcsel may be harmful. If the bcsel uses are unlikely to be eliminated in both branch of an if statement, propagating through it may result in extra moves for phi instructions and extended live ranges. v2: Fix missing parameter in call. Noticed by Rhys. I fixed this on the test machine, but I must have forgotten to propagate the change back to my dev machine. Reviewed-by: Rhys Perry Part-of: --- src/compiler/nir/nir_opt_if.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/compiler/nir/nir_opt_if.c b/src/compiler/nir/nir_opt_if.c index c2df6876439..ea336a19cf0 100644 --- a/src/compiler/nir/nir_opt_if.c +++ b/src/compiler/nir/nir_opt_if.c @@ -958,8 +958,15 @@ propagate_condition_eval(nir_builder *b, nir_if *nif, nir_src *use_src, return true; } +/** + * In some cases propagating through a bcsel may be harmful. If the bcsel uses + * are unlikely to be eliminated in both branch of an if statement, + * propagating through it may result in extra moves for phi instructions and + * extended live ranges. The \c ignore_bcsel flag can be used to avoid these + * cases. + */ static bool -can_propagate_through_alu(nir_src *src) +can_propagate_through_alu(nir_src *src, bool ignore_bcsel) { if (nir_src_parent_instr(src)->type != nir_instr_type_alu) return false; @@ -972,7 +979,7 @@ can_propagate_through_alu(nir_src *src) case nir_op_b2i32: return true; case nir_op_bcsel: - return src == &alu->src[0].src; + return src == &alu->src[0].src && !ignore_bcsel; default: return false; } @@ -993,7 +1000,7 @@ evaluate_condition_use(nir_builder *b, nir_if *nif, nir_src *use_src, progress = true; } - if (!nir_src_is_if(use_src) && can_propagate_through_alu(use_src)) { + if (!nir_src_is_if(use_src) && can_propagate_through_alu(use_src), false) { nir_alu_instr *alu = nir_instr_as_alu(nir_src_parent_instr(use_src)); nir_foreach_use_including_if_safe(alu_use, &alu->def) {