From 82501c380c196b977d36b72bb5a0fda1841f56e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Sch=C3=BCrmann?= Date: Wed, 29 Apr 2026 19:29:51 +0200 Subject: [PATCH 1/4] nir/builder: constant-fold nir_mov_alu() if requested --- src/compiler/nir/nir_builder.h | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/compiler/nir/nir_builder.h b/src/compiler/nir/nir_builder.h index 51f0aadfb07..e81a15b8488 100644 --- a/src/compiler/nir/nir_builder.h +++ b/src/compiler/nir/nir_builder.h @@ -718,6 +718,17 @@ nir_mov_alu(nir_builder *build, nir_alu_src src, unsigned num_components) return src.src.ssa; } + if (build->constant_fold_alu && nir_src_is_const(src.src)) { + nir_const_value dest[NIR_MAX_VEC_COMPONENTS]; + nir_load_const_instr *load_const = nir_src_as_load_const(src.src); + for (unsigned i = 0; i < num_components; i++) + dest[i] = load_const->value[src.swizzle[i]]; + + return nir_build_imm(build, num_components, + nir_src_bit_size(src.src), + dest); + } + nir_alu_instr *mov = nir_alu_instr_create(build->shader, nir_op_mov); nir_def_init(&mov->instr, &mov->def, num_components, nir_src_bit_size(src.src)); From b0665d19262a5ff96c44ecbeeeca3518a4539398 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Sch=C3=BCrmann?= Date: Wed, 29 Apr 2026 15:32:38 +0200 Subject: [PATCH 2/4] nir/lower_bit_size: use nir_builder::constant_fold_alu --- src/amd/vulkan/radv_pipeline.c | 4 +--- src/compiler/nir/nir_lower_bit_size.c | 1 + 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/amd/vulkan/radv_pipeline.c b/src/amd/vulkan/radv_pipeline.c index e908efe8077..530442d3927 100644 --- a/src/amd/vulkan/radv_pipeline.c +++ b/src/amd/vulkan/radv_pipeline.c @@ -556,9 +556,7 @@ radv_postprocess_nir(const struct radv_compiler_info *compiler_info, const struc if (gfx_level >= GFX8) nir_divergence_analysis(stage->nir); - if (nir_lower_bit_size(stage->nir, ac_nir_lower_bit_size_callback, &gfx_level)) { - NIR_PASS(_, stage->nir, nir_opt_constant_folding); - } + NIR_PASS(_, stage->nir, nir_lower_bit_size, ac_nir_lower_bit_size_callback, &gfx_level); } if (gfx_level >= GFX9) { bool separate_g16 = gfx_level >= GFX10; diff --git a/src/compiler/nir/nir_lower_bit_size.c b/src/compiler/nir/nir_lower_bit_size.c index 9f887bb2d0e..142672d0450 100644 --- a/src/compiler/nir/nir_lower_bit_size.c +++ b/src/compiler/nir/nir_lower_bit_size.c @@ -323,6 +323,7 @@ lower_impl(nir_function_impl *impl, void *callback_data) { nir_builder b = nir_builder_create(impl); + b.constant_fold_alu = true; bool progress = false; nir_foreach_block(block, impl) { From e7f7eaa2d51be2503423abe5d3dcb1fa3e01a31b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Sch=C3=BCrmann?= Date: Thu, 30 Apr 2026 10:16:52 +0200 Subject: [PATCH 3/4] nir/lower_bit_size: use nir_def_replace() instead of nir_def_rewrite_uses() --- src/compiler/nir/nir_lower_bit_size.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/compiler/nir/nir_lower_bit_size.c b/src/compiler/nir/nir_lower_bit_size.c index 142672d0450..a9c9f9d21fb 100644 --- a/src/compiler/nir/nir_lower_bit_size.c +++ b/src/compiler/nir/nir_lower_bit_size.c @@ -168,9 +168,9 @@ lower_alu_instr(nir_builder *bld, nir_alu_instr *alu, unsigned bit_size) dst_bit_size != bit_size) { nir_alu_type type = nir_op_infos[op].output_type; nir_def *dst = nir_convert_to_bit_size(bld, lowered_dst, type, dst_bit_size); - nir_def_rewrite_uses(&alu->def, dst); + nir_def_replace(&alu->def, dst); } else { - nir_def_rewrite_uses(&alu->def, lowered_dst); + nir_def_replace(&alu->def, lowered_dst); } } @@ -268,7 +268,7 @@ lower_intrinsic_instr(nir_builder *b, nir_intrinsic_instr *intrin, res = nir_convert_to_bit_size(b, res, type, old_bit_size); - nir_def_rewrite_uses(&intrin->def, res); + nir_def_replace(&intrin->def, res); break; } From 3e671d9670431fa492329b6ceca88fc332797fa0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Sch=C3=BCrmann?= Date: Thu, 30 Apr 2026 10:30:49 +0200 Subject: [PATCH 4/4] nir/lower_bit_size: skip conversion for more opcodes --- src/compiler/nir/nir_lower_bit_size.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/compiler/nir/nir_lower_bit_size.c b/src/compiler/nir/nir_lower_bit_size.c index a9c9f9d21fb..0cfbfbd2c33 100644 --- a/src/compiler/nir/nir_lower_bit_size.c +++ b/src/compiler/nir/nir_lower_bit_size.c @@ -62,6 +62,9 @@ before_conversion(nir_builder *bld, nir_alu_type type, unsigned bit_size, nir_de case nir_op_ishl: case nir_op_isub: case nir_op_ixor: + case nir_op_inot: + case nir_op_bcsel: + case nir_op_bitfield_select: case nir_op_mov: break; default: