From ee5017a0fa9820142374035706af4b6a779f3c59 Mon Sep 17 00:00:00 2001 From: Georg Lehmann Date: Sun, 12 Jan 2025 17:25:15 +0100 Subject: [PATCH] nir/opt_algebaric: convert fadd(a, a) to a * 2.0 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit On AMD, this is a clear win. 2.0 is a free constant, the multiplication can be fused into fma, or it can be done as a free output modifier. Otherwise, fmul and fadd have the same throughput/latency, with the only possible downside being potentially power consumption. For other hardware this might not be as clear, but we should at least choose one option for NIR because it allows more CSE. Foz-DB Navi21: Totals from 12231 (15.41% of 79395) affected shaders: MaxWaves: 309068 -> 309032 (-0.01%) Instrs: 11826395 -> 11790132 (-0.31%); split: -0.31%, +0.00% CodeSize: 63531496 -> 63512868 (-0.03%); split: -0.03%, +0.00% VGPRs: 551256 -> 551328 (+0.01%); split: -0.00%, +0.02% SpillSGPRs: 984 -> 979 (-0.51%) Latency: 88486492 -> 88394296 (-0.10%); split: -0.11%, +0.01% InvThroughput: 22360595 -> 22300790 (-0.27%); split: -0.27%, +0.00% VClause: 226267 -> 226273 (+0.00%); split: -0.01%, +0.01% SClause: 293820 -> 293783 (-0.01%); split: -0.02%, +0.00% Copies: 727187 -> 727106 (-0.01%); split: -0.03%, +0.02% PreSGPRs: 539623 -> 539625 (+0.00%) PreVGPRs: 440843 -> 440946 (+0.02%); split: -0.00%, +0.03% VALU: 8324962 -> 8288809 (-0.43%); split: -0.43%, +0.00% SALU: 1278550 -> 1278538 (-0.00%); split: -0.00%, +0.00% VMEM: 440600 -> 440599 (-0.00%) Reviewed-by: Timur Kristóf Reviewed-by: Alyssa Rosenzweig Part-of: --- src/compiler/nir/nir_opt_algebraic.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/compiler/nir/nir_opt_algebraic.py b/src/compiler/nir/nir_opt_algebraic.py index d27fa05b6d0..7a9ae9b3eb4 100644 --- a/src/compiler/nir/nir_opt_algebraic.py +++ b/src/compiler/nir/nir_opt_algebraic.py @@ -242,6 +242,7 @@ optimizations = [ (('~fadd', ('fneg', a), ('fadd', a, b)), b), (('~fadd', a, ('fadd', ('fneg', a), b)), b), (('fadd', ('fsat', a), ('fsat', ('fneg', a))), ('fsat', ('fabs', a))), + (('fadd', a, a), ('fmul', a, 2.0)), (('~fmul', a, 0.0), 0.0), # The only effect a*0.0 should have is when 'a' is infinity, -0.0 or NaN (('fmul(nsz,nnan)', 'a', 0.0), 0.0),