From e6134c388dd76088ba813b23df73eec36fa0895a Mon Sep 17 00:00:00 2001 From: Georg Lehmann Date: Sat, 22 Mar 2025 16:40:33 +0100 Subject: [PATCH] nir/opt_algebraic: disable fsat(a + 1.0) opt if a can be NaN Foz-DB Navi21: Totals from 9 (0.01% of 79789) affected shaders: Instrs: 6782 -> 6796 (+0.21%); split: -0.03%, +0.24% CodeSize: 40020 -> 40108 (+0.22%); split: -0.04%, +0.26% Latency: 23764 -> 23758 (-0.03%) InvThroughput: 6424 -> 6431 (+0.11%); split: -0.08%, +0.19% SClause: 273 -> 275 (+0.73%) Copies: 338 -> 339 (+0.30%) VALU: 5138 -> 5147 (+0.18%); split: -0.06%, +0.23% SALU: 349 -> 350 (+0.29%) SMEM: 498 -> 500 (+0.40%) Fixes: a4a3487aae9 ("nir/opt_algebraic: optimize patterns from Skia") Reviewed-by: Alyssa Rosenzweig Part-of: (cherry picked from commit 3e26fc4498948e3a320469ff12be80b9ba5005a7) --- .pick_status.json | 2 +- src/compiler/nir/nir_opt_algebraic.py | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/.pick_status.json b/.pick_status.json index 3217d39d82e..a30dcbadce3 100644 --- a/.pick_status.json +++ b/.pick_status.json @@ -94,7 +94,7 @@ "description": "nir/opt_algebraic: disable fsat(a + 1.0) opt if a can be NaN", "nominated": true, "nomination_type": 2, - "resolution": 0, + "resolution": 1, "main_sha": null, "because_sha": "a4a3487aae98cc83990b1c79785983b65124145f", "notes": null diff --git a/src/compiler/nir/nir_opt_algebraic.py b/src/compiler/nir/nir_opt_algebraic.py index 7a9ae9b3eb4..77716f7542e 100644 --- a/src/compiler/nir/nir_opt_algebraic.py +++ b/src/compiler/nir/nir_opt_algebraic.py @@ -980,7 +980,9 @@ optimizations.extend([ (('~fmin', ('fsat', a), '#b(is_zero_to_one)'), ('fsat', ('fmin', a, b))), # If a >= 0 ... 1 + a >= 1 ... so fsat(1 + a) = 1 - (('fsat', ('fadd', 1.0, 'a(is_ge_zero)')), 1.0), + # But 1 + NaN is NaN and fsat(NaN) = 0. + (('~fsat', ('fadd', 1.0, 'a(is_not_negative)')), 1.0), + (('fsat', ('fadd', 1.0, 'a(is_a_number_not_negative)')), 1.0), # Let constant folding do its job. This can have emergent behaviour. (('fneg', ('bcsel(is_used_once)', a, '#b', '#c')), ('bcsel', a, ('fneg', b), ('fneg', c))),