From 4b81cb62064bddb78e3fee27a7d49f51eeb51068 Mon Sep 17 00:00:00 2001 From: Alyssa Rosenzweig Date: Tue, 14 Apr 2026 12:04:23 -0400 Subject: [PATCH] nir/opt_generate_bfi: avoid trivial instructions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit With the pass order shuffling, code like `(x & 0xf) + (x & 0xfffffff0)` gets optimized to bitfield_select(0xF, x, x). But it would be much better to optimize simply to x. nir_opt_algebraic would do that for us but we run this pass too late for algebraic to save us from ourselves, so be smarter. Observed on dEQP-GLES31.functional.compute.basic.image_atomic_op_local_size_8 with Jay, this saves an instruction there. Signed-off-by: Alyssa Rosenzweig Reviewed-by: Daniel Schürmann Reviewed-by: Ian Romanick Part-of: --- src/compiler/nir/nir_opt_generate_bfi.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/compiler/nir/nir_opt_generate_bfi.c b/src/compiler/nir/nir_opt_generate_bfi.c index 63289d9a05b..6581eee7f0d 100644 --- a/src/compiler/nir/nir_opt_generate_bfi.c +++ b/src/compiler/nir/nir_opt_generate_bfi.c @@ -146,6 +146,14 @@ nir_opt_generate_bfi_instr(nir_builder *b, b->cursor = nir_before_instr(&alu->instr); + /* Optimize with the rule bitfield_select(a, b, b) = b. We do this ourselves + * since we run late after nir_opt_algebraic. + */ + if (alu->def.num_components == 1 && nir_scalar_equal(insert[0], base[0])) { + nir_def_replace(&alu->def, nir_mov_scalar(b, insert[0])); + return true; + } + nir_def *mask_vec = nir_build_imm(b, alu->def.num_components, alu->def.bit_size, mask_cvals); nir_def *insert_vec = nir_vec_scalars(b, insert, alu->def.num_components); nir_def *base_vec = nir_vec_scalars(b, base, alu->def.num_components);