From 77d4ed0a0159a15cc6fcc3fd2dc0bfa9c55562be Mon Sep 17 00:00:00 2001 From: Alyssa Rosenzweig Date: Thu, 28 Nov 2024 17:39:44 -0500 Subject: [PATCH] nir/opt_algebraic: optimize sign bit manipulation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit libclc loves to generate the iand(0x7fffffff) pattern. ior/ixor patterns are added for completeness. Shaves 4 instructions off libclc vec4 normalize. v2: Loop over the bit sizes (Georg). Signed-off-by: Alyssa Rosenzweig Reviewed-by: Marek Olšák [v1] Reviewed-by: Georg Lehmann Part-of: --- src/compiler/nir/nir_opt_algebraic.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/compiler/nir/nir_opt_algebraic.py b/src/compiler/nir/nir_opt_algebraic.py index 27bc76da7f4..45cabe808ef 100644 --- a/src/compiler/nir/nir_opt_algebraic.py +++ b/src/compiler/nir/nir_opt_algebraic.py @@ -352,6 +352,18 @@ optimizations = [ ('fexp2', ('fmulz', a, 'mb'))), {'mb': b}), ] +# Bitwise operations affecting the sign may be replaced by equivalent +# floating point operations, except possibly for denormal +# behaviour hence the is_only_used_as_float. +for sz in (16, 32, 64): + sign_bit = 1 << (sz - 1) + + optimizations.extend([ + (('iand(is_only_used_as_float)', f'a@{sz}', sign_bit - 1), ('fabs', a)), + (('ixor(is_only_used_as_float)', f'a@{sz}', sign_bit), ('fneg', a)), + (('ior(is_only_used_as_float)', f'a@{sz}', sign_bit), ('fneg', ('fabs', a))), + ]) + # Shorthand for the expansion of just the dot product part of the [iu]dp4a # instructions. sdot_4x8_a_b = ('iadd', ('iadd', ('imul', ('extract_i8', a, 0), ('extract_i8', b, 0)),