From d1dd65d4255ed564ea0851502135e3027df3ef3b Mon Sep 17 00:00:00 2001 From: Brandon Jones Date: Fri, 17 Apr 2026 09:07:43 -0700 Subject: [PATCH] nir/opt_algebraic: fix fabs optimization This fixes a regression found in blender's unit testing, which called fabs(-0.0) and invoked an NIR optimization that is was not valid for the parameter -0.0. IEEE 754 requires that abs clear the sign bit for the value -0.0. Reviewed-by: Georg Lehmann Part-of: --- src/compiler/nir/nir_opt_algebraic.py | 3 ++- src/compiler/nir/nir_search_helpers.h | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/compiler/nir/nir_opt_algebraic.py b/src/compiler/nir/nir_opt_algebraic.py index 90fabd487ba..b065ca59181 100644 --- a/src/compiler/nir/nir_opt_algebraic.py +++ b/src/compiler/nir/nir_opt_algebraic.py @@ -1943,7 +1943,8 @@ optimizations.extend([ # fract(x) = x - floor(x), so fract(NaN/Inf) = NaN (('ffract(nnan)', 'a(is_integral)'), 0.0), (('ffract', ('ffract', a)), ('ffract', a)), - (('fabs', 'a(is_not_negative)'), ('fcanonicalize', a)), + (('fabs(nsz)', 'a(is_not_negative)'), ('fcanonicalize', a)), + (('fabs', 'a(is_not_negative_or_negative_zero)'), ('fcanonicalize', a)), (('fabs(nsz)', 'a(is_not_positive)'), ('fneg', a)), (('fneu', 'a(is_not_zero)', 0.0), True), diff --git a/src/compiler/nir/nir_search_helpers.h b/src/compiler/nir/nir_search_helpers.h index be838f4a35b..5a86c73418e 100644 --- a/src/compiler/nir/nir_search_helpers.h +++ b/src/compiler/nir/nir_search_helpers.h @@ -891,6 +891,7 @@ RELATION_AND_NUM(lt_zero, FP_CLASS_ANY_POS | FP_CLASS_ANY_ZERO) RELATION_AND_NUM(not_positive, FP_CLASS_ANY_POS) RELATION_AND_NUM(gt_zero, FP_CLASS_ANY_NEG | FP_CLASS_ANY_ZERO) RELATION_AND_NUM(not_negative, FP_CLASS_ANY_NEG) +RELATION_AND_NUM(not_negative_or_negative_zero, FP_CLASS_ANY_NEG | FP_CLASS_NEG_ZERO) RELATION_AND_NUM(not_zero, FP_CLASS_ANY_ZERO) RELATION_AND_NUM(zero_to_one, FP_CLASS_ANY_NEG | FP_CLASS_GT_POS_ONE | FP_CLASS_POS_INF) RELATION_AND_NUM(le_pos_one, FP_CLASS_GT_POS_ONE | FP_CLASS_POS_INF)