From e1bb53bb3c7c14ff4d33a895ddebe9e7494635de Mon Sep 17 00:00:00 2001 From: Ian Romanick Date: Tue, 3 Dec 2024 12:09:27 -0800 Subject: [PATCH] nir/algebraic: Optimize some trivial bfi In fossil-db, one big compute shader on Hogwarts Legacy is helped for spills and fills. It has a lot of instances of bfi(0x3f, a, a). On Tiger Lake and Skylake, a compute shader in Unicom that has a single instance of this pattern is hurt for spills and fills. I think this is just due to non-determinism in the register allocation algorithm. shader-db: All Intel platforms had similar results. (Lunar Lake shown) total instructions in shared programs: 16992643 -> 16992548 (<.01%) instructions in affected programs: 17533 -> 17438 (-0.54%) helped: 33 / HURT: 0 total cycles in shared programs: 914313986 -> 914316238 (<.01%) cycles in affected programs: 3734544 -> 3736796 (0.06%) helped: 26 / HURT: 6 fossil-db: Lunar Lake, Meteor Lake, DG2, and Ice Lake had similar results. (Lunar Lake shown) Totals: Instrs: 208952780 -> 208952537 (-0.00%) Send messages: 10934879 -> 10934875 (-0.00%) Cycle count: 30988230904 -> 30988228660 (-0.00%); split: -0.00%, +0.00% Spill count: 534864 -> 534843 (-0.00%) Fill count: 667081 -> 667068 (-0.00%) Max live registers: 65686656 -> 65686624 (-0.00%) Non SSA regs after NIR: 244185358 -> 244185335 (-0.00%) Totals from 3 (0.00% of 704834) affected shaders: Instrs: 4708 -> 4465 (-5.16%) Send messages: 234 -> 230 (-1.71%) Cycle count: 264382 -> 262138 (-0.85%); split: -0.88%, +0.03% Spill count: 91 -> 70 (-23.08%) Fill count: 73 -> 60 (-17.81%) Max live registers: 647 -> 615 (-4.95%) Non SSA regs after NIR: 3957 -> 3934 (-0.58%) Tiger Lake Totals: Instrs: 230516919 -> 230515185 (-0.00%); split: -0.00%, +0.00% Send messages: 12657684 -> 12657680 (-0.00%) Cycle count: 23060318600 -> 23060279758 (-0.00%); split: -0.00%, +0.00% Spill count: 548462 -> 548446 (-0.00%); split: -0.00%, +0.00% Fill count: 582304 -> 582294 (-0.00%); split: -0.00%, +0.00% Scratch Memory Size: 19538944 -> 19539968 (+0.01%) Max live registers: 41713622 -> 41713593 (-0.00%) Non SSA regs after NIR: 260667939 -> 260667712 (-0.00%); split: -0.00%, +0.00% Totals from 174 (0.02% of 794323) affected shaders: Instrs: 158346 -> 156612 (-1.10%); split: -1.13%, +0.04% Send messages: 14330 -> 14326 (-0.03%) Cycle count: 24859875 -> 24821033 (-0.16%); split: -0.32%, +0.16% Spill count: 183 -> 167 (-8.74%); split: -9.29%, +0.55% Fill count: 284 -> 274 (-3.52%); split: -7.39%, +3.87% Scratch Memory Size: 9216 -> 10240 (+11.11%) Max live registers: 12587 -> 12558 (-0.23%) Non SSA regs after NIR: 164466 -> 164239 (-0.14%); split: -0.16%, +0.02% Skylake Totals: Instrs: 158904982 -> 158903764 (-0.00%) Send messages: 8490500 -> 8490496 (-0.00%) Cycle count: 19732284279 -> 19732345496 (+0.00%); split: -0.00%, +0.00% Spill count: 519127 -> 519115 (-0.00%) Fill count: 594283 -> 594290 (+0.00%); split: -0.00%, +0.00% Max live registers: 33708764 -> 33708739 (-0.00%) Non SSA regs after NIR: 169377234 -> 169377007 (-0.00%); split: -0.00%, +0.00% Totals from 174 (0.03% of 648725) affected shaders: Instrs: 160391 -> 159173 (-0.76%) Send messages: 14354 -> 14350 (-0.03%) Cycle count: 24776486 -> 24837703 (+0.25%); split: -0.07%, +0.32% Spill count: 332 -> 320 (-3.61%) Fill count: 587 -> 594 (+1.19%); split: -0.17%, +1.36% Max live registers: 12709 -> 12684 (-0.20%) Non SSA regs after NIR: 166557 -> 166330 (-0.14%); split: -0.16%, +0.02% Reviewed-by: Rhys Perry Part-of: --- src/compiler/nir/nir_opt_algebraic.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/compiler/nir/nir_opt_algebraic.py b/src/compiler/nir/nir_opt_algebraic.py index 45cabe808ef..ac683671764 100644 --- a/src/compiler/nir/nir_opt_algebraic.py +++ b/src/compiler/nir/nir_opt_algebraic.py @@ -1580,6 +1580,15 @@ optimizations.extend([ # bfi is either b or c. (('bfi', ('ineg', ('b2i', 'a@1')), b, c), ('bcsel', a, b, c)), + # bfi(a, 0, 0) = ((0 << find_lsb(a)) & a) | (0 & ~a) + # = 0 + (('bfi', a, 0, 0), 0), + + # bfi(a, b, b) = ((b << find_lsb(a)) & a) | (b & ~a) + # = (a & b) | (b & ~a) If a is odd, find_lsb(a) == 0 + # = b + (('bfi', '#a(is_odd)', b, b), b), + # bfi(a, a, b) = ((a << find_lsb(a)) & a) | (b & ~a) # = (a & a) | (b & ~a) If a is odd, find_lsb(a) == 0 # = a | (b & ~a)