From cff106c4b690c6bfe8015e5bd457a17fe3482275 Mon Sep 17 00:00:00 2001 From: Samuel Pitoiset Date: Tue, 24 Aug 2021 08:35:59 +0200 Subject: [PATCH] nir/opt_algebraic: optimize fmax(-fmin(b, a), b) -> fmax(fabs(b), -a) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit and fmin(-fmax(b, a)) to fmin(-fabs(b), -a). fossils-db (Sienna Cichlid): Totals from 34 (0.02% of 150170) affected shaders: CodeSize: 388540 -> 387748 (-0.20%) Instrs: 74621 -> 74423 (-0.27%) Latency: 1039407 -> 1039011 (-0.04%) InvThroughput: 208364 -> 208150 (-0.10%) Signed-off-by: Samuel Pitoiset Reviewed-by: Daniel Schürmann Reviewed-by: Ian Romanick Part-of: --- src/compiler/nir/nir_opt_algebraic.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/compiler/nir/nir_opt_algebraic.py b/src/compiler/nir/nir_opt_algebraic.py index bb07031e615..140b92c2dec 100644 --- a/src/compiler/nir/nir_opt_algebraic.py +++ b/src/compiler/nir/nir_opt_algebraic.py @@ -717,6 +717,11 @@ optimizations.extend([ # fmin(0.0, b)) while the right one is "b", so this optimization is inexact. (('~fmin', ('fsat', a), '#b(is_zero_to_one)'), ('fsat', ('fmin', a, b))), + # max(-min(b, a), b) -> max(abs(b), -a) + # min(-max(b, a), b) -> min(-abs(b), -a) + (('fmax', ('fneg', ('fmin', b, a)), b), ('fmax', ('fabs', b), ('fneg', a))), + (('fmin', ('fneg', ('fmax', b, a)), b), ('fmin', ('fneg', ('fabs', b)), ('fneg', a))), + # If a in [0,b] then b-a is also in [0,b]. Since b in [0,1], max(b-a, 0) = # fsat(b-a). #