From 98cc57bccb441a2d7adb93ffa13a06968d2ceec9 Mon Sep 17 00:00:00 2001 From: Georg Lehmann Date: Wed, 29 May 2024 17:13:01 +0200 Subject: [PATCH] nir/optimize cmp(a, -0.0) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit +0.0 can use an inline constant for AMD hardware, -0.0 needs a literal. Foz-DB Navi21: Totals from 1014 (1.28% of 79395) affected shaders: Instrs: 3037490 -> 3036849 (-0.02%); split: -0.02%, +0.00% CodeSize: 17060228 -> 17051276 (-0.05%); split: -0.05%, +0.00% Latency: 45916788 -> 45916600 (-0.00%); split: -0.00%, +0.00% InvThroughput: 12982201 -> 12982187 (-0.00%); split: -0.00%, +0.00% VClause: 79475 -> 79478 (+0.00%) SClause: 119935 -> 119934 (-0.00%); split: -0.00%, +0.00% Copies: 301641 -> 300964 (-0.22%); split: -0.23%, +0.00% PreSGPRs: 59155 -> 59144 (-0.02%) VALU: 2032016 -> 2032034 (+0.00%) SALU: 386424 -> 385729 (-0.18%) Reviewed-by: Timur Kristóf Reviewed-by: Faith Ekstrand Reviewed-by: Ian Romanick Part-of: --- src/compiler/nir/nir_opt_algebraic.py | 6 ++++++ src/compiler/nir/nir_search_helpers.h | 19 +++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/src/compiler/nir/nir_opt_algebraic.py b/src/compiler/nir/nir_opt_algebraic.py index e7957f730c6..d6919950dd5 100644 --- a/src/compiler/nir/nir_opt_algebraic.py +++ b/src/compiler/nir/nir_opt_algebraic.py @@ -608,6 +608,12 @@ optimizations.extend([ (('fge', -1.0, ('fneg', a)), ('fge', a, 1.0)), (('fneu', ('fneg', a), -1.0), ('fneu', 1.0, a)), (('feq', -1.0, ('fneg', a)), ('feq', a, 1.0)), + (('flt', a, '#b(is_negative_zero)'), ('flt', a, 0.0)), + (('flt', '#b(is_negative_zero)', a), ('flt', 0.0, a)), + (('fge', a, '#b(is_negative_zero)'), ('fge', a, 0.0)), + (('fge', '#b(is_negative_zero)', a), ('fge', 0.0, a)), + (('fneu', a, '#b(is_negative_zero)'), ('fneu', 0.0, a)), + (('feq', '#b(is_negative_zero)', a), ('feq', a, 0.0)), (('ieq', ('ineg', a), 0), ('ieq', a, 0)), (('ine', ('ineg', a), 0), ('ine', a, 0)), diff --git a/src/compiler/nir/nir_search_helpers.h b/src/compiler/nir/nir_search_helpers.h index 6a1740fbae8..44ede8b3ee6 100644 --- a/src/compiler/nir/nir_search_helpers.h +++ b/src/compiler/nir/nir_search_helpers.h @@ -29,6 +29,7 @@ #include #include "util/bitscan.h" +#include "util/u_math.h" #include "nir.h" #include "nir_range_analysis.h" @@ -127,6 +128,24 @@ is_nan(UNUSED struct hash_table *ht, const nir_alu_instr *instr, return true; } +static inline bool +is_negative_zero(UNUSED struct hash_table *ht, const nir_alu_instr *instr, + unsigned src, unsigned num_components, const uint8_t *swizzle) +{ + /* only constant srcs: */ + if (!nir_src_is_const(instr->src[src].src)) + return false; + + for (unsigned i = 0; i < num_components; i++) { + union di tmp; + tmp.d = nir_src_comp_as_float(instr->src[src].src, swizzle[i]); + if (tmp.ui != 0x8000000000000000ull) + return false; + } + + return true; +} + static inline bool is_any_comp_nan(UNUSED struct hash_table *ht, const nir_alu_instr *instr, unsigned src, unsigned num_components, const uint8_t *swizzle)