From a7d25702964835ba70f7ba1b7cf03cfb379666ea Mon Sep 17 00:00:00 2001 From: Christian Gmeiner Date: Tue, 16 Sep 2025 23:42:50 +0200 Subject: [PATCH] nir/opt_algebraic: optimize f2i32(fround_even(x)) to f2i32_rtne(x) Add late optimization to fuse f2i32 and fround_even operations into a single f2i32_rtne instruction when the intermediate fround_even result is only used once. This eliminates redundant rounding since f2i32_rtne performs round-to-nearest-even conversion directly. Signed-off-by: Christian Gmeiner Tested-by: Simon Perretta Part-of: --- src/compiler/nir/nir_opt_algebraic.py | 4 ++++ src/compiler/nir/nir_shader_compiler_options.h | 3 +++ 2 files changed, 7 insertions(+) diff --git a/src/compiler/nir/nir_opt_algebraic.py b/src/compiler/nir/nir_opt_algebraic.py index bca31e57ea2..3ca39ec745b 100644 --- a/src/compiler/nir/nir_opt_algebraic.py +++ b/src/compiler/nir/nir_opt_algebraic.py @@ -4024,6 +4024,10 @@ for s in [16, 32, 64]: (('bcsel@{}'.format(s), ('ine', 0, 'a@{}'.format(s)), 'b@{}'.format(s), 'c@{}'.format(s)), ('icsel_eqz', a, c, b), "options->has_icsel_eqz{} && !options->no_integers".format(s)), ]) +late_optimizations += [ + (('f2i32', ('fround_even(is_used_once)', 'a@32')), ('f2i32_rtne', a), 'options->has_f2i32_rtne'), +] + distribute_src_mods = [ # Try to remove some spurious negations rather than pushing them down. (('fmul', ('fneg', a), ('fneg', b)), ('fmul', a, b)), diff --git a/src/compiler/nir/nir_shader_compiler_options.h b/src/compiler/nir/nir_shader_compiler_options.h index f97df5ac67f..571345f3399 100644 --- a/src/compiler/nir/nir_shader_compiler_options.h +++ b/src/compiler/nir/nir_shader_compiler_options.h @@ -646,6 +646,9 @@ typedef struct nir_shader_compiler_options { /** Backend supports load_global_bounded intrinsics. */ bool has_load_global_bounded; + /** Backend supports f2i32_rtne opcode. */ + bool has_f2i32_rtne; + /** * Is this the Intel vec4 backend? *