From 81925d7f41589738bf716ad83139ca092d98a2a0 Mon Sep 17 00:00:00 2001 From: Rhys Perry Date: Wed, 13 May 2026 10:07:41 +0100 Subject: [PATCH] nir/algebraic: optimize ishl(iadd(iadd(iadd(a, #b), c), d), #e) This improves combining of constants offsets into memory accesses in dEQP-VK.compute.pipeline.cooperative_matrix.khr_a.subgroupscope.mul.float16_float16.buffer.colmajor.linear fossil-db (gfx1201): Totals from 121 (0.06% of 208640) affected shaders: Instrs: 204278 -> 204199 (-0.04%); split: -0.06%, +0.03% CodeSize: 1110856 -> 1110076 (-0.07%); split: -0.10%, +0.03% VGPRs: 7620 -> 7680 (+0.79%); split: -0.16%, +0.94% Latency: 1225169 -> 1225067 (-0.01%); split: -0.02%, +0.01% InvThroughput: 191629 -> 191580 (-0.03%); split: -0.03%, +0.01% SClause: 5732 -> 5731 (-0.02%) Copies: 16358 -> 16356 (-0.01%); split: -0.02%, +0.01% PreSGPRs: 5715 -> 5711 (-0.07%) PreVGPRs: 5907 -> 5905 (-0.03%) VALU: 112808 -> 112742 (-0.06%); split: -0.06%, +0.00% SALU: 27121 -> 27113 (-0.03%) fossil-db (gfx1201, dEQP-VK.compute.pipeline.cooperative_matrix.*): Totals from 198 (12.20% of 1623) affected shaders: Instrs: 13011 -> 11584 (-10.97%) CodeSize: 90188 -> 77920 (-13.60%) VGPRs: 3456 -> 2724 (-21.18%) Latency: 144421 -> 142553 (-1.29%) InvThroughput: 11158 -> 10608 (-4.93%) Copies: 1119 -> 1117 (-0.18%) PreSGPRs: 1954 -> 1857 (-4.96%) PreVGPRs: 1675 -> 1354 (-19.16%) VALU: 4894 -> 3476 (-28.97%) Signed-off-by: Rhys Perry Reviewed-by: Georg Lehmann Part-of: --- src/compiler/nir/nir_opt_algebraic.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/compiler/nir/nir_opt_algebraic.py b/src/compiler/nir/nir_opt_algebraic.py index def08e7ada3..a8f71b8bc90 100644 --- a/src/compiler/nir/nir_opt_algebraic.py +++ b/src/compiler/nir/nir_opt_algebraic.py @@ -721,6 +721,8 @@ optimizations.extend([ (('ishl', ('iadd', a, '#b'), '#c'), ('iadd', ('ishl', a, c), ('ishl', b, c))), (('ishl', ('iadd(is_used_once)', ('iadd', a, '#b'), c), '#d'), ('iadd', ('ishl', ('iadd', a, c), d), ('ishl', b, d))), + (('ishl', ('iadd(is_used_once)', ('iadd(is_used_once)', ('iadd', a, '#b'), c), d), '#e'), + ('iadd', ('ishl', ('iadd', ('iadd', a, c), d), e), ('ishl', b, e))), # (a + #b) * #c => (a * #c) + (#b * #c) (('imul', ('iadd(is_used_once)', a, '#b'), '#c'), ('iadd', ('imul', a, c), ('imul', b, c))),