From 5625b2058d0c4dd8f27d138781ec0fbaeec23b6c Mon Sep 17 00:00:00 2001 From: Luuk Motz Date: Tue, 1 Dec 2026 18:09:23 +0100 Subject: [PATCH 1/2] shader: merge mesh writes under one CMPX inside a lane skip and restore the guard Inside the vertex skip, skinned mesh programs save EXEC, narrow it with one v_cmpx_ne_u32 and no EXECZ, run straight-line VALU work and restore the saved vertex mask. The restore was refused as "mesh EXEC write inside a lane-skip block". When the CMPX runs, every register is exact for the skip's guard lanes. A write under the narrowed mask whose old value is still that exact value (unchanged since the CMPX, or itself merged) is now merged per lane with it, so it stays exact for the guard. A restore of the guard's saved mask keeps every register that is unchanged since the CMPX or merged, and clears the rest, such as a register first written under the narrowed mask. A second narrowing, or a restore of a mask saved under the narrowed EXEC, stays refused. Part of #3703. Co-Authored-By: Claude Opus 5.5 --- src/gfx1013_direct_mesh.inl | 51 +++++++++++++++++++++++++++++-- tests/native_direct_mesh_check.py | 28 +++++++++++++++++ 2 files changed, 77 insertions(+), 2 deletions(-) diff --git a/src/gfx1013_direct_mesh.inl b/src/gfx1013_direct_mesh.inl index abc97056b8..b2b306409d 100644 --- a/src/gfx1013_direct_mesh.inl +++ b/src/gfx1013_direct_mesh.inl @@ -286,6 +286,10 @@ NativeComputeShader lower_mesh(const string &code, const MeshStageLayout &layout struct MaskState { bool full; string lane,wave,lo,hi; decltype(alu.graphics_registers) regs; std::set partial; }; std::map saved_exec; std::map before_narrowing; + // One CMPX narrowing an open lane skip's guard, with no EXECZ: the registers when it ran (all exact + // for the guard's lanes) and the values merged per lane against them since (see the EXEC restore). + struct SkipNarrowing { unsigned skip; string lane; decltype(alu.graphics_registers) regs; std::set merged; }; + std::optional skip_narrowing; auto saved_mask=[&](unsigned source)->const MaskState * { if (source>104 || (source&1)) return nullptr; const auto &lo=state[source], &hi=state[source+1]; @@ -1285,6 +1289,7 @@ NativeComputeShader lower_mesh(const string &code, const MeshStageLayout &layout before_partial[j]=partial_writes.count(vec_dst+j)!=0; } } + const string lane_at_start=lane_active; const bool export_cmp=i.name=="v_cmpx_gt_i32" && allocated && i.src[0]<106 && full_exec && (alu.read(i.src[0])==vertex_count || alu.read(i.src[0])==primitive_count); if ((i.name=="v_cmpx_gt_u32" || export_cmp) && full_exec && !branch && !exec_skip && i.enc==Encoding::Vopc && !i.wide && !i.subword && !i.dpp_control && @@ -1949,12 +1954,28 @@ NativeComputeShader lower_mesh(const string &code, const MeshStageLayout &layout saved_exec[pc]={full_exec,lane_active,wave_active,exec_value[0],exec_value[1],alu.graphics_registers,partial_writes}; } 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 && + // The guard restored after one CMPX narrowed it inside the skip: each register is either + // unchanged since that CMPX or merged per lane against its value then. + const bool narrowed_restore=exec_skip && !branch && skip_narrowing && skip_narrowing->lane==lane_active && + skip_narrowing->skip==exec_skip->start && + mask && !mask->full && mask->lane==exec_skip->guard && mask->wave==exec_skip->wave_guard; + if (exec_skip && !diamond_exec_restore && !narrowed_restore && !((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"); - if (mask->lane!=lane_active) { + if (narrowed_restore) { + std::set remaining; + for (auto r:partial_writes) { + const auto &now=alu.graphics_registers.at(r); + const auto at=skip_narrowing->regs.find(r); + if ((at!=skip_narrowing->regs.end() && !at->second.empty() && at->second==now) || + skip_narrowing->merged.count(now)) remaining.insert(r); + else alu.write(r,{}); + } + partial_writes=std::move(remaining); + skip_narrowing.reset(); + } else if (mask->lane!=lane_active) { auto at_narrow=before_narrowing.find(mask->lane); const auto ®s=at_narrow==before_narrowing.end() ? mask->regs : at_narrow->second.regs; const auto &valid=at_narrow==before_narrowing.end() ? mask->partial : at_narrow->second.partial; @@ -2177,8 +2198,34 @@ NativeComputeShader lower_mesh(const string &code, const MeshStageLayout &layout alu.record_graphics_bits(value,bits); if (!u.empty()) active_uniform[value]={lane_active,u}; partial_writes.erase(vec_dst+j); + } else if (skip_narrowing && skip_narrowing->lane==lane_active && exec_skip && !branch && + skip_narrowing->skip==exec_skip->start && + !before_vec[j].empty() && before_partial[j] && !value_limits.count(before_vec[j]) && + alu.graphics_registers.at(vec_dst+j)!=before_vec[j] && + (skip_narrowing->merged.count(before_vec[j]) || [&] { + const auto at=skip_narrowing->regs.find(vec_dst+j); + return at!=skip_narrowing->regs.end() && at->second==before_vec[j]; + }())) { + // The old value is exact for the guard's lanes, so the merge is too; it stays partial + // against full EXEC. + auto &value=alu.graphics_registers.at(vec_dst+j); + auto u=uniform(value); + const auto bits=alu.graphics_bits(value)&alu.graphics_bits(before_vec[j]); + value=alu.select(lane_active,value,before_vec[j]); + alu.record_graphics_bits(value,bits); + if (!u.empty()) active_uniform[value]={lane_active,u}; + skip_narrowing->merged.insert(value); } } + // A skip opened under the narrowed mask ends the straight-line region; its close is not a merge. + if (iname=="s_cbranch_execz") skip_narrowing.reset(); + // A CMPX narrowing the open skip's guard starts a region the restore above can close. + if (iname.rfind("v_cmpx",0)==0 && exec_skip && !branch && lane_at_start==exec_skip->guard && + lane_active!=lane_at_start) { + const auto parent=mask_parent.find(lane_active); + if (parent!=mask_parent.end() && parent->second==lane_at_start) + skip_narrowing=SkipNarrowing{exec_skip->start,lane_active,alu.graphics_registers,{}}; + } if (result_bound && *result_boundsecond.empty()) upper_bound[it->second]=*result_bound; diff --git a/tests/native_direct_mesh_check.py b/tests/native_direct_mesh_check.py index 0816a19356..a0d1ebd874 100644 --- a/tests/native_direct_mesh_check.py +++ b/tests/native_direct_mesh_check.py @@ -1411,6 +1411,34 @@ def merged(condition): assert 'storeVertexOutput' in run('bounded-export-root-branch', bounded(v=merged(0xbf068015))) # s21 == 0 run('bounded-export-wave-branch', bounded(v=merged(0xbf068016)), lane_range) # s22 == 0 + # Inside the vertex skip, the title saves EXEC, narrows it with one CMPX and no EXECZ, runs + # straight-line VALU work and restores the saved vertex mask. Every register is exact for the + # vertex lanes when the CMPX runs, so a write under the narrowed mask is merged per lane with + # the old value and the restore keeps it. v10 is then read and written under the vertex mask. + lane_skip_exec = 'mesh EXEC write inside a lane-skip block' + def narrowed(region, restore=0xbefe0426, after=(0x061414f2,)): # v_add_f32 v10, 1.0, v10 + body = ([0xbea6047e, # s_mov_b64 s[38:39], exec (the vertex mask) + 0x7e1402f2, # v_mov_b32 v10, 1.0 + 0x7daa0080] + list(region) + # v_cmpx_ne_u32 0, v0 + [restore] + list(after)) # s_mov_b64 exec, s[38:39] + return (count_setup[:4] + [0x8717ff15, 0x3, 0xbf068017, 0x85221780, 0xbea30381, 0x8f1c8616, + 0xd7650000, 0x000038c1, 0xd7660000, 0x000200c1] + + [0x7da80017, 0xbf880000 | len(body)] + body + # v_cmpx_gt_u32 s23, v0; the vertex skip + [0xbefe04c1] + allocate + [0x7d280022, 0xbf880002, + 0xf8000201, 0x0000000a, # exp param0 v10 + 0xbefe04c1] + program[3:]) + base = run('skip-narrow-empty', narrowed([])) + text = run('skip-narrow-merge', narrowed([0x061414f2])) # v_add_f32 v10, 1.0, v10 under v0 != 0 + assert 'storeVertexOutput' in text and text.count('select ') == base.count('select ') + 1, text + # A register first written under the narrowed mask has no value for thread 0 at the restore. + run('skip-narrow-new-register', narrowed([0x7e1602f4], # v_mov_b32 v11, 2.0 + after=(0x0614150b,)), # v_add_f32 v10, v11, v10 + 'graphics register undefined on an incoming path: 267') + # A second narrowing is not merged against the vertex mask, so its restore stays refused. + run('skip-narrow-twice', narrowed([0x7daa0080, 0x061414f2]), lane_skip_exec) + # So is a restore of a mask saved under the narrowed EXEC. + run('skip-narrow-inner-save', narrowed([0xbea8047e, 0x061414f2], restore=0xbefe0428), lane_skip_exec) + layout.write_text('mesh1 128 3 1 2 128 64\ntable 16 0') run('mistyped-root',words+controlled,'root parameter kind') layout.write_text('mesh1 128 3 1 2 128 64') From de321a364d790b5d068696b6c0c0f2d9a9ce2c3f Mon Sep 17 00:00:00 2001 From: Luuk Motz Date: Tue, 1 Dec 2026 20:44:32 +0100 Subject: [PATCH 2/2] shader: track lane-skip narrowing merges per register A merged value is exact for the guard's lanes only in the register it was merged into. Keying the merges by value let a copy of a merged value into another register pass the restore as exact. Record the merged value per register instead, and add the copy fixture from review plus a second merge into the same register. Co-Authored-By: Claude Opus 5.5 --- src/gfx1013_direct_mesh.inl | 16 ++++++++++------ tests/native_direct_mesh_check.py | 7 +++++++ 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/src/gfx1013_direct_mesh.inl b/src/gfx1013_direct_mesh.inl index b2b306409d..e87687ddfa 100644 --- a/src/gfx1013_direct_mesh.inl +++ b/src/gfx1013_direct_mesh.inl @@ -287,8 +287,9 @@ NativeComputeShader lower_mesh(const string &code, const MeshStageLayout &layout std::map saved_exec; std::map before_narrowing; // One CMPX narrowing an open lane skip's guard, with no EXECZ: the registers when it ran (all exact - // for the guard's lanes) and the values merged per lane against them since (see the EXEC restore). - struct SkipNarrowing { unsigned skip; string lane; decltype(alu.graphics_registers) regs; std::set merged; }; + // for the guard's lanes) and, per register, the value merged per lane against them since (see the + // EXEC restore). A merged value is exact only in the register it was merged into, not in a copy. + struct SkipNarrowing { unsigned skip; string lane; decltype(alu.graphics_registers) regs; std::map merged; }; std::optional skip_narrowing; auto saved_mask=[&](unsigned source)->const MaskState * { if (source>104 || (source&1)) return nullptr; @@ -1969,8 +1970,9 @@ NativeComputeShader lower_mesh(const string &code, const MeshStageLayout &layout for (auto r:partial_writes) { const auto &now=alu.graphics_registers.at(r); const auto at=skip_narrowing->regs.find(r); + const auto merged=skip_narrowing->merged.find(r); if ((at!=skip_narrowing->regs.end() && !at->second.empty() && at->second==now) || - skip_narrowing->merged.count(now)) remaining.insert(r); + (merged!=skip_narrowing->merged.end() && merged->second==now)) remaining.insert(r); else alu.write(r,{}); } partial_writes=std::move(remaining); @@ -2202,10 +2204,12 @@ NativeComputeShader lower_mesh(const string &code, const MeshStageLayout &layout skip_narrowing->skip==exec_skip->start && !before_vec[j].empty() && before_partial[j] && !value_limits.count(before_vec[j]) && alu.graphics_registers.at(vec_dst+j)!=before_vec[j] && - (skip_narrowing->merged.count(before_vec[j]) || [&] { + [&] { + const auto merged=skip_narrowing->merged.find(vec_dst+j); + if (merged!=skip_narrowing->merged.end() && merged->second==before_vec[j]) return true; const auto at=skip_narrowing->regs.find(vec_dst+j); return at!=skip_narrowing->regs.end() && at->second==before_vec[j]; - }())) { + }()) { // The old value is exact for the guard's lanes, so the merge is too; it stays partial // against full EXEC. auto &value=alu.graphics_registers.at(vec_dst+j); @@ -2214,7 +2218,7 @@ NativeComputeShader lower_mesh(const string &code, const MeshStageLayout &layout value=alu.select(lane_active,value,before_vec[j]); alu.record_graphics_bits(value,bits); if (!u.empty()) active_uniform[value]={lane_active,u}; - skip_narrowing->merged.insert(value); + skip_narrowing->merged[vec_dst+j]=value; } } // A skip opened under the narrowed mask ends the straight-line region; its close is not a merge. diff --git a/tests/native_direct_mesh_check.py b/tests/native_direct_mesh_check.py index a0d1ebd874..e690ddaaa6 100644 --- a/tests/native_direct_mesh_check.py +++ b/tests/native_direct_mesh_check.py @@ -1434,6 +1434,13 @@ def narrowed(region, restore=0xbefe0426, after=(0x061414f2,)): # v_add_f32 v10, run('skip-narrow-new-register', narrowed([0x7e1602f4], # v_mov_b32 v11, 2.0 after=(0x0614150b,)), # v_add_f32 v10, v11, v10 'graphics register undefined on an incoming path: 267') + # A merged value is exact only in the register it was merged into: a copy of it is not. + run('skip-narrow-merged-copy', narrowed([0x061414f2, 0x7e16030a], # v_mov_b32 v11, v10 + after=(0x0614150b,)), # v_add_f32 v10, v11, v10 + 'graphics register undefined on an incoming path: 267') + # The same register merged again stays exact. + text = run('skip-narrow-merge-twice', narrowed([0x061414f2, 0x061414f2])) + assert 'storeVertexOutput' in text and text.count('select ') == base.count('select ') + 2, text # A second narrowing is not merged against the vertex mask, so its restore stays refused. run('skip-narrow-twice', narrowed([0x7daa0080, 0x061414f2]), lane_skip_exec) # So is a restore of a mask saved under the narrowed EXEC.