From c0efa612cb377692a782e0d1879881433e73da62 Mon Sep 17 00:00:00 2001 From: Dave Airlie Date: Thu, 27 Nov 2025 10:30:28 +1000 Subject: [PATCH] nir/algebraic: optimise ishl + ubfe into iand + ishl/ushr I've no idea if this correct, but I've got an insane shader that -Spilled VGPRs: 4063 -Code size: 253708 +Spilled VGPRs: 3635 +Code size: 226124 -Instructions: 41025 -Copies: 4 +Instructions: 40112 +Copies: 1028 --- src/compiler/nir/nir_opt_algebraic.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/compiler/nir/nir_opt_algebraic.py b/src/compiler/nir/nir_opt_algebraic.py index 10b2773d8c5..588d76ebdcd 100644 --- a/src/compiler/nir/nir_opt_algebraic.py +++ b/src/compiler/nir/nir_opt_algebraic.py @@ -594,6 +594,14 @@ def ubfe_ubfe(a, b, c, d, e): # whose offset and bits operands will also be constant folded. return ('bcsel', offset_out_of_range, 0, collapsed) +# drop ubfe + ushl - into iand + ushl/ushr +def ubfe_iand(a, b, c, d): + iand_val = ('iand', a, ('ishl', ('isub', ('ishl', 1, c), 1), b)) + ishl_val = ('ishl', iand_val, ('isub', d, b)) + ushr_val = ('ushr', iand_val, ('isub', b, d)) + cmp_val = ('ilt', b, d) + return ('bcsel', cmp_val, ishl_val, ushr_val) + optimizations.extend([ # Create bitfield extract from right-shift + and pattern. (('iand@32', ('ushr@32(is_used_once)', a, b), '#c(is_const_bitmask)'), @@ -620,6 +628,10 @@ optimizations.extend([ (('ushr@32', ('ubfe', a, '#b', '#c'), '#d(is_5lsb_not_zero)'), ubfe_ubfe(a, b, c, d, 31)), + # Replace ishl/ubfe with ishl/iand + (('ishl', ('ubfe', a, '#b', '#c'), '#d'), + ubfe_iand(a, b, c, d)), + (('iand', 'a(is_unsigned_multiple_of_4)', -4), a), ]) -- 2.52.0