From b30c0d82642d74ac84d59d646630e06d235c5dd4 Mon Sep 17 00:00:00 2001 From: Rhys Perry Date: Tue, 31 Mar 2026 17:45:23 +0100 Subject: [PATCH] nir/algebraic: optimize exact f2u32(fmul(unpack_norm)) fossil-db (navi21): Totals from 16 (0.01% of 202427) affected shaders: Instrs: 17730 -> 17226 (-2.84%) CodeSize: 97500 -> 95708 (-1.84%) InvThroughput: 44437 -> 44419 (-0.04%) Copies: 1502 -> 1446 (-3.73%) VALU: 9973 -> 9525 (-4.49%) SALU: 3509 -> 3453 (-1.60%) Signed-off-by: Rhys Perry Reviewed-by: Georg Lehmann Part-of: --- src/compiler/nir/nir_opt_algebraic.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/compiler/nir/nir_opt_algebraic.py b/src/compiler/nir/nir_opt_algebraic.py index aa9999034cf..f1db9880d59 100644 --- a/src/compiler/nir/nir_opt_algebraic.py +++ b/src/compiler/nir/nir_opt_algebraic.py @@ -2708,6 +2708,23 @@ optimizations.extend([ 127.0))), 'options->lower_unpack_snorm_4x8'), + # f2u32(fmul(unpack_*norm_*)). These are exact replacements. + # 0x37800080 = 1.0 / 65535.0 + # 0x3b808081 = 1.0 / 255.0 + # 0x38000100 = 1.0 / 32767.0 + # 0x3c010204 = 1.0 / 127.0 + (('f2u32', ('fmul', ('fmul', ('u2f32', ('extract_u16', 'a@32', b)), 0x37800080), 65535.0)), + ('extract_u8', a, b), + '!nir_is_rounding_mode_rtz(info->float_controls_execution_mode, 32)'), + (('f2u32', ('fmul', ('fmul', ('u2f32', ('extract_u8', 'a@32', b)), 0x3b808081), 255.0)), + ('extract_u8', a, b)), + (('f2u32', ('fmul', ('fmin', 1.0, ('fmax', -1.0, ('fmul', ('u2f32', ('extract_i16', 'a@32', b)), 0x38000100))), 32767.0)), + ('imax', ('extract_i16', a, b), -32767), + '!nir_is_rounding_mode_rtz(info->float_controls_execution_mode, 32)'), + (('f2u32', ('fmul', ('fmin', 1.0, ('fmax', -1.0, ('fmul', ('u2f32', ('extract_i8', 'a@32', b)), 0x3c010204))), 127.0)), + ('imax', ('extract_i8', a, b), -127), + '!nir_is_rounding_mode_rtz(info->float_controls_execution_mode, 32)'), + (('pack_half_2x16_split', 'a@32', 'b@32'), ('ior', ('ishl', ('u2u32', ('f2f16', b)), 16), ('u2u32', ('f2f16', a))), 'options->lower_pack_split'),