From 9d21285b6d919c9a39e93cb0520700e68b578351 Mon Sep 17 00:00:00 2001 From: Luuk Motz Date: Mon, 30 Nov 2026 19:53:01 +0100 Subject: [PATCH] shader: lower s_movk_i32 and s_xor_b32 in the mesh frontend s_movk_i32 writes the sign-extended 16-bit immediate as a known constant; VCC, EXEC and other special destinations stay refused. s_xor_b32 joins the SGPR-destination SOP2 bitwise branch. Source-built tests cover both, plus VCC/EXEC/M0 destination controls. Part of #2413. Co-Authored-By: Claude Opus 5.5 --- src/gfx1013_direct_mesh.inl | 13 +++++++++++-- tests/native_direct_mesh_check.py | 14 ++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/gfx1013_direct_mesh.inl b/src/gfx1013_direct_mesh.inl index a338a8f75b..19eb432d7f 100644 --- a/src/gfx1013_direct_mesh.inl +++ b/src/gfx1013_direct_mesh.inl @@ -1079,6 +1079,14 @@ NativeComputeShader lower_mesh(const string &code, const MeshStageLayout &layout // NULL discards the scalar move; MOV does not write SCC. } else if (i.name=="s_mov_b32" && i.dst==124) { allocation_word=constant32(i,i.src[0]); + } else if (i.name=="s_movk_i32") { + // SOPK: the destination takes the sign-extended 16-bit immediate. SCC is unchanged. + if (i.dst==126 || i.dst==127) refuse(pc,"mesh scalar write to EXEC"); + if (i.dst==106 || i.dst==107) refuse(pc,"mesh scalar write to VCC"); + if (i.dst>=106) refuse(pc,"unsupported mesh scalar destination: s_movk_i32"); + const Word value=Word(int16_t(i.a)); + alu.write(i.dst,to_string(value)); + state[i.dst]={Symbol::Type::constant,value}; } else if (i.name=="s_mov_b32" && i.dst<106) { const auto provenance=source(i,i.src[0],state); if (auto value=constant32(i,i.src[0])) alu.write(i.dst,to_string(*value)); @@ -1117,7 +1125,7 @@ NativeComputeShader lower_mesh(const string &code, const MeshStageLayout &layout alu.write(i.dst,ir.binary("or",high ? ir.binary("lshr",a,"16") : ir.binary("and",a,"65535"), high ? ir.binary("and",b,"4294901760") : ir.binary("shl",b,"16"))); } else if (i.enc==Encoding::Sop2 && i.dst<106 && (i.name=="s_lshr_b32" || i.name=="s_lshl_b32" || - i.name=="s_or_b32" || i.name=="s_and_b32")) { + i.name=="s_or_b32" || i.name=="s_and_b32" || i.name=="s_xor_b32")) { const auto known=[&](unsigned r) { return constant32(i,r).has_value() || (alu.graphics_registers.count(r) && !alu.graphics_registers.at(r).empty()); @@ -1126,7 +1134,8 @@ NativeComputeShader lower_mesh(const string &code, const MeshStageLayout &layout const auto a=alu.operand32(i,i.src[0]), b=alu.operand32(i,i.src[1]); const bool shift=i.name=="s_lshl_b32" || i.name=="s_lshr_b32"; const auto value=ir.binary(i.name=="s_lshl_b32" ? "shl" : i.name=="s_lshr_b32" ? "lshr" : - i.name=="s_or_b32" ? "or" : "and",a,shift ? ir.binary("and",b,"31") : b); + i.name=="s_or_b32" ? "or" : i.name=="s_xor_b32" ? "xor" : "and", + a,shift ? ir.binary("and",b,"31") : b); if (i.name=="s_lshl_b32" && a==wave_number && constant32(i,i.src[1])==(layout.wave32 ? 5u : 6u)) wave_bases.insert(value); const auto bits=root_values.find(a); diff --git a/tests/native_direct_mesh_check.py b/tests/native_direct_mesh_check.py index 8329a7d32c..1591d86a3b 100644 --- a/tests/native_direct_mesh_check.py +++ b/tests/native_direct_mesh_check.py @@ -224,6 +224,20 @@ def run(name, words, expected=None): run(scalar_vcc_case, program[:7] + [0x7d880080, scalar_vcc_word, 0x02020285] + program[7:], '+0x20: mesh scalar write to VCC') + # s_movk_i32 s20, 0xfffe sign-extends to -2; s_xor_b32 s21, s20, -1 gives 1, which feeds + # v_and_b32 v1, s21, v0 and so the exported X position. + text = run('scalar-movk-xor', program[:5] + [0xb014fffe, 0x8915c114, 0x36020015] + program[6:]) + xor = re.search(r'(%[\w.]+) = xor i32 -2, -1\n', text) + assert xor, text + assert_position_x(text, r'and i32 ' + re.escape(xor[1]) + ',') + for scalar_k_case, scalar_k_word, scalar_k_reason in ( + ('scalar-movk-vcc', 0xb06afffe, 'mesh scalar write to VCC'), + ('scalar-movk-exec', 0xb07efffe, 'mesh scalar write to EXEC'), + ('scalar-movk-m0', 0xb07cfffe, 'unsupported mesh scalar destination: s_movk_i32'), + ('scalar-xor-exec', 0x897ec114, 'unsupported mesh instruction or EXEC state: s_xor_b32'), + ('scalar-xor-vcc', 0x896ac114, 'unsupported mesh instruction or EXEC state: s_xor_b32'), + ): + run(scalar_k_case, program[:5] + [scalar_k_word] + program[5:], '+0x14: ' + scalar_k_reason) # The three primitive slots are 9-bit indices with the edge flag in # bit 9. 0x20280600 encodes (0,1,2) with all three edge flags set. edge = program.copy()