From c725dc6e1eb7ee900ac234b52a859ada35a7d230 Mon Sep 17 00:00:00 2001 From: Leyukaka <45659323+Leyukaka@users.noreply.github.com> Date: Sun, 29 Nov 2026 01:42:18 +0100 Subject: [PATCH] shader: branch on VCC from a wave-uniform mesh compare --- src/gfx1013_direct_mesh.inl | 31 +++++++++++++++++------ tests/native_direct_mesh_check.py | 41 +++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 7 deletions(-) diff --git a/src/gfx1013_direct_mesh.inl b/src/gfx1013_direct_mesh.inl index cc2f1b3232..c60aacfa85 100644 --- a/src/gfx1013_direct_mesh.inl +++ b/src/gfx1013_direct_mesh.inl @@ -26,12 +26,16 @@ NativeComputeShader lower_mesh(const string &code, const MeshStageLayout &layout const unsigned width = layout.wave32 ? 32 : 64; const auto wave_lane = ir.binary("and",thread,to_string(width-1)); const auto wave_number = ir.binary("lshr",thread,layout.wave32 ? "5" : "6"); + constexpr unsigned vcc_words=0x70000000, vcc_nonempty=vcc_words+2; alu.graphics_input = [&](unsigned r) -> string { refuse(alu.graphics_pc,"unproven mesh entry register " + to_string(r)); }; alu.after_graphics_write = [&](unsigned r) { if (r<106) program.before.front()[r].type=alu.graphics_registers.at(r).empty() ? Symbol::Type::unbound : Symbol::Type::unknown; + else if (r==106 || r==107) { + alu.write(vcc_words,{}); alu.write(vcc_words+1,{}); alu.write(vcc_nonempty,{}); + } }; auto &state=program.before.front(); auto inputs=layout.root_inputs; @@ -144,7 +148,6 @@ NativeComputeShader lower_mesh(const string &code, const MeshStageLayout &layout }; std::optional branch; unsigned lane_else=0; - constexpr unsigned vcc_words=0x70000000, vcc_nonempty=vcc_words+2; std::map mask_parent; auto covered_by=[&](string current,const string &ancestor) { if (ancestor.empty()) return true; @@ -212,6 +215,9 @@ NativeComputeShader lower_mesh(const string &code, const MeshStageLayout &layout struct Skip { unsigned start,end; Edge entry; bool per_lane; string guard; }; std::vector skip_parents; std::optional exec_skip; + auto exec_nonempty_known=[&] { + return full_exec || !wave_active.empty() || (exec_skip && exec_skip->guard==lane_active); + }; unsigned mask_generation=0x1000000; auto merge_edges=[&](const Edge &first, const string &l, const string &r) { auto phi=[&](const string &a,const string &b) {return ir.value("phi i32 [ "+a+", %"+l+" ], [ "+b+", %"+r+" ]");}; @@ -310,6 +316,7 @@ NativeComputeShader lower_mesh(const string &code, const MeshStageLayout &layout auto now=alu.graphics_registers.find(r), old=exec_skip->entry.regs.find(r); if (now!=alu.graphics_registers.end() && (old==exec_skip->entry.regs.end() || now->second!=old->second)) { now->second.clear(); + if (r==106 || r==107) alu.after_graphics_write(r); if (r<128) state[r].type=Symbol::Type::unbound; } } @@ -350,6 +357,7 @@ NativeComputeShader lower_mesh(const string &code, const MeshStageLayout &layout }; if (differs(first.regs) || differs(alu.graphics_registers)) { alu.graphics_registers[r].clear(); + if (r==106 || r==107) alu.after_graphics_write(r); if (r<106) state[r].type=Symbol::Type::unbound; } } @@ -360,9 +368,6 @@ NativeComputeShader lower_mesh(const string &code, const MeshStageLayout &layout alu.write(r,{}); if (rguard==lane_active))) { + exec_nonempty_known()) { auto flag=mask_uniform.at(i.src[0]==126 ? b : a).second; if (i.name=="s_andn2_b64") flag=ir.value("xor i1 "+flag+", true"); if (!wave_active.empty()) flag=ir.value("and i1 "+flag+", "+wave_active); diff --git a/tests/native_direct_mesh_check.py b/tests/native_direct_mesh_check.py index 7580c89a9e..46c189a2f0 100644 --- a/tests/native_direct_mesh_check.py +++ b/tests/native_direct_mesh_check.py @@ -1,5 +1,6 @@ """Source-built mesh ISA, direct DXIL validation and fail-closed controls.""" from pathlib import Path +import re import struct import subprocess import sys @@ -128,6 +129,46 @@ def run(name, words, expected=None): text = run('root-branch',words+controlled) for marker in ('cbufferLoadLegacy','rawBufferLoad','phi i32','storeVertexOutput'): assert marker in text, marker + + # s20 = root0[0]; v_cmp_gt_u32_e64 vcc, s20, 6. + copy_root = [0xbe940315] + compare_root = [0xd4c4006a, 0x00010c14] + choose_output = [0xbf860002, 0xbe940381, 0xbf820001, 0xbe940382] + text = run('root-vcc-branch', copy_root+compare_root+choose_output+controlled) + assert 'cbufferLoadLegacy' in text and 'storeVertexOutput' in text + root_word = re.search(r'(%[\w.]+) = extractvalue %dx.types.CBufRet.i32 %[\w.]+, 0', text) + assert root_word, text + compares_root = r'(?P%[\w.]+) = icmp u(?:gt|le) i32 ' + re.escape(root_word[1]) + r', 6\b' + branches_on_compare = r'br i1 (?P=cmp),' + branches_on_zero = (r'(?P%[\w.]+) = zext i1 (?P=cmp) to i32.*?' + r'(?P%[\w.]+) = icmp eq i32 (?P=word), 0\b.*?br i1 (?P=zero),') + assert re.search(compares_root + r'.*?(?:' + branches_on_compare + '|' + branches_on_zero + ')', + text, re.DOTALL), text + + vcc_refusal = 'mesh VCC branch lacks a proven guest whole-wave mask' + lane_id = program[3:5] # v0 = mbcnt_lo(-1, 0) + compare_lane = [0x7d880014] # v_cmp_gt_u32_e32 vcc, s20, v0 + run('lane-vcc-branch', copy_root+lane_id+compare_lane+choose_output+controlled, vcc_refusal) + # v_cmpx_eq_u32 0, v0 narrows EXEC without proving wave nonemptiness. + # Restore full EXEC before the scalar branch to isolate the VCC check. + run('narrowed-vcc-branch', copy_root+lane_id+[0x7da40080]+compare_root+ + [0xbefe04c1]+choose_output+controlled, vcc_refusal) + run('overwritten-vcc-branch', copy_root+lane_id+compare_root+compare_lane+ + choose_output+controlled, vcc_refusal) + + # Seed VCC so both edges have a value to merge. A VCC write inside + # a per-lane skip or diamond cannot supply a whole-wave branch mask. + seed_vcc = [0xbeea04c1] # s_mov_b64 vcc, -1 + restore_full_exec = [0xbefe04c1] + run('skip-uniform-vcc', copy_root+lane_id+seed_vcc+[0x7da40080, 0xbf880002]+ + compare_root+restore_full_exec+choose_output+controlled, vcc_refusal) + + diamond_body = ([0xbe9e047e, 0x7d280014, 0xbf880002]+compare_root+ + [0x8afe7e1e, 0xbf880001, 0xbf800000, 0xbefe041e]+choose_output) + wave_skip = [0xbf061414, 0x85a080c1, 0x87fe7e20, 0xbf880000 | len(diamond_body)] + run('diamond-known-vcc', copy_root+lane_id+seed_vcc+wave_skip+diamond_body+ + restore_full_exec+controlled, vcc_refusal) + typed = [0xbe9e0317,0xbe9f0380, # copy the heap base; zero high address 0x8715ff15,31,0x8f158515, # (root0[0] & 31) * descriptor size 0xf408080f,0x2a000000, # load V# into s32:s35