From 8bb100fc02bc74bb2423ce5c6bba0046a24ac5d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pavel=20Ondra=C4=8Dka?= Date: Tue, 31 Jan 2023 13:57:22 +0100 Subject: [PATCH] nir: mark progress when removing trailing unused load_const channels MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When the unused channels were at the end and so no reswizzling was needed, we wouldn't correctly mark the progress. Fixes: 3305c960 Signed-off-by: Pavel Ondračka Reviewed-by: Timur Kristóf Reviewed-by: Emma Anholt Part-of: (cherry picked from commit 7e6acfd58772fbfbcd59404c26444939cfb84555) --- .pick_status.json | 2 +- src/compiler/nir/nir_opt_shrink_vectors.c | 11 +++--- .../nir/tests/opt_shrink_vectors_tests.cpp | 35 +++++++++++++++++++ 3 files changed, 43 insertions(+), 5 deletions(-) diff --git a/.pick_status.json b/.pick_status.json index a5f28cd1317..6adf34f7538 100644 --- a/.pick_status.json +++ b/.pick_status.json @@ -4,7 +4,7 @@ "description": "nir: mark progress when removing trailing unused load_const channels", "nominated": true, "nomination_type": 1, - "resolution": 0, + "resolution": 1, "main_sha": null, "because_sha": "3305c9602df65d08f586ee520de9d8f0c46b14f5" }, diff --git a/src/compiler/nir/nir_opt_shrink_vectors.c b/src/compiler/nir/nir_opt_shrink_vectors.c index 3d6ba3df22e..6dbde5c811e 100644 --- a/src/compiler/nir/nir_opt_shrink_vectors.c +++ b/src/compiler/nir/nir_opt_shrink_vectors.c @@ -330,13 +330,16 @@ opt_shrink_vectors_load_const(nir_load_const_instr *instr) } } - unsigned rounded = round_up_components(num_components); - assert(rounded <= def->num_components); - - def->num_components = rounded; if (progress) reswizzle_alu_uses(def, reswizzle); + unsigned rounded = round_up_components(num_components); + assert(rounded <= def->num_components); + if (rounded < def->num_components) + progress = true; + + def->num_components = rounded; + return progress; } diff --git a/src/compiler/nir/tests/opt_shrink_vectors_tests.cpp b/src/compiler/nir/tests/opt_shrink_vectors_tests.cpp index fef3632a302..49329115a20 100644 --- a/src/compiler/nir/tests/opt_shrink_vectors_tests.cpp +++ b/src/compiler/nir/tests/opt_shrink_vectors_tests.cpp @@ -84,6 +84,41 @@ static void check_swizzle(nir_alu_src * src, const char * swizzle) } } +TEST_F(nir_opt_shrink_vectors_test, opt_shrink_vectors_load_const_trailing_component_only) +{ + /* Test that opt_shrink_vectors correctly removes unused trailing channels + * of load_const. + * + * vec4 32 ssa_1 = load_const (1.0, 2.0, 3.0, 4.0) + * vec1 32 ssa_2 = fmov ssa_1.x + * + * to + * + * vec1 32 ssa_1 = load_const (1.0) + * vec1 32 ssa_2 = fmov ssa_1.x + */ + + nir_ssa_def *imm_vec = nir_imm_vec4(&bld, 1.0, 2.0, 3.0, 4.0); + + nir_ssa_def *alu_result = nir_build_alu1(&bld, nir_op_mov, imm_vec); + nir_alu_instr *alu_instr = nir_instr_as_alu(alu_result->parent_instr); + set_swizzle(&alu_instr->src[0], "x"); + alu_result->num_components = 1; + alu_instr->dest.write_mask = BITFIELD_MASK(1); + + nir_store_var(&bld, out_var, alu_result, 1); + + ASSERT_TRUE(nir_opt_shrink_vectors(bld.shader)); + + nir_validate_shader(bld.shader, NULL); + + ASSERT_TRUE(imm_vec->num_components == 1); + nir_load_const_instr * imm_vec_instr = nir_instr_as_load_const(imm_vec->parent_instr); + ASSERT_TRUE(nir_const_value_as_float(imm_vec_instr->value[0], 32) == 1.0); + + ASSERT_FALSE(nir_opt_shrink_vectors(bld.shader)); +} + TEST_F(nir_opt_shrink_vectors_test, opt_shrink_vectors_alu_trailing_component_only) { /* Test that opt_shrink_vectors correctly removes unused trailing channels