From c78f1d516c8889afb3e77ba2d7ad2ceac704caf3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Sch=C3=BCrmann?= Date: Thu, 4 Sep 2025 11:55:50 +0200 Subject: [PATCH] nir/algebraic: add pattern for (a << #b) * #c => a * (#c << #b) Totals from 2545 (3.19% of 79839) affected shaders: (Navi48) Instrs: 6371003 -> 6364130 (-0.11%); split: -0.12%, +0.01% CodeSize: 33827548 -> 33812244 (-0.05%); split: -0.06%, +0.01% Latency: 47451755 -> 47430108 (-0.05%); split: -0.05%, +0.00% InvThroughput: 10442450 -> 10437159 (-0.05%); split: -0.05%, +0.00% SClause: 159829 -> 159874 (+0.03%); split: -0.01%, +0.04% Copies: 500725 -> 500721 (-0.00%); split: -0.01%, +0.01% PreSGPRs: 110482 -> 110478 (-0.00%); split: -0.00%, +0.00% PreVGPRs: 147289 -> 147287 (-0.00%); split: -0.00%, +0.00% VALU: 3456135 -> 3454241 (-0.05%); split: -0.06%, +0.01% SALU: 925982 -> 923616 (-0.26%) VOPD: 1243 -> 1212 (-2.49%) Part-of: --- src/compiler/nir/nir_opt_algebraic.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/compiler/nir/nir_opt_algebraic.py b/src/compiler/nir/nir_opt_algebraic.py index 3827ce40a44..5b3b265ab00 100644 --- a/src/compiler/nir/nir_opt_algebraic.py +++ b/src/compiler/nir/nir_opt_algebraic.py @@ -522,9 +522,10 @@ optimizations.extend([ (('ishl', ('iadd', ('imul', a, '#b'), '#c'), '#d'), ('iadd', ('imul', a, ('ishl', b, d)), ('ishl', c, d))), - # (a * #b) << #c - # a * (#b << #c) + # (a * #b) << #c => a * (#b << #c) (('ishl', ('imul', a, '#b'), '#c'), ('imul', a, ('ishl', b, c))), + # (a << #b) * #c => a * (#c << #b) + (('imul', ('ishl', a, '#b'), '#c'), ('imul', a, ('ishl', c, b))), ]) # Care must be taken here. Shifts in NIR uses only the lower log2(bitsize)