From 3b3c3ccf561294ef9beb8f8b03f0c388357ac17a Mon Sep 17 00:00:00 2001 From: Gert Wollny Date: Wed, 17 Sep 2025 15:29:49 +0200 Subject: [PATCH] nir+r600: add option to avoid contracting fabs into ffma On r600 ternary operations can't use the fabs source modifier, so converting "fadd(fabs(fmul(a, b), c)" to "ffma(fabs(a), fabs(b), c)" adds one more instruction in the backend, hence avoid this. Signed-off-by: Gert Wollny Part-of: --- src/compiler/nir/nir_opt_algebraic.py | 5 +++-- src/compiler/nir/nir_shader_compiler_options.h | 5 +++++ src/gallium/drivers/r600/r600_pipe_common.c | 1 + 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/compiler/nir/nir_opt_algebraic.py b/src/compiler/nir/nir_opt_algebraic.py index 3ca39ec745b..a6d488e426a 100644 --- a/src/compiler/nir/nir_opt_algebraic.py +++ b/src/compiler/nir/nir_opt_algebraic.py @@ -3650,6 +3650,7 @@ for sz, mulz in itertools.product([16, 32, 64], [False, True]): fadd = 'fadd@{}(contract)'.format(sz) option = 'options->fuse_ffma{}'.format(sz) + option_with_abs = 'options->fuse_ffma{} && !options->avoid_ternary_with_fabs'.format(sz) late_optimizations.extend([ ((fadd, (fmul, a, b), c), (ffma, a, b, c), option), @@ -3658,10 +3659,10 @@ for sz, mulz in itertools.product([16, 32, 64], [False, True]): (ffma, ('fneg', a), b, c), option), ((fadd, ('fabs(is_only_used_by_fadd)', (fmul, a, b)), c), - (ffma, ('fabs', a), ('fabs', b), c), option), + (ffma, ('fabs', a), ('fabs', b), c), option_with_abs), ((fadd, ('fneg(is_only_used_by_fadd)', ('fabs', (fmul, a, b))), c), - (ffma, ('fneg', ('fabs', a)), ('fabs', b), c), option), + (ffma, ('fneg', ('fabs', a)), ('fabs', b), c), option_with_abs), ]) late_optimizations.extend([ diff --git a/src/compiler/nir/nir_shader_compiler_options.h b/src/compiler/nir/nir_shader_compiler_options.h index 571345f3399..5a884f36b7b 100644 --- a/src/compiler/nir/nir_shader_compiler_options.h +++ b/src/compiler/nir/nir_shader_compiler_options.h @@ -665,6 +665,11 @@ typedef struct nir_shader_compiler_options { */ bool avoid_ternary_with_two_constants; + /* Avoid using fabs on sources to ternary operations + * r600 can't use source modifiers for these + */ + bool avoid_ternary_with_fabs; + /** Whether 8-bit ALU is supported. */ bool support_8bit_alu; diff --git a/src/gallium/drivers/r600/r600_pipe_common.c b/src/gallium/drivers/r600/r600_pipe_common.c index c1c51f755b6..85b49dd0bca 100644 --- a/src/gallium/drivers/r600/r600_pipe_common.c +++ b/src/gallium/drivers/r600/r600_pipe_common.c @@ -1166,6 +1166,7 @@ bool r600_common_screen_init(struct r600_common_screen *rscreen, .vectorize_tess_levels = 1, .io_options = nir_io_mediump_is_32bit, .vertex_id_zero_based = rscreen->info.gfx_level >= EVERGREEN, + .avoid_ternary_with_fabs = 1, }; rscreen->nir_options = nir_options;