From 7f97fa6df01bc3d707d4e39885e4d6b577537ad5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Iv=C3=A1n=20Briano?= Date: Mon, 12 Feb 2024 17:28:38 -0800 Subject: [PATCH] nir/algebraic: move float control conditions to be per instruction Reviewed-by: Alyssa Rosenzweig Part-of: --- src/compiler/nir/nir_opt_algebraic.py | 55 ++++++++++----------------- 1 file changed, 21 insertions(+), 34 deletions(-) diff --git a/src/compiler/nir/nir_opt_algebraic.py b/src/compiler/nir/nir_opt_algebraic.py index 71aa49817fa..20e3836ff58 100644 --- a/src/compiler/nir/nir_opt_algebraic.py +++ b/src/compiler/nir/nir_opt_algebraic.py @@ -37,15 +37,6 @@ d = 'd' e = 'e' NAN = math.nan -signed_zero_preserve_16 = 'nir_is_float_control_signed_zero_preserve(info->float_controls_execution_mode, 16)' -signed_zero_preserve_32 = 'nir_is_float_control_signed_zero_preserve(info->float_controls_execution_mode, 32)' -signed_zero_nan_preserve_16 = ('(nir_is_float_control_signed_zero_preserve(info->float_controls_execution_mode, 16) ||' - ' nir_is_float_control_nan_preserve(info->float_controls_execution_mode, 16))') -signed_zero_nan_preserve_32 = ('(nir_is_float_control_signed_zero_preserve(info->float_controls_execution_mode, 32) ||' - ' nir_is_float_control_nan_preserve(info->float_controls_execution_mode, 32))') -signed_zero_inf_nan_preserve_16 = 'nir_is_float_control_signed_zero_inf_nan_preserve(info->float_controls_execution_mode, 16)' -signed_zero_inf_nan_preserve_32 = 'nir_is_float_control_signed_zero_inf_nan_preserve(info->float_controls_execution_mode, 32)' - has_fmulz = '(options->has_fmulz || \ (options->has_fmulz_no_denorms && \ !nir_is_denorm_preserve(info->float_controls_execution_mode, 32)))' @@ -159,8 +150,7 @@ optimizations = [ # a+0.0 is 'a' unless 'a' is denormal or -0.0. If it's only used by a # floating point instruction, they should flush any input denormals and we # can replace -0.0 with 0.0 if the float execution mode allows it. - (('fadd(is_only_used_as_float)', 'a@16', 0.0), a, '!'+signed_zero_preserve_16), - (('fadd(is_only_used_as_float)', 'a@32', 0.0), a, '!'+signed_zero_preserve_32), + (('fadd(is_only_used_as_float,nsz)', 'a', 0.0), a), (('iadd', a, 0), a), (('iadd_sat', a, 0), a), (('isub_sat', a, 0), a), @@ -195,13 +185,12 @@ optimizations = [ (('fadd', ('fsat', a), ('fsat', ('fneg', a))), ('fsat', ('fabs', a))), (('~fmul', a, 0.0), 0.0), # The only effect a*0.0 should have is when 'a' is infinity, -0.0 or NaN - (('fmul', 'a@16', 0.0), 0.0, '!'+signed_zero_nan_preserve_16), - (('fmul', 'a@32', 0.0), 0.0, '!'+signed_zero_nan_preserve_32), + (('fmul(nsz,nnan)', 'a', 0.0), 0.0), (('fmulz', a, 0.0), 0.0), - (('fmulz', a, 'b(is_finite_not_zero)'), ('fmul', a, b), '!'+signed_zero_preserve_32), + (('fmulz(nsz)', a, 'b(is_finite_not_zero)'), ('fmul', a, b)), (('fmulz', 'a(is_finite)', 'b(is_finite)'), ('fmul', a, b)), (('fmulz', a, a), ('fmul', a, a)), - (('ffmaz', a, 'b(is_finite_not_zero)', c), ('ffma', a, b, c), '!'+signed_zero_preserve_32), + (('ffmaz(nsz)', a, 'b(is_finite_not_zero)', c), ('ffma', a, b, c)), (('ffmaz', 'a(is_finite)', 'b(is_finite)', c), ('ffma', a, b, c)), (('ffmaz', a, a, b), ('ffma', a, a, b)), (('imul', a, 0), 0), @@ -223,17 +212,15 @@ optimizations = [ (('fmul', ('fsign', a), ('fmul', a, a)), ('fmul', ('fabs', a), a)), (('fmul', ('fmul', ('fsign', a), a), a), ('fmul', ('fabs', a), a)), (('~ffma', 0.0, a, b), b), - (('ffma@16(is_only_used_as_float)', 0.0, a, b), b, '!'+signed_zero_inf_nan_preserve_16), - (('ffma@32(is_only_used_as_float)', 0.0, a, b), b, '!'+signed_zero_inf_nan_preserve_32), + (('ffma(is_only_used_as_float,nsz,nnan,ninf)', 0.0, a, b), b), (('ffmaz', 0.0, a, b), ('fadd', 0.0, b)), (('~ffma', a, b, 0.0), ('fmul', a, b)), - (('ffma@16', a, b, 0.0), ('fmul', a, b), '!'+signed_zero_preserve_16), - (('ffma@32', a, b, 0.0), ('fmul', a, b), '!'+signed_zero_preserve_32), - (('ffmaz', a, b, 0.0), ('fmulz', a, b), '!'+signed_zero_preserve_32), + (('ffma(nsz)', a, b, 0.0), ('fmul', a, b)), + (('ffmaz(nsz)', a, b, 0.0), ('fmulz', a, b)), (('ffma', 1.0, a, b), ('fadd', a, b)), - (('ffmaz', 1.0, a, b), ('fadd', a, b), '!'+signed_zero_preserve_32), + (('ffmaz(nsz)', 1.0, a, b), ('fadd', a, b)), (('ffma', -1.0, a, b), ('fadd', ('fneg', a), b)), - (('ffmaz', -1.0, a, b), ('fadd', ('fneg', a), b), '!'+signed_zero_preserve_32), + (('ffmaz(nsz)', -1.0, a, b), ('fadd', ('fneg', a), b)), (('~ffma', '#a', '#b', c), ('fadd', ('fmul', a, b), c)), (('~ffmaz', '#a', '#b', c), ('fadd', ('fmulz', a, b), c)), (('~flrp', a, b, 0.0), a), @@ -287,21 +274,21 @@ optimizations = [ # Optimize open-coded fmulz. # (b==0.0 ? 0.0 : a) * (a==0.0 ? 0.0 : b) -> fmulz(a, b) - (('fmul@32', ('bcsel', ignore_exact('feq', b, 0.0), 0.0, a), ('bcsel', ignore_exact('feq', a, 0.0), 0.0, b)), - ('fmulz', a, b), has_fmulz+' && !'+signed_zero_preserve_32), - (('fmul@32', a, ('bcsel', ignore_exact('feq', a, 0.0), 0.0, '#b(is_not_const_zero)')), - ('fmulz', a, b), has_fmulz+' && !'+signed_zero_preserve_32), + (('fmul@32(nsz)', ('bcsel', ignore_exact('feq', b, 0.0), 0.0, a), ('bcsel', ignore_exact('feq', a, 0.0), 0.0, b)), + ('fmulz', a, b), has_fmulz), + (('fmul@32(nsz)', a, ('bcsel', ignore_exact('feq', a, 0.0), 0.0, '#b(is_not_const_zero)')), + ('fmulz', a, b), has_fmulz), # ffma(b==0.0 ? 0.0 : a, a==0.0 ? 0.0 : b, c) -> ffmaz(a, b, c) - (('ffma@32', ('bcsel', ignore_exact('feq', b, 0.0), 0.0, a), ('bcsel', ignore_exact('feq', a, 0.0), 0.0, b), c), - ('ffmaz', a, b, c), has_fmulz+' && !'+signed_zero_preserve_32), - (('ffma@32', a, ('bcsel', ignore_exact('feq', a, 0.0), 0.0, '#b(is_not_const_zero)'), c), - ('ffmaz', a, b, c), has_fmulz+' && !'+signed_zero_preserve_32), + (('ffma@32(nsz)', ('bcsel', ignore_exact('feq', b, 0.0), 0.0, a), ('bcsel', ignore_exact('feq', a, 0.0), 0.0, b), c), + ('ffmaz', a, b, c), has_fmulz), + (('ffma@32(nsz)', a, ('bcsel', ignore_exact('feq', a, 0.0), 0.0, '#b(is_not_const_zero)'), c), + ('ffmaz', a, b, c), has_fmulz), # b == 0.0 ? 1.0 : fexp2(fmul(a, b)) -> fexp2(fmulz(a, b)) - (('bcsel', ignore_exact('feq', b, 0.0), 1.0, ('fexp2', ('fmul@32', a, b))), + (('bcsel(nsz,nnan,ninf)', ignore_exact('feq', b, 0.0), 1.0, ('fexp2', ('fmul@32', a, b))), ('fexp2', ('fmulz', a, b)), - has_fmulz+' && !'+signed_zero_inf_nan_preserve_32), + has_fmulz), ] # Shorthand for the expansion of just the dot product part of the [iu]dp4a @@ -845,7 +832,7 @@ optimizations.extend([ (('fsat', ('fsat', a)), ('fsat', a)), (('fsat', ('fneg(is_used_once)', ('fadd(is_used_once)', a, b))), ('fsat', ('fadd', ('fneg', a), ('fneg', b))), '!options->lower_fsat'), (('fsat', ('fneg(is_used_once)', ('fmul(is_used_once)', a, b))), ('fsat', ('fmul', ('fneg', a), b)), '!options->lower_fsat'), - (('fsat', ('fneg(is_used_once)', ('fmulz(is_used_once)', a, b))), ('fsat', ('fmulz', ('fneg', a), b)), '!options->lower_fsat && !'+signed_zero_preserve_32), + (('fsat(nsz)', ('fneg(is_used_once)', ('fmulz(is_used_once)', a, b))), ('fsat', ('fmulz', ('fneg', a), b)), '!options->lower_fsat'), (('fsat', ('fabs(is_used_once)', ('fmul(is_used_once)', a, b))), ('fsat', ('fmul', ('fabs', a), ('fabs', b))), '!options->lower_fsat'), (('fmin', ('fmax', ('fmin', ('fmax', a, b), c), b), c), ('fmin', ('fmax', a, b), c)), (('imin', ('imax', ('imin', ('imax', a, b), c), b), c), ('imin', ('imax', a, b), c)), @@ -1858,7 +1845,7 @@ optimizations.extend([ # Propagate negation up multiplication chains (('fmul(is_used_by_non_fsat)', ('fneg', a), b), ('fneg', ('fmul', a, b))), - (('fmulz(is_used_by_non_fsat)', ('fneg', a), b), ('fneg', ('fmulz', a, b)), '!'+signed_zero_preserve_32), + (('fmulz(is_used_by_non_fsat,nsz)', ('fneg', a), b), ('fneg', ('fmulz', a, b))), (('ffma', ('fneg', a), ('fneg', b), c), ('ffma', a, b, c)), (('ffmaz', ('fneg', a), ('fneg', b), c), ('ffmaz', a, b, c)), (('imul', ('ineg', a), b), ('ineg', ('imul', a, b))),