From 0a30611c101d7f1b58303a734ce429985c3946e5 Mon Sep 17 00:00:00 2001 From: Georg Lehmann Date: Tue, 29 Apr 2025 09:55:57 +0200 Subject: [PATCH] nir/opt_algebraic: some bitfield_select optimizations MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Foz-DB Navi21: Totals from 47 (0.06% of 79789) affected shaders: Instrs: 69536 -> 69363 (-0.25%) CodeSize: 370624 -> 369388 (-0.33%) Latency: 383505 -> 383298 (-0.05%) InvThroughput: 72924 -> 72727 (-0.27%) PreSGPRs: 2618 -> 2610 (-0.31%) VALU: 43261 -> 43091 (-0.39%) SALU: 13065 -> 13063 (-0.02%) Reviewed-by: Marek Olšák Part-of: --- src/compiler/nir/nir_opt_algebraic.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/compiler/nir/nir_opt_algebraic.py b/src/compiler/nir/nir_opt_algebraic.py index 13d18a3cb1d..f42577122de 100644 --- a/src/compiler/nir/nir_opt_algebraic.py +++ b/src/compiler/nir/nir_opt_algebraic.py @@ -2437,9 +2437,25 @@ optimizations.extend([ ('ubfe', 'value', 'offset', 'bits')), 'options->lower_bitfield_extract && options->has_bfe'), - # (src0 & src1) | (~src0 & src2). Constant fold if src2 is 0. + # (src0 & src1) | (~src0 & src2). Constant fold if a src is 0/-1. (('bitfield_select', a, b, 0), ('iand', a, b)), + (('bitfield_select', a, 0, b), ('iand', ('inot', a), b)), + (('bitfield_select', 0, a, b), b), + (('bitfield_select', a, b, -1), ('ior', ('inot', a), b)), + (('bitfield_select', a, -1, b), ('ior', a, b)), + (('bitfield_select', -1, a, b), a), + (('bitfield_select', a, b, b), b), + (('bitfield_select', a, ('inot', b), b), ('ixor', a, b)), + (('bitfield_select', a, b, ('inot', b)), ('inot', ('ixor', a, b))), (('bitfield_select', a, ('iand', a, b), c), ('bitfield_select', a, b, c)), + (('bitfield_select', a, b, ('iand', ('inot', a), c)), ('bitfield_select', a, b, c)), + (('bitfield_select', ('inot', a), b, c), ('bitfield_select', a, c, b)), + (('bitfield_select', ('ineg', ('b2i', 'a@1')), b, c), ('bcsel', a, b, c)), + + (('ior@32', ('iand', a, b), ('iand', ('inot', a), c)), ('bitfield_select', a, b, c), 'options->has_bitfield_select'), + (('iadd@32', ('iand', a, b), ('iand', ('inot', a), c)), ('bitfield_select', a, b, c), 'options->has_bitfield_select'), + (('ixor@32', ('iand', a, b), ('iand', ('inot', a), c)), ('bitfield_select', a, b, c), 'options->has_bitfield_select'), + (('ixor@32', ('iand', a, ('ixor', b, c)), c), ('bitfield_select', a, b, c), 'options->has_bitfield_select'), # Note that these opcodes are defined to only use the five least significant bits of 'offset' and 'bits' (('ubfe', 'value', 'offset', ('iand', 31, 'bits')), ('ubfe', 'value', 'offset', 'bits')),