nir/opt_if: allow load_const instructions on ELSE-side for if-simplifaction

Totals from 1974 (0.98% of 202440) affected shaders: (Navi48)

Instrs: 6438920 -> 6437055 (-0.03%); split: -0.06%, +0.03%
CodeSize: 35080732 -> 35075136 (-0.02%); split: -0.04%, +0.02%
SpillSGPRs: 2106 -> 2122 (+0.76%)
Latency: 52684517 -> 52677236 (-0.01%); split: -0.02%, +0.01%
InvThroughput: 8977644 -> 8976740 (-0.01%); split: -0.01%, +0.00%
VClause: 124447 -> 124444 (-0.00%)
SClause: 117561 -> 117560 (-0.00%); split: -0.00%, +0.00%
Copies: 413450 -> 410708 (-0.66%); split: -0.67%, +0.01%
Branches: 136429 -> 136169 (-0.19%); split: -0.20%, +0.01%
PreSGPRs: 114813 -> 114918 (+0.09%); split: -0.01%, +0.10%
PreVGPRs: 108142 -> 108145 (+0.00%); split: -0.00%, +0.00%
VALU: 3275624 -> 3274927 (-0.02%); split: -0.03%, +0.00%
SALU: 1166159 -> 1165039 (-0.10%); split: -0.17%, +0.07%
VOPD: 333456 -> 333183 (-0.08%); split: +0.02%, -0.10%
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/40738>
This commit is contained in:
Daniel Schürmann 2026-03-31 19:04:50 +02:00 committed by Marge Bot
parent dbb7050ca7
commit 1394f79517

View file

@ -694,11 +694,19 @@ opt_simplify_bcsel_of_phi(nir_builder *b, nir_loop *loop)
return progress;
}
/* Checks whether the block is empty except for load_const instructions. */
static bool
is_block_empty(nir_block *block)
is_block_empty_or_constant(nir_block *block)
{
return nir_cf_node_is_last(&block->cf_node) &&
exec_list_is_empty(&block->instr_list);
if (!nir_cf_node_is_last(&block->cf_node))
return false;
nir_foreach_instr(instr, block) {
if (instr->type != nir_instr_type_load_const)
return false;
}
return true;
}
/* Walk all the phis in the block immediately following the if statement and
@ -744,8 +752,8 @@ static bool
opt_if_simplification(nir_builder *b, nir_if *nif)
{
/* Only simplify if the then block is empty and the else block is not. */
if (!is_block_empty(nir_if_first_then_block(nif)) ||
is_block_empty(nir_if_first_else_block(nif)))
if (!is_block_empty_or_constant(nir_if_first_then_block(nif)) ||
is_block_empty_or_constant(nir_if_first_else_block(nif)))
return false;
/* Insert the inverted instruction and rewrite the condition. */
@ -770,8 +778,13 @@ opt_if_simplification(nir_builder *b, nir_if *nif)
rewrite_phi_predecessor_blocks(nif, then_block, else_block, else_block,
then_block);
/* Finally, move the else block to the then block. */
/* Move potential load_const before the if. */
nir_cf_list tmp;
nir_cf_extract(&tmp, nir_before_cf_list(&nif->then_list),
nir_after_cf_list(&nif->then_list));
nir_cf_reinsert(&tmp, nir_before_cf_node(&nif->cf_node));
/* Finally, move the else block to the then block. */
nir_cf_extract(&tmp, nir_before_cf_list(&nif->else_list),
nir_after_cf_list(&nif->else_list));
nir_cf_reinsert(&tmp, nir_before_cf_list(&nif->then_list));