From 3f12902e14ad730586fd26261bfcfc1a1951a075 Mon Sep 17 00:00:00 2001 From: Luuk Motz Date: Mon, 30 Nov 2026 18:00:03 +0100 Subject: [PATCH 1/2] shader: accept scalar work between a mesh diamond join and its EXEC restore A compiler may schedule EXEC-independent scalar instructions (SMEM loads, SALU, s_waitcnt, s_clause) between the join of a signed-CMPX if/else and the s_mov_b64 exec restore that closes it. The frontend accepted the restore only at the join and refused the rest as an EXEC write inside a lane-skip block. Those instructions run once per wave whatever EXEC holds, so the restore still closes the diamond. Each must leave EXEC, VCC and control flow alone, no other path may arrive in the gap, and the restore must still name the open skip's guard. Tests to follow. Co-Authored-By: Claude Opus 5.5 --- src/gfx1013_direct_mesh.inl | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/src/gfx1013_direct_mesh.inl b/src/gfx1013_direct_mesh.inl index a338a8f75b..0163a6a8c5 100644 --- a/src/gfx1013_direct_mesh.inl +++ b/src/gfx1013_direct_mesh.inl @@ -296,6 +296,8 @@ NativeComputeShader lower_mesh(const string &code, const MeshStageLayout &layout }; std::map> forward; bool live=true; + // A diamond joined and only scalar loads and their waits have run since (see the EXEC restore). + bool diamond_joined=false; unsigned next_edge=0; auto arrive=[&](unsigned target) { string label="mesh_edge"+to_string(next_edge++); @@ -316,6 +318,7 @@ NativeComputeShader lower_mesh(const string &code, const MeshStageLayout &layout pc=next->first; } if (forward.count(pc)) { + diamond_joined=false; // another path arrives between the join and the restore if (live) ir.body<<" br label %"<=128 && s<=250) || s==253 || s==255; }; + bool independent=false; + if (next.enc==Encoding::Smem) // opcodes 0-4: s_load_dword{,x2..x16}; 8-12: s_buffer_load_dword{,...} + independent=(next.op<=4 || (next.op>=8 && next.op<=12)) && next.dst+memory_count(next)<=106; + else if (next.enc==Encoding::Sopp) + independent=name=="s_waitcnt" || name=="s_clause" || name=="s_nop"; + else if (next.enc==Encoding::Sop1 || next.enc==Encoding::Sop2 || next.enc==Encoding::Sopk || + next.enc==Encoding::Sopc) + independent=name.find("exec")==string::npos && name.find("pc_")==string::npos && + name.find("movrel")==string::npos && lane_free(next.src[0]) && lane_free(next.src[1]) && + (next.enc==Encoding::Sopc || next.dst<106); + if (!independent) diamond_joined=false; } program.at[pc]=0; alu.graphics_pc = pc; @@ -1044,7 +1071,7 @@ NativeComputeShader lower_mesh(const string &code, const MeshStageLayout &layout } else if (i.name=="s_mov_b64" && i.dst==126 && i.src[0]<=104 && !(i.src[0]&1)) { const auto mask=saved_mask(i.src[0]); if (exec_skip && !diamond_exec_restore && - !(closed_nested_skip && mask && !mask->full && mask->lane==exec_skip->guard && + !((closed_nested_skip || after_diamond_join) && mask && !mask->full && mask->lane==exec_skip->guard && mask->wave==exec_skip->wave_guard)) refuse(pc,"mesh EXEC write inside a lane-skip block"); if (!mask) refuse(pc,"mesh EXEC restore lacks a still-valid reaching mask"); From ebaf1817050d7f8db3d6fca50fd550e9b851d17c Mon Sep 17 00:00:00 2001 From: Luuk Motz Date: Mon, 30 Nov 2026 19:20:40 +0100 Subject: [PATCH 2/2] tests: scalar work between a mesh diamond join and its EXEC restore The positive case places a table load, its wait and a scalar add between the join of a signed-CMPX diamond and the restore. Controls put a VCC write, a branch, a saved-mask overwrite or an EXEC read in the same gap, and each keeps the restore refused. Co-Authored-By: Claude Opus 5.5 --- src/gfx1013_direct_mesh.inl | 2 +- tests/native_direct_mesh_check.py | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/gfx1013_direct_mesh.inl b/src/gfx1013_direct_mesh.inl index 0163a6a8c5..4ffd0e20f3 100644 --- a/src/gfx1013_direct_mesh.inl +++ b/src/gfx1013_direct_mesh.inl @@ -296,7 +296,7 @@ NativeComputeShader lower_mesh(const string &code, const MeshStageLayout &layout }; std::map> forward; bool live=true; - // A diamond joined and only scalar loads and their waits have run since (see the EXEC restore). + // A diamond joined and only EXEC-independent scalar work has run since (see the EXEC restore). bool diamond_joined=false; unsigned next_edge=0; auto arrive=[&](unsigned target) { diff --git a/tests/native_direct_mesh_check.py b/tests/native_direct_mesh_check.py index 8329a7d32c..ff4f24db10 100644 --- a/tests/native_direct_mesh_check.py +++ b/tests/native_direct_mesh_check.py @@ -373,6 +373,32 @@ def lane_skip(name, body): 0x7d280015] # v_cmpx_gt_i32_e32 s21, v0 assert 'phi i32' in lane_skip('signed-cmpx-exec-restore', cmpx+cmpx_tail) + # A compiler may place EXEC-independent scalar work between the join + # and the restore: a table load, its wait and SALU run once per wave + # whatever EXEC holds, so the restore still closes the diamond. + layout.write_text('mesh1 128 3 1 2 128 64\nconstants 21 0 1\ntable 16 1') + scalar_gap = [0xbe910380, # s_mov_b32 s17, 0 + 0xf4080608, 0xfa000000, # s_load_dwordx4 s[24:27], s[16:17], 0 + 0xbf8c3f70, # s_waitcnt + 0x800c8415] # s_add_u32 s12, s21, 4 + def gap_diamond(gap): + # The else arm's EXECZ target, index 7 of the tail, is the join. + return cmpx+cmpx_tail[:7]+gap+cmpx_tail[7:] + assert 'phi i32' in lane_skip('signed-cmpx-scalar-gap', gap_diamond(scalar_gap)) + # The join is body word 9 after the outer CMPX and EXECZ at +0x1c. + def gap_refused(name, gap): + body = gap_diamond(gap) + wrapped = [0x7da40081, 0xbf880000 | len(body), *body, 0xbefe04c1] + restore = 0x24 + 4 * (9 + len(gap)) + run(name, program[:7]+wrapped+program[7:], f'+0x{restore:x}: {exec_write}') + # Writes to VCC, a branch, an overwritten saved mask or a read of + # EXEC in the gap keep the restore refused. + gap_refused('gap-vcc-write', scalar_gap + [0xbeea04c1]) # s_mov_b64 vcc, -1 + gap_refused('gap-branch', scalar_gap + [0xbf820000]) # s_branch to the next word + gap_refused('gap-saved-mask', scalar_gap + [0xbe960380]) # s_mov_b32 s22, 0 + gap_refused('gap-exec-read', scalar_gap + [0xbe9c047e]) # s_mov_b64 s[28:29], exec + layout.write_text('mesh1 128 3 1 2 128 64\nconstants 21 0 1') + # No diamond follows either write. The generic mask ALU must use # the narrowed lane predicate for the following v1 update. narrowing = [0x7d080015, # v_cmp_gt_i32_e32 vcc, s21, v0