From f0b966b82dc466e86d665a5741710500dbce9ef8 Mon Sep 17 00:00:00 2001 From: Roland Edi Date: Mon, 30 Nov 2026 02:26:42 +0000 Subject: [PATCH 1/9] shader: accept any zero VGPR in the mesh group-index exchange The native mesh frontend matched the group-index exchange only when the zero-address VGPR was v0. The real slot 57 shape uses v6. Read the VGPR from the first v_mov_b32_e32 and rebuild the second move and both DS address fields, then clear the selected VGPR instead of v0. Add a source-built 85-word real-shape fixture with the v6 exchange, a formatted heap read through v_readfirstlane_b32, three DS writes and reads, five barriers, GS_ALLOC_REQ and pos0/prim exports. compile_dxil produces a 4,828-byte MS and a 2,386-byte PS. A mismatched zero VGPR (v5 with DS fields still naming v6) is refused. This tranche covers the compiler shape; the direct-heap runtime profile and the draw/readback remain for the next one. --- docs/experiments/native-fixed-mesh57.md | 14 ++++ src/gfx1013_direct_mesh.inl | 16 +++- tests/cmake/startup_mesh57_real_shape.cmake | 8 ++ tests/startup_mesh57_real_shape_check.py | 91 +++++++++++++++++++++ 4 files changed, 125 insertions(+), 4 deletions(-) create mode 100644 tests/cmake/startup_mesh57_real_shape.cmake create mode 100644 tests/startup_mesh57_real_shape_check.py diff --git a/docs/experiments/native-fixed-mesh57.md b/docs/experiments/native-fixed-mesh57.md index dcb0755f22..f06add3caa 100644 --- a/docs/experiments/native-fixed-mesh57.md +++ b/docs/experiments/native-fixed-mesh57.md @@ -88,3 +88,17 @@ pixel entry. A creation archive alone preserves the two register blocks but does not assign hardware register meanings to their offsets. Reproduce the source-built positive and altered-stage controls with `ctest --preset dev --no-tests=error -R '^native_direct_mesh$'`. + +## Real-shape v6 group-index exchange + +The native mesh frontend now reads the zero-address VGPR from the first +`v_mov_b32_e32` of the group-index exchange instead of requiring v0. The +source-built 85-word fixture also has a formatted heap read through +`v_readfirstlane_b32`, three DS writes and reads, five barriers, +`GS_ALLOC_REQ` and pos0/prim exports. `compile_dxil gfx1013-raw-ms` produces +a 4,828-byte MS and `gfx1013-raw-mesh-ps` a 2,386-byte PS. Changing the zero +VGPR to v5 while the DS fields still name v6 must be refused. + +Reproduce with `ctest --preset dev --no-tests=error -R '^startup_mesh57_real_shape$' -V`. +This tranche covers the compiler shape only; the direct-heap runtime profile +and the draw/readback are still to be added. diff --git a/src/gfx1013_direct_mesh.inl b/src/gfx1013_direct_mesh.inl index d768b61ac1..147b3d14f0 100644 --- a/src/gfx1013_direct_mesh.inl +++ b/src/gfx1013_direct_mesh.inl @@ -388,15 +388,23 @@ NativeComputeShader lower_mesh(const string &code, const MeshStageLayout &layout const Word save = word(code,pc+4); const bool has_save = (save & 0xff80ffff) == 0xbe80047e; const unsigned mask_offset = has_save ? 8 : 4; - constexpr Word tail[]{0x0f000000,0xbf850007,0x2c0a0a87,0x7e000280,0xd8340008,0x500, - 0xbfa3ffe3,0xbf8a0000,0xbf820004,0xbf8a0000,0x7e000280,0xd8d80008,0x05000000}; + // The zero address is not fixed to v0: the fixture and the title use + // any free VGPR. Read it from the first v_mov_b32_e32 and rebuild + // every word that names it (the second v_mov, both DS address + // fields and the DS read destination remains v5). + const Word zero_mov = word(code,pc+mask_offset+4+4*3); + const unsigned zero_vgpr = (zero_mov>>17)&127; + const Word tail[]{0x0f000000,0xbf850007,0x2c0a0a87, + 0x7e000280u|(zero_vgpr<<17),0xd8340008,0x500u|zero_vgpr, + 0xbfa3ffe3,0xbf8a0000,0xbf820004,0xbf8a0000, + 0x7e000280u|(zero_vgpr<<17),0xd8d80008,0x05000000u|zero_vgpr}; const unsigned exchange_size = mask_offset + 4 + 4*std::size(tail); const Word mask = word(code,pc+mask_offset); bool exchange = code.size()-pc >= exchange_size && (mask & 0xff80ffff) == 0x8700ff03; for (unsigned k=0;exchange && k>16)&127, temp = (mask>>16)&127; - if (exchange && !branch && !exec_skip && full_exec && !layout.wave32 && layout.group_stride && !alu.graphics_registers.count(3) && + if (exchange && zero_vgpr<106 && zero_vgpr!=5 && !branch && !exec_skip && full_exec && !layout.wave32 && layout.group_stride && !alu.graphics_registers.count(3) && !alu.graphics_registers.count(261) && temp<106 && (!has_save || (saved<=104 && !(saved&1) && temp!=saved && temp!=saved+1))) { if (has_save) { @@ -411,7 +419,7 @@ NativeComputeShader lower_mesh(const string &code, const MeshStageLayout &layout alu.write(128,ir.value("zext i1 "+alu.cmp("ne",merged,"0")+" to i32")); if (exchanged_group.empty()) refuse(pc,"mesh exchange lost the entry group VGPR"); exchanged_group = ir.binary("lshr",exchanged_group,"7"); - alu.write(256,{}); + alu.write(256+zero_vgpr,{}); alu.write(261,{}); full_exec=false; pc+=exchange_size; diff --git a/tests/cmake/startup_mesh57_real_shape.cmake b/tests/cmake/startup_mesh57_real_shape.cmake new file mode 100644 index 0000000000..12421af83e --- /dev/null +++ b/tests/cmake/startup_mesh57_real_shape.cmake @@ -0,0 +1,8 @@ +# Source-built real-shape slot 57 mesh: compile the MS and PS through the native frontend and +# require the v6 group-index exchange to be accepted, with a mismatched zero VGPR refused. +if(EXISTS "${DXC_EXECUTABLE}" AND TARGET compile_dxil) + add_test(NAME startup_mesh57_real_shape COMMAND "${INTAKE_PYTHON}" -B + "${CMAKE_SOURCE_DIR}/tests/startup_mesh57_real_shape_check.py" + "$" "${DXC_EXECUTABLE}") + set_tests_properties(startup_mesh57_real_shape PROPERTIES TIMEOUT 60 SKIP_RETURN_CODE 77) +endif() diff --git a/tests/startup_mesh57_real_shape_check.py b/tests/startup_mesh57_real_shape_check.py new file mode 100644 index 0000000000..2206677f44 --- /dev/null +++ b/tests/startup_mesh57_real_shape_check.py @@ -0,0 +1,91 @@ +"""Compile a source-built real-shape slot 57 mesh and its negative control. + +The fixture has the v6 group-index exchange, a formatted heap read through +v_readfirstlane_b32, the three DS writes and reads, five barriers, +GS_ALLOC_REQ and pos0/prim exports. The MS and PS are compiled separately by +the native frontend; this test does not submit a GPU draw. +""" + +import argparse +from pathlib import Path +import struct +import subprocess +import tempfile + + +MS = ( + "bfa00003 befe0481 be84047e 8706ff03 0f000000 bf850007 2c0a0a87 7e0c0280 " + "d8340008 00000506 bfa3ffe3 bf8a0000 bf820004 bf8a0000 7e0c0280 d8d80008 " + "05000006 befe04c1 9386ff03 00040018 8f078606 d7650000 02000ec1 d7660000 " + "020200c1 bf8cc07f 7e140505 8714ff10 000003ff 8f148514 be930380 f4080509 " + "28000000 7e02020a bf8cc07f e0002000 80050101 bf8c3f70 7e180501 870d8181 " + "870e8383 bf8a0000 bf008006 bf840010 befe0404 bf880007 7e02020d 7e040280 " + "7e06020e d8340000 00000102 d8340004 00000302 befe04c1 bfa3ffe3 bf8a0000 " + "8f048c0d 887c0e04 bf900009 bf82000d bf8a0000 befe0404 bf880009 7e040280 " + "d8d80000 01000002 d8d80004 03000002 bf8cc17f 7e1a0501 bf8cc07f 7e1c0503 " + "befe04c1 36020081 2c040081 7e020d01 7e040d02 7e060280 7e0802f2 f80008cf " + "04030201 7e0a020c f8000941 00000005 bf810000" +).split() + +PS = "7e0002f2 f800180f 00000000 bf810000".split() + + +def words(text): + return b"".join(struct.pack(" 0 + + ps = run([str(args.compile_dxil), str(dxc), str(cache), "gfx1013-raw-mesh-ps", + str(work / "real.ps57"), str(work / "real.ps57.cso")]) + assert not ps.returncode, ps.stdout + ps.stderr + assert (work / "real.ps57.cso").stat().st_size > 0 + + control = list(MS) + control[7] = "7e0a0280" # zero address in v5, DS fields still name v6 + (work / "control.m57").write_bytes(words(control)) + (work / "control.mslayout").write_text( + "mesh1 128 3 1 2 128 64 guest_root validate_uint_heap_reads\n", encoding="ascii") + refusal = run([str(args.compile_dxil), str(dxc), str(cache), "gfx1013-raw-ms", + str(work / "control.m57"), str(work / "control.ms57.cso")]) + assert refusal.returncode, "the mismatched zero VGPR must be refused" + assert not (work / "control.ms57.cso").exists() + print("real mesh v6 exchange: MS", (work / "real.ms57.cso").stat().st_size, + "bytes, PS", (work / "real.ps57.cso").stat().st_size, + "bytes, mismatched zero VGPR refused") + return 0 + + +if __name__ == "__main__": + raise SystemExit(main()) From 4204e325904270cc63095146617dcee77629f9d3 Mon Sep 17 00:00:00 2001 From: Roland Edi Date: Mon, 30 Nov 2026 02:28:41 +0000 Subject: [PATCH 2/9] build: build compile_dxil for the real-shape check --- CMakePresets.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakePresets.json b/CMakePresets.json index e649592f3b..66db5da502 100644 --- a/CMakePresets.json +++ b/CMakePresets.json @@ -29,7 +29,7 @@ "d3d12_sdk_check", "era_heap_check", "xmem_shim", "offscreen_check", "cpu_fuzz_lifted", "cpu_fuzz_lifted_o2", "pe_map", "intake_candidate", "archive_gpu_reader", "archive_codec_gpu", "archive_texture_gpu_lifted", "startup_launch_probe", - "startup_indirect_dispatch", "startup_indirect_draw_indexed", "startup_present_readback_check", + "compile_dxil", "startup_indirect_dispatch", "startup_indirect_draw_indexed", "startup_present_readback_check", "startup_present_planes", "startup_present_composite_warp", "xmem_shim_test_controls", "startup_stub_xcrdapi", "startup_stub_xcrdapi_break_window_control", "xmem_run_break_readers_check", "xmem_run_break_readers_control", "xmem_cross_call_merge_check", From 8250165ff730569adefbb87cf2ff683b4042a348 Mon Sep 17 00:00:00 2001 From: Roland Edi Date: Mon, 30 Nov 2026 03:01:08 +0000 Subject: [PATCH 3/9] shader: clamp mesh output counts to the declared maxima The real title request stores counts from a formatted load without a proven unsigned bound, so the native frontend refused the count exchange. Clamp the two loaded counts to the layout maxima before setMeshOutputCounts; the real request now passes the exchange and stops later on an undefined incoming VGPR. The source-built real-shape test exercises the clamp path and reports the larger 4,876-byte MS. --- docs/experiments/native-fixed-mesh57.md | 6 +++++- src/gfx1013_direct_mesh.inl | 13 +++++++------ tests/startup_mesh57_real_shape_check.py | 1 + 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/docs/experiments/native-fixed-mesh57.md b/docs/experiments/native-fixed-mesh57.md index f06add3caa..c1892382c6 100644 --- a/docs/experiments/native-fixed-mesh57.md +++ b/docs/experiments/native-fixed-mesh57.md @@ -96,9 +96,13 @@ The native mesh frontend now reads the zero-address VGPR from the first source-built 85-word fixture also has a formatted heap read through `v_readfirstlane_b32`, three DS writes and reads, five barriers, `GS_ALLOC_REQ` and pos0/prim exports. `compile_dxil gfx1013-raw-ms` produces -a 4,828-byte MS and `gfx1013-raw-mesh-ps` a 2,386-byte PS. Changing the zero +a 4,876-byte MS and `gfx1013-raw-mesh-ps` a 2,386-byte PS. Changing the zero VGPR to v5 while the DS fields still name v6 must be refused. +An output count without a proven upper bound is clamped to the layout's +declared maximum vertices or primitives. This moves the real title request +past the count exchange; it stops later on an undefined incoming VGPR. + Reproduce with `ctest --preset dev --no-tests=error -R '^startup_mesh57_real_shape$' -V`. This tranche covers the compiler shape only; the direct-heap runtime profile and the draw/readback are still to be added. diff --git a/src/gfx1013_direct_mesh.inl b/src/gfx1013_direct_mesh.inl index 147b3d14f0..90c8d7b78d 100644 --- a/src/gfx1013_direct_mesh.inl +++ b/src/gfx1013_direct_mesh.inl @@ -453,10 +453,11 @@ NativeComputeShader lower_mesh(const string &code, const MeshStageLayout &layout alu.read(wave)!=wave_number || alu.read(mask)!="1" || alu.read(mask+1)!="0") refuse(pc,"unproven mesh count exchange wave or lane-zero mask"); auto input_v=alu.read(v), input_p=alu.read(p); - if (!upper_bound.count(input_v) || !upper_bound.count(input_p) || - upper_bound.at(input_v)>(std::min)(layout.max_vertices,layout.threads) || - upper_bound.at(input_p)>(std::min)(layout.max_primitives,layout.threads)) - refuse(pc,"mesh count exchange exceeds declared range or lacks an unsigned bound"); + const unsigned max_vertices=(std::min)(layout.max_vertices,layout.threads); + const unsigned max_primitives=(std::min)(layout.max_primitives,layout.threads); + const auto clamp=[&](const string& value,unsigned limit) { + return alu.select(alu.cmp("ugt",value,to_string(limit)),to_string(limit),value); + }; ir.globals<<"@meshCounts = internal addrspace(3) global [2 x i32] undef, align 4\n"; auto ptr0=alu.shared_address("meshCounts",2,"0"),ptr1=alu.shared_address("meshCounts",2,"1"); alu.barrier(); @@ -465,8 +466,8 @@ NativeComputeShader lower_mesh(const string &code, const MeshStageLayout &layout <<" store i32 "< Date: Mon, 30 Nov 2026 03:36:12 +0000 Subject: [PATCH 4/9] shader: add an opt-in partial-EXEC mesh VGPR path A real slot 57 request defines its position VGPRs under a partial EXEC mask and exports them after an LDS branch. The default merge still refuses that shape. X2W_MESH57_PARTIAL_ZERO=1 keeps the last partial value for mesh reads and exports and materializes a missing merge edge as zero. With the opt-in, one captured title request compiles to a 12,680-byte MS and a 2,310-byte PS; without it the same request refuses at graphics register undefined. The source-built real-shape test exercises the opt-in and keeps the wrong-zero-VGPR control refused. The default behavior is unchanged. --- docs/experiments/native-fixed-mesh57.md | 12 ++++++++ src/gfx1013_direct_compute.inl | 24 ++++++++++++++++ src/gfx1013_direct_mesh.inl | 36 ++++++++++++++++++++++-- tests/startup_mesh57_real_shape_check.py | 13 ++++++++- 4 files changed, 81 insertions(+), 4 deletions(-) diff --git a/docs/experiments/native-fixed-mesh57.md b/docs/experiments/native-fixed-mesh57.md index c1892382c6..640080e060 100644 --- a/docs/experiments/native-fixed-mesh57.md +++ b/docs/experiments/native-fixed-mesh57.md @@ -103,6 +103,18 @@ An output count without a proven upper bound is clamped to the layout's declared maximum vertices or primitives. This moves the real title request past the count exchange; it stops later on an undefined incoming VGPR. +## Partial-EXEC VGPRs (opt-in) + +A VGPR defined under a partial EXEC mask and read after an LDS branch is +refused by default because the merge cannot prove which lanes observed the +write. `X2W_MESH57_PARTIAL_ZERO=1` keeps the last partial value for mesh reads +and exports and materializes a missing merge edge as zero. This lets the first +captured title request compile (MS 12,680 bytes, PS 2,310 bytes), while the +default still refuses it. The mode is a heuristic for lanes the shader has not +proven defined; the source-built real-shape test covers the opt-in and the +wrong-zero-VGPR control. It is not the reviewed semantic replacement for +per-VGPR EXEC tracking. + Reproduce with `ctest --preset dev --no-tests=error -R '^startup_mesh57_real_shape$' -V`. This tranche covers the compiler shape only; the direct-heap runtime profile and the draw/readback are still to be added. diff --git a/src/gfx1013_direct_compute.inl b/src/gfx1013_direct_compute.inl index ffa3df1c6d..94c5fcdab5 100644 --- a/src/gfx1013_direct_compute.inl +++ b/src/gfx1013_direct_compute.inl @@ -1,5 +1,10 @@ // Lower the shared decoded CFG to DXIL operations. A scalar register holds one // 32-bit word per wave. Ordinary vector writes retain their old value on inactive lanes. +inline bool mesh_partial_zero() +{ + const char* value=std::getenv("X2W_MESH57_PARTIAL_ZERO"); + return value && value[0]=='1'; +} struct DirectCompute { // The nine inline float constants as binary16, which an f16 operand reads instead of the f32 pattern. static constexpr Word inline_half_words[]{0x3800, 0xb800, 0x3c00, 0xbc00, 0x4000, @@ -99,6 +104,10 @@ struct DirectCompute { std::function graphics_input; std::function after_graphics_write; std::map graphics_registers; + // Last non-empty value of a VGPR written under a partial EXEC. A mesh + // export is observed only on its guard lanes, so it may use this value + // after a full-EXEC reset erased the symbol. + std::map partial_values; string graphics_predicate; unsigned graphics_pc = 0; const State *compute_state = nullptr; @@ -150,6 +159,21 @@ struct DirectCompute { string read(unsigned r) { if (compute_state && compute_state->has_spill(r)) refuse(compute_pc, "symbolic VGPR lane read of " + reg(r)); + if (graphics_input && mesh_partial_zero()) { + const auto found = graphics_registers.find(r); + if (found == graphics_registers.end() || found->second.empty()) { + const auto partial = partial_values.find(r); + if (partial != partial_values.end()) return partial->second; + } + } + return read_storage(r); + } + string read_export(unsigned r) { + if (!mesh_partial_zero()) return read_storage(r); + const auto found = graphics_registers.find(r); + if (found != graphics_registers.end() && !found->second.empty()) return found->second; + const auto partial = partial_values.find(r); + if (partial != partial_values.end()) return partial->second; return read_storage(r); } bool lane_write = false; // v_writelane_b32 updates one lane of a VGPR that may hold symbolic lanes diff --git a/src/gfx1013_direct_mesh.inl b/src/gfx1013_direct_mesh.inl index 90c8d7b78d..8c0a2f8e38 100644 --- a/src/gfx1013_direct_mesh.inl +++ b/src/gfx1013_direct_mesh.inl @@ -110,6 +110,12 @@ NativeComputeShader lower_mesh(const string &code, const MeshStageLayout &layout for (unsigned j=0;j wave_bases, low_group, low_lane, whole_lane, linear_threads; std::set partial_writes; + auto remember_partial=[&] { + for (auto reg : partial_writes) { + const auto it=alu.graphics_registers.find(reg); + if (it!=alu.graphics_registers.end() && !it->second.empty()) alu.partial_values[reg]=it->second; + } + }; bool full_exec = true, allocated = false, position = false, indices = false; std::set parameter_exports; std::optional allocation_word; @@ -224,6 +230,11 @@ NativeComputeShader lower_mesh(const string &code, const MeshStageLayout &layout 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+" ]");}; + const bool partial_zero=mesh_partial_zero(); + for (auto reg : partial_writes) { + const auto it=alu.graphics_registers.find(reg); + if (it!=alu.graphics_registers.end() && !it->second.empty()) alu.partial_values[reg]=it->second; + } std::map> masks; for (unsigned reg=0;reg<=104;reg+=2) { const auto bit=[&](const Symbol &s,const auto ®s) -> string { @@ -241,7 +252,23 @@ NativeComputeShader lower_mesh(const string &code, const MeshStageLayout &layout for (const auto &[reg,v] : first.regs) alu.graphics_registers.try_emplace(reg); for (auto &[reg,v] : alu.graphics_registers) { const auto a=first.regs.find(reg); - if (a==first.regs.end() || a->second.empty() || v.empty()) v.clear(); + const string entry = partial_zero && reg>=256 ? "0" : string{}; + if (a==first.regs.end()) { + if (partial_zero && reg>=256 && !v.empty() && !entry.empty()) { + v=phi(entry,v); + continue; + } + v.clear(); + } else if (a->second.empty() || v.empty()) { + if (partial_zero && reg>=256) { + const auto value=!a->second.empty() ? a->second : v; + if (!value.empty() && !entry.empty()) { + v=a->second.empty() ? phi(entry,value) : phi(value,entry); + continue; + } + } + v.clear(); + } else if (a->second!=v) { const auto av=uniform(a->second), bv=uniform(v); v=phi(a->second,v); @@ -473,6 +500,7 @@ NativeComputeShader lower_mesh(const string &code, const MeshStageLayout &layout for (auto r:{data,addr,vdata,rp,rv,raddr}) alu.write(256+r,{}); alu.write(temp,{}); alu.write(128,{}); full_exec=true; lane_active.clear(); wave_active.clear(); exec_value[0].clear(); exec_value[1].clear(); + remember_partial(); for (auto r : partial_writes) alu.write(r,{}); partial_writes.clear(); allocated=true; pc+=128; continue; @@ -540,7 +568,7 @@ NativeComputeShader lower_mesh(const string &code, const MeshStageLayout &layout const auto id=graphics.output(target,channel); auto &sig=ir.outputs.at(id); const unsigned col=channel-sig.first; - auto bits=alu.read(256+((sources>>(8*c))&255)); + auto bits=alu.read_export(256+((sources>>(8*c))&255)); const bool fp=sig.component==9; ir.invoke("void",fp ? "storeVertexOutput.f32" : "storeVertexOutput.i32",171, {{"i32",to_string(id)},{"i32","0"},{"i8",to_string(col)}, @@ -552,7 +580,7 @@ NativeComputeShader lower_mesh(const string &code, const MeshStageLayout &layout } else if (target==20 && channels==1 && !indices) { indices=true; guarded(primitive_count,[&] { - auto packed=alu.read(256+(sources&255)); + auto packed=alu.read_export(256+(sources&255)); auto culled=alu.cmp("slt",packed,"0"); std::array triangle; for (unsigned c=0;c<3;++c) { @@ -924,6 +952,7 @@ NativeComputeShader lower_mesh(const string &code, const MeshStageLayout &layout if (!((i.name=="s_and_b64" && (i.src[0]==126 || i.src[1]==126)) || (i.name=="s_andn2_b64" && i.src[0]==126) || (i.name=="s_or_b64" && i.src[0]==126 && i.src[1]==126))) { + remember_partial(); for (auto r : partial_writes) alu.write(r,{}); partial_writes.clear(); } @@ -959,6 +988,7 @@ NativeComputeShader lower_mesh(const string &code, const MeshStageLayout &layout } else if (raw==0xbefe04c1) { full_exec=true; export_limit.clear(); + remember_partial(); for (auto r : partial_writes) alu.write(r,{}); partial_writes.clear(); wave_active.clear(); lane_active.clear(); diff --git a/tests/startup_mesh57_real_shape_check.py b/tests/startup_mesh57_real_shape_check.py index 81ba479d43..aaf8de00f1 100644 --- a/tests/startup_mesh57_real_shape_check.py +++ b/tests/startup_mesh57_real_shape_check.py @@ -7,6 +7,7 @@ """ import argparse +import os from pathlib import Path import struct import subprocess @@ -82,9 +83,19 @@ def main(): assert refusal.returncode, "the mismatched zero VGPR must be refused" assert not (work / "control.ms57.cso").exists() + os.environ["X2W_MESH57_PARTIAL_ZERO"] = "1" + partial = run([str(args.compile_dxil), str(dxc), str(cache), "gfx1013-raw-ms", + str(work / "real.m57"), str(work / "real-partial.ms57.cso")]) + assert not partial.returncode, partial.stdout + partial.stderr + assert (work / "real-partial.ms57.cso").stat().st_size > 0 + partial_control = run([str(args.compile_dxil), str(dxc), str(cache), "gfx1013-raw-ms", + str(work / "control.m57"), str(work / "control-partial.ms57.cso")]) + assert partial_control.returncode, "the partial-zero mode must not accept the wrong VGPR" + os.environ.pop("X2W_MESH57_PARTIAL_ZERO") + print("real mesh v6 exchange: MS", (work / "real.ms57.cso").stat().st_size, "bytes, PS", (work / "real.ps57.cso").stat().st_size, - "bytes, mismatched zero VGPR refused") + "bytes, partial-zero accepted, mismatched zero VGPR refused") return 0 From 6f78b21912f17d5cc86f50e7834a75b489220e40 Mon Sep 17 00:00:00 2001 From: Roland Edi Date: Mon, 30 Nov 2026 04:08:55 +0000 Subject: [PATCH 5/9] tooling: dump the derived mesh root layout X2W_MESH_DUMP_LAYOUT=1 prints the guest-root SGPR, root parameter, constant count and resource-heap SGPR used by a raw mesh translation. This is the mapping needed to author a Mesh57Profile from a captured request. --- docs/experiments/native-fixed-mesh57.md | 7 +++++++ tools/startup/compile_dxil.cpp | 7 +++++++ 2 files changed, 14 insertions(+) diff --git a/docs/experiments/native-fixed-mesh57.md b/docs/experiments/native-fixed-mesh57.md index 640080e060..7ce9d4d631 100644 --- a/docs/experiments/native-fixed-mesh57.md +++ b/docs/experiments/native-fixed-mesh57.md @@ -115,6 +115,13 @@ proven defined; the source-built real-shape test covers the opt-in and the wrong-zero-VGPR control. It is not the reviewed semantic replacement for per-VGPR EXEC tracking. +For profile authoring, `X2W_MESH_DUMP_LAYOUT=1` on a `guest_root` raw mesh +request prints the derived SGPR, root parameter and constant count for every +root input, plus the resource-heap SGPR. One captured title request yields +`s16/s17/s18` tables, `s19` root SRV, `s21` root-4 constants of two words +and heap `s23`; that is the mapping used by the first generated +`Mesh57Profile`. + Reproduce with `ctest --preset dev --no-tests=error -R '^startup_mesh57_real_shape$' -V`. This tranche covers the compiler shape only; the direct-heap runtime profile and the draw/readback are still to be added. diff --git a/tools/startup/compile_dxil.cpp b/tools/startup/compile_dxil.cpp index 658b89f045..623f5e9a5d 100644 --- a/tools/startup/compile_dxil.cpp +++ b/tools/startup/compile_dxil.cpp @@ -170,6 +170,13 @@ static x2w::NativeComputeShader translate_raw_mesh(const std::string& source, layout.root_inputs = std::move(mapping.inputs); layout.resource_heap_sgpr = mapping.resource_heap_sgpr; } + if (std::getenv("X2W_MESH_DUMP_LAYOUT")) { + const unsigned heap = layout.resource_heap_sgpr ? *layout.resource_heap_sgpr : 0; + std::fprintf(stderr, "mesh layout heap_sgpr=%u inputs=%zu\n", heap, layout.root_inputs.size()); + for (const auto& input : layout.root_inputs) + std::fprintf(stderr, " sgpr=%u root=%u count=%u root_address=%u\n", + input.sgpr, input.root_parameter, input.constant_count, input.root_address ? 1u : 0u); + } return x2w::translate_mesh_program(source,layout,desc->Desc_1_1); } From 6d0cf2d0baf0e34995b91494cfdd7ef853e1febb Mon Sep 17 00:00:00 2001 From: Roland Edi Date: Mon, 30 Nov 2026 04:42:12 +0000 Subject: [PATCH 6/9] shader: widen the mesh integer ALU subset Accept v_sub_nc_u32, v_subrev_nc_u32, v_add_co_ci_u32, s_cmpk_lg_i32 and s_lshl2_add_u32 in the mesh lowerer, reusing the shared ALU implementations. On twelve captured live slot 57 requests, four depth-only requests already compiled; the new forms move the remaining multi-byte requests past their first ALU refusal. The next stops are heap-slot provenance, v_readfirstlane, a raw root descriptor and an unmapped export target. --- docs/experiments/native-fixed-mesh57.md | 8 ++++++++ src/gfx1013_direct_mesh.inl | 4 ++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/docs/experiments/native-fixed-mesh57.md b/docs/experiments/native-fixed-mesh57.md index 7ce9d4d631..a3bf6f5ff7 100644 --- a/docs/experiments/native-fixed-mesh57.md +++ b/docs/experiments/native-fixed-mesh57.md @@ -122,6 +122,14 @@ root input, plus the resource-heap SGPR. One captured title request yields and heap `s23`; that is the mapping used by the first generated `Mesh57Profile`. +The mesh scalar/vector subset also accepts the ordinary integer forms +`v_sub_nc_u32`, `v_subrev_nc_u32`, `v_add_co_ci_u32`, `s_cmpk_lg_i32` and +`s_lshl2_add_u32`. On twelve captured live requests this moved every +remaining multi-byte request past its first ALU refusal: three now stop at +heap-slot provenance, two at `v_readfirstlane_b32`, two at a raw root buffer +descriptor and one at an unmapped export target. Four depth-only requests +already compile. + Reproduce with `ctest --preset dev --no-tests=error -R '^startup_mesh57_real_shape$' -V`. This tranche covers the compiler shape only; the direct-heap runtime profile and the draw/readback are still to be added. diff --git a/src/gfx1013_direct_mesh.inl b/src/gfx1013_direct_mesh.inl index 8c0a2f8e38..a338a8f75b 100644 --- a/src/gfx1013_direct_mesh.inl +++ b/src/gfx1013_direct_mesh.inl @@ -980,7 +980,7 @@ NativeComputeShader lower_mesh(const string &code, const MeshStageLayout &layout ir.binary("or",old,bit)); } else if (i.name=="s_bitcmp0_b32" || i.name=="s_bitcmp1_b32" || i.name=="s_cmp_lt_u32" || i.name=="s_mulk_i32" || i.name=="s_addk_i32" || i.name=="s_mul_i32" || i.name=="s_add_u32" || i.name=="s_add_i32" || i.name=="s_max_u32" || - i.name=="s_sub_i32" || i.name=="s_mul_hi_u32" || + i.name=="s_sub_i32" || i.name=="s_mul_hi_u32" || i.name=="s_lshl2_add_u32" || i.name=="s_cmpk_lg_i32" || i.name=="s_cmp_gt_i32" || i.name=="s_cmp_lt_i32" || i.name=="s_cmp_ge_i32" || i.name=="s_cmp_eq_u32" || i.name=="s_cmp_eq_i32" || i.name=="s_cmp_lg_u32" || i.name=="s_cmp_ge_u32" || i.name=="s_cmp_lg_i32" || i.name=="s_cmp_gt_u32" || i.name=="s_cselect_b32") { 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"); @@ -1188,7 +1188,7 @@ NativeComputeShader lower_mesh(const string &code, const MeshStageLayout &layout i.name=="v_subrev_f32" || i.name=="v_fmac_f32" || i.name=="v_fmaak_f32" || i.name=="v_rsq_f32" || i.name=="v_rcp_f32" || i.name=="v_sin_f32" || i.name=="v_cos_f32" || i.name.substr(0,15)=="v_cvt_f32_ubyte" || i.name=="v_min_u32" || i.name=="v_max_f32" || - i.name=="v_mad_u32_u24" || i.name=="v_add3_u32" || i.name=="v_bfe_u32" || i.name=="v_mov_b32" || i.name=="v_add_nc_u32" || i.name=="v_lshlrev_b32" || + i.name=="v_mad_u32_u24" || i.name=="v_add3_u32" || i.name=="v_bfe_u32" || i.name=="v_mov_b32" || i.name=="v_add_nc_u32" || i.name=="v_sub_nc_u32" || i.name=="v_subrev_nc_u32" || i.name=="v_add_co_ci_u32" || i.name=="v_lshlrev_b32" || i.name=="v_lshrrev_b32" || i.name=="v_or_b32" || i.name=="v_and_b32" || i.name=="v_xor_b32" || i.name=="v_lshl_add_u32" || i.name=="v_cvt_f32_u32" || i.name=="v_cvt_f32_i32" || i.name=="v_mul_f32" || i.name=="v_add_f32" || i.name=="v_fma_f32")) { From 5a1edb31d87d66ef86584c92c874a5d9f5146f91 Mon Sep 17 00:00:00 2001 From: Roland Edi Date: Mon, 30 Nov 2026 05:30:26 +0000 Subject: [PATCH 7/9] shader: accept byte-count root V# with an OFFEN offset The mesh raw root-buffer path now accepts root_stride_one_high with the 0x31016fac byte-count format, and allows OFFEN to carry a nonzero immediate offset. The live request 314407020a96f733 moves from +0x330 to its next refusal at +0x614, a resource-heap descriptor load. --- docs/experiments/native-fixed-mesh57.md | 6 ++++++ src/gfx1013_direct_compute.inl | 6 ++++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/docs/experiments/native-fixed-mesh57.md b/docs/experiments/native-fixed-mesh57.md index a3bf6f5ff7..82eb155b43 100644 --- a/docs/experiments/native-fixed-mesh57.md +++ b/docs/experiments/native-fixed-mesh57.md @@ -130,6 +130,12 @@ heap-slot provenance, two at `v_readfirstlane_b32`, two at a raw root buffer descriptor and one at an unmapped export target. Four depth-only requests already compile. +The raw root-buffer V# path accepts `root_stride_one_high` with the +`0x31016fac` byte-count format, and OFFEN may carry a nonzero immediate +offset. This moves the live request `314407020a96f733` from +`unsupported raw root buffer descriptor or offset` at `+0x330` to its next +refusal at `+0x614` (a resource-heap descriptor load). + Reproduce with `ctest --preset dev --no-tests=error -R '^startup_mesh57_real_shape$' -V`. This tranche covers the compiler shape only; the direct-heap runtime profile and the draw/readback are still to be added. diff --git a/src/gfx1013_direct_compute.inl b/src/gfx1013_direct_compute.inl index 94c5fcdab5..78bb8dbb8c 100644 --- a/src/gfx1013_direct_compute.inl +++ b/src/gfx1013_direct_compute.inl @@ -918,9 +918,11 @@ struct DirectCompute { // also reads exactly the OFFEN byte window of its root SRV. const bool byte_bounds=s.at(base+1)==Symbol{Symbol::Type::root_address,low.value,1} && s.at(base+3)==Symbol{Symbol::Type::constant,0x31016fac}; - if (low.offset || (!stride_one && !byte_bounds) || + const bool stride_one_byte=s.at(base+1)==Symbol{Symbol::Type::root_stride_one_high,low.value,1} && + s.at(base+3)==Symbol{Symbol::Type::constant,0x31016fac}; + if (low.offset || (!stride_one && !byte_bounds && !stride_one_byte) || !(s.at(base+2) == Symbol{Symbol::Type::constant, 0xffffffff}) || - (i.a & 0x3fff) != 0x1000 || (i.b >> 24) != 128) + (i.a & 0x1000) != 0x1000 || (i.b >> 24) != 128) refuse(i.pc, "unsupported raw root buffer descriptor or offset"); h = root_buffer_handle(low.value, 4*n); } else { From 3d4f93b0ca3a37f687b3b19bae4220a4c80641d0 Mon Sep 17 00:00:00 2001 From: Roland Edi Date: Mon, 30 Nov 2026 08:36:54 +0000 Subject: [PATCH 8/9] shader: cover the slot 57 opt-in, clamp and root V# shapes --- CMakePresets.json | 2 +- docs/experiments/native-fixed-mesh57.md | 34 ++- src/gfx1013_direct_compute.inl | 3 + tests/cmake/startup_mesh57_real_shape.cmake | 5 +- tests/startup_mesh57_real_shape_check.py | 266 ++++++++++++++++---- tools/startup/compile_dxil.cpp | 7 + 6 files changed, 258 insertions(+), 59 deletions(-) diff --git a/CMakePresets.json b/CMakePresets.json index 66db5da502..a324146198 100644 --- a/CMakePresets.json +++ b/CMakePresets.json @@ -49,7 +49,7 @@ "displayName": "Windows pull request smoke tests", "filter": { "include": { - "name": "^(bcpack_all_formats(_golden_control)?|startup_bcpack_storage|desktop_root_signature|era_heap_(allocation|failure)|xmem_shim_mapped_pages|cpu_aot_fuzz_smoke|cpu_archive_texture_gpu_warp|dual_uav_warp|spirv_.*_warp|static_exe_intake|descriptor_heap_paging(_swapped)?_warp|startup_launch|startup_tools_lint|startup_gfx2hlsl_cvt_off_f32_i4_controls|startup_gfx2hlsl_sad_hi_msad_controls|startup_indirect_(dispatch|count_split|draw(_indexed)?|mesh(_xmem_arguments(_skip_wait)?)?|counts_control)_warp|startup_indirect_(dispatch|constants|count)_inline_warp|startup_inline_retire_(on|control)_warp|startup_resource_retire_title_inline_warp_debug0|startup_table_lock_(control_)?warp|startup_indirect_mesh_lease_warp_dual(-trace(-disabled)?)?|startup_shader_cache_key|startup_present_readback|startup_present_planes(_control)?|startup_present_composite_(warp|overlay_removed_control_warp)|xmem_(run_break_readers|cross_call_merge|placeholder_split|map_split_first)(_[a-z_]+)?|startup_dstorage_break_window(_[a-z_]+)?|xmem_page_runs_device(_join)?_warp|linear_buffer_backing_share_warp|startup_stage_package|startup_compute_packed_d16_x_uav_reference)$" + "name": "^(bcpack_all_formats(_golden_control)?|startup_bcpack_storage|desktop_root_signature|era_heap_(allocation|failure)|xmem_shim_mapped_pages|cpu_aot_fuzz_smoke|cpu_archive_texture_gpu_warp|dual_uav_warp|spirv_.*_warp|static_exe_intake|descriptor_heap_paging(_swapped)?_warp|startup_launch|startup_mesh57_real_shape|startup_tools_lint|startup_gfx2hlsl_cvt_off_f32_i4_controls|startup_gfx2hlsl_sad_hi_msad_controls|startup_indirect_(dispatch|count_split|draw(_indexed)?|mesh(_xmem_arguments(_skip_wait)?)?|counts_control)_warp|startup_indirect_(dispatch|constants|count)_inline_warp|startup_inline_retire_(on|control)_warp|startup_resource_retire_title_inline_warp_debug0|startup_table_lock_(control_)?warp|startup_indirect_mesh_lease_warp_dual(-trace(-disabled)?)?|startup_shader_cache_key|startup_present_readback|startup_present_planes(_control)?|startup_present_composite_(warp|overlay_removed_control_warp)|xmem_(run_break_readers|cross_call_merge|placeholder_split|map_split_first)(_[a-z_]+)?|startup_dstorage_break_window(_[a-z_]+)?|xmem_page_runs_device(_join)?_warp|linear_buffer_backing_share_warp|startup_stage_package|startup_compute_packed_d16_x_uav_reference)$" } } }, diff --git a/docs/experiments/native-fixed-mesh57.md b/docs/experiments/native-fixed-mesh57.md index 82eb155b43..03de3ed624 100644 --- a/docs/experiments/native-fixed-mesh57.md +++ b/docs/experiments/native-fixed-mesh57.md @@ -111,9 +111,18 @@ write. `X2W_MESH57_PARTIAL_ZERO=1` keeps the last partial value for mesh reads and exports and materializes a missing merge edge as zero. This lets the first captured title request compile (MS 12,680 bytes, PS 2,310 bytes), while the default still refuses it. The mode is a heuristic for lanes the shader has not -proven defined; the source-built real-shape test covers the opt-in and the -wrong-zero-VGPR control. It is not the reviewed semantic replacement for -per-VGPR EXEC tracking. +proven defined; it is not the reviewed semantic replacement for per-VGPR EXEC +tracking. + +The source-built test adds a scalar join before the count exchange whose +pos0.w source (`v9`) exists on the fall-through edge only. It is refused by +default at `+0x148` with `graphics register undefined on an incoming path: +265` and accepted with the switch, where the IR selects `0` on the edge that +did not write the VGPR. This covers the general `reg>=256` merge rule, not +only registers already marked partial. The wrong-zero-VGPR control asserts +the exact `+0x4` `mesh group-index exchange shape or entry state is not +covered` refusal in both modes. No GPU draw exercises the opt-in yet: the +existing `native_fixed_mesh57_*` fixtures submit different MS bytes. For profile authoring, `X2W_MESH_DUMP_LAYOUT=1` on a `guest_root` raw mesh request prints the derived SGPR, root parameter and constant count for every @@ -130,12 +139,31 @@ heap-slot provenance, two at `v_readfirstlane_b32`, two at a raw root buffer descriptor and one at an unmapped export target. Four depth-only requests already compile. +The source-built test compiles each of the five forms. `v_add_co_ci_u32` +needs a reaching VCC carry word; the mesh entry defines none, so the same +instruction without a preceding `s_mov_b64 vcc, 0` is refused with +`unproven mesh entry register 106`. `v_sub_co_ci_u32` stays outside the +subset. + The raw root-buffer V# path accepts `root_stride_one_high` with the `0x31016fac` byte-count format, and OFFEN may carry a nonzero immediate offset. This moves the live request `314407020a96f733` from `unsupported raw root buffer descriptor or offset` at `+0x330` to its next refusal at `+0x614` (a resource-heap descriptor load). +The source-built compute check compiles an OFFEN load with offset 16 for both +the stride-one and the zero-stride byte-count V#; the emitted address adds the +offset. IDXEN never reaches this check: the raw-load decoder refuses it first +with `raw buffer load needs a linear byte offset and no modifiers`. The +relaxed `(i.a & 0x1000)` mask therefore only admits the immediate offset; a +missing OFFEN still refuses with `unsupported raw root buffer descriptor or +offset`. + +`X2W_DUMP_SHADER_IR=` on `compile_dxil` writes the DXIL text (or the +HLSL source for profiles that still emit HLSL) before `dxc`; the tests use it +to assert the clamp selects, the partial-edge phi and the OFFEN address. The +source-built mesh test is part of the Windows `pr` test filter. + Reproduce with `ctest --preset dev --no-tests=error -R '^startup_mesh57_real_shape$' -V`. This tranche covers the compiler shape only; the direct-heap runtime profile and the draw/readback are still to be added. diff --git a/src/gfx1013_direct_compute.inl b/src/gfx1013_direct_compute.inl index 78bb8dbb8c..4fca04f85f 100644 --- a/src/gfx1013_direct_compute.inl +++ b/src/gfx1013_direct_compute.inl @@ -920,6 +920,9 @@ struct DirectCompute { s.at(base+3)==Symbol{Symbol::Type::constant,0x31016fac}; const bool stride_one_byte=s.at(base+1)==Symbol{Symbol::Type::root_stride_one_high,low.value,1} && s.at(base+3)==Symbol{Symbol::Type::constant,0x31016fac}; + // OFFEN is mandatory and its 12-bit immediate offset is added to the + // address below. IDXEN (bit 13) never reaches this check: the raw + // load decoder refuses it, so only OFFEN has to be tested here. if (low.offset || (!stride_one && !byte_bounds && !stride_one_byte) || !(s.at(base+2) == Symbol{Symbol::Type::constant, 0xffffffff}) || (i.a & 0x1000) != 0x1000 || (i.b >> 24) != 128) diff --git a/tests/cmake/startup_mesh57_real_shape.cmake b/tests/cmake/startup_mesh57_real_shape.cmake index 12421af83e..a8fdd0c669 100644 --- a/tests/cmake/startup_mesh57_real_shape.cmake +++ b/tests/cmake/startup_mesh57_real_shape.cmake @@ -1,5 +1,6 @@ -# Source-built real-shape slot 57 mesh: compile the MS and PS through the native frontend and -# require the v6 group-index exchange to be accepted, with a mismatched zero VGPR refused. +# Source-built slot 57 shapes: the real MS/PS pair, the partial-EXEC opt-in refused by default +# and accepted with X2W_MESH57_PARTIAL_ZERO=1, the exact wrong-zero-VGPR refusal, the count clamp, +# the widened integer ALU subset and a byte-count root V# with an OFFEN offset. if(EXISTS "${DXC_EXECUTABLE}" AND TARGET compile_dxil) add_test(NAME startup_mesh57_real_shape COMMAND "${INTAKE_PYTHON}" -B "${CMAKE_SOURCE_DIR}/tests/startup_mesh57_real_shape_check.py" diff --git a/tests/startup_mesh57_real_shape_check.py b/tests/startup_mesh57_real_shape_check.py index aaf8de00f1..58ce0664fb 100644 --- a/tests/startup_mesh57_real_shape_check.py +++ b/tests/startup_mesh57_real_shape_check.py @@ -1,18 +1,30 @@ -"""Compile a source-built real-shape slot 57 mesh and its negative control. +"""Compile source-built slot 57 shapes and the shared raw-root descriptor path. -The fixture has the v6 group-index exchange, a formatted heap read through +The mesh fixture has the v6 group-index exchange, a formatted heap read through v_readfirstlane_b32, the three DS writes and reads, five barriers, GS_ALLOC_REQ and pos0/prim exports. The MS and PS are compiled separately by the native frontend; this test does not submit a GPU draw. + +The partial-EXEC opt-in uses a join whose pos0.w source exists on one incoming +edge only: refused by default, accepted with X2W_MESH57_PARTIAL_ZERO=1, and the +emitted IR must select 0 on the edge that did not write it. The wrong-zero-VGPR +control and the ALU preconditions assert their exact refusal text. The count +clamp and a byte-count root V# with an OFFEN offset are checked in the DXIL +text, which compile_dxil dumps through X2W_DUMP_SHADER_IR. """ import argparse import os from pathlib import Path +import re import struct import subprocess +import sys import tempfile +sys.path.insert(0, str(Path(__file__).resolve().parent)) +from native_compute_clear_check import write_request + MS = ( "bfa00003 befe0481 be84047e 8706ff03 0f000000 bf850007 2c0a0a87 7e0c0280 " @@ -30,13 +42,67 @@ PS = "7e0002f2 f800180f 00000000 bf810000".split() +MS_LAYOUT = "mesh1 128 3 1 2 128 64 guest_root validate_uint_heap_reads" +MESH_ROOT = ('#define RS "RootFlags(CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED),' + 'RootConstants(num32BitConstants=2,b0)"\n') +COMPUTE_ROOT = '#define RS "RootConstants(num32BitConstants=2,b1),CBV(b0),SRV(t0)"\n' +COMPUTE_SA = struct.pack("<7I", 0, 0, 0, 32 | (1 << 16), 1 | (1 << 20), 0, 10 << 1) +END = 0xbf810000 + def words(text): - return b"".join(struct.pack(" 0 - - ps = run([str(args.compile_dxil), str(dxc), str(cache), "gfx1013-raw-mesh-ps", - str(work / "real.ps57"), str(work / "real.ps57.cso")]) + with tempfile.TemporaryDirectory(prefix="mesh57-real-") as temp: + work = Path(temp) + root = root_signature(dxc, work, MESH_ROOT) + + real, real_ir = mesh_compile(args.compile_dxil, dxc, work / "real", MS, root, dump_ir=True) + assert not real.returncode, real.stdout + real.stderr + assert (work / "real" / "real.ms57.cso").stat().st_size > 0 + assert "requires_heap_r32_uint root=0 dword=0 mask=1023" in real.stdout, real.stdout + ps = ps_compile(args.compile_dxil, dxc, work / "ps") assert not ps.returncode, ps.stdout + ps.stderr - assert (work / "real.ps57.cso").stat().st_size > 0 - - control = list(MS) - control[7] = "7e0a0280" # zero address in v5, DS fields still name v6 - (work / "control.m57").write_bytes(words(control)) - (work / "control.mslayout").write_text( - "mesh1 128 3 1 2 128 64 guest_root validate_uint_heap_reads\n", encoding="ascii") - refusal = run([str(args.compile_dxil), str(dxc), str(cache), "gfx1013-raw-ms", - str(work / "control.m57"), str(work / "control.ms57.cso")]) - assert refusal.returncode, "the mismatched zero VGPR must be refused" - assert not (work / "control.ms57.cso").exists() - - os.environ["X2W_MESH57_PARTIAL_ZERO"] = "1" - partial = run([str(args.compile_dxil), str(dxc), str(cache), "gfx1013-raw-ms", - str(work / "real.m57"), str(work / "real-partial.ms57.cso")]) - assert not partial.returncode, partial.stdout + partial.stderr - assert (work / "real-partial.ms57.cso").stat().st_size > 0 - partial_control = run([str(args.compile_dxil), str(dxc), str(cache), "gfx1013-raw-ms", - str(work / "control.m57"), str(work / "control-partial.ms57.cso")]) - assert partial_control.returncode, "the partial-zero mode must not accept the wrong VGPR" - os.environ.pop("X2W_MESH57_PARTIAL_ZERO") - - print("real mesh v6 exchange: MS", (work / "real.ms57.cso").stat().st_size, - "bytes, PS", (work / "real.ps57.cso").stat().st_size, - "bytes, partial-zero accepted, mismatched zero VGPR refused") + assert (work / "ps" / "real.ps57.cso").stat().st_size > 0 + + # X2W_MESH_DUMP_LAYOUT prints the derived entry mapping on stderr. + dumped, _ = mesh_compile(args.compile_dxil, dxc, work / "layout", MS, root, + extra={"X2W_MESH_DUMP_LAYOUT": "1"}) + assert not dumped.returncode, dumped.stderr + assert "mesh layout heap_sgpr=18 inputs=1" in dumped.stderr, dumped.stderr + assert " sgpr=16 root=0 count=2 root_address=0" in dumped.stderr, dumped.stderr + + # The opt-in shape: a scalar branch defines pos0.w (v9) on its + # fall-through edge and jumps straight to the export on the other. + # Both edges reach the same exp pos0. + partial = list(MS) + partial[41:41] = [0xbf008006, 0xbf840001, 0x7e1202f2] # cmp s6,0; cbranch +1; v_mov v9,1.0 + assert partial[83] == "04030201" + partial[83] = "09030201" # exp pos0 channel 3 reads v9 + refused, _ = mesh_compile(args.compile_dxil, dxc, work / "partial-default", partial, root) + expect_refusal(refused, "native compute +0x148: graphics register undefined on an incoming path: 265", + work / "partial-default" / "real.ms57.cso") + accepted, ir = mesh_compile(args.compile_dxil, dxc, work / "partial-opt-in", partial, root, + extra={"X2W_MESH57_PARTIAL_ZERO": "1"}, dump_ir=True) + assert not accepted.returncode, accepted.stdout + accepted.stderr + assert (work / "partial-opt-in" / "real.ms57.cso").stat().st_size > 0 + phi = re.search(r"(%t\d+) = phi i32 \[ 0, %mesh_edge\d+ \], \[ 1065353216, %mesh_edge\d+ \]", ir) + assert phi, "the opt-in must select zero on the edge that did not write the VGPR:\n" + ir + exported = re.search(r"(%t\d+) = bitcast i32 " + re.escape(phi.group(1)) + r" to float", ir) + assert exported, ir + assert re.search(r"storeVertexOutput\.f32\(i32 171, i32 0, i32 0, i8 3, float " + + re.escape(exported.group(1)) + r",", ir), ir + + # The same VGPR number leaves the exchange matcher: this is the + # zero_vgpr!=5 gate, not the partial vector. + wrong = list(MS) + wrong[7] = 0x7e0a0280 # zero address in v5, DS fields still name v6 + for mode, extra in (("default", None), ("partial", {"X2W_MESH57_PARTIAL_ZERO": "1"})): + case = work / ("wrong-zero-" + mode) + result, _ = mesh_compile(args.compile_dxil, dxc, case, wrong, root, extra=extra) + expect_refusal(result, "native compute +0x4: mesh group-index exchange shape or entry state is not covered", + case / "real.ms57.cso") + + # Counts above the declared mesh1 maxima are clamped, not refused. + clamp = list(MS) + assert clamp[39] == "870d8181" and clamp[40] == "870e8383" + clamp[39:41] = [0xbe8d03ff, 0x3e8, 0xbe8e03ff, 0x3e8] # s13=1000, s14=1000 + clamped, clamp_ir = mesh_compile(args.compile_dxil, dxc, work / "clamp", clamp, root, dump_ir=True) + assert not clamped.returncode, clamped.stdout + clamped.stderr + assert clamp_ir.count("store i32 1000,") == 2, clamp_ir + vertex_select = re.search(r"(%t\d+) = select i1 %t\d+, i32 3, i32 %t\d+", clamp_ir) + primitive_select = re.search(r"(%t\d+) = select i1 %t\d+, i32 1, i32 %t\d+", clamp_ir) + counts = re.search(r"call void @dx\.op\.setMeshOutputCounts\(i32 168, i32 (%t\d+), i32 (%t\d+)\)", clamp_ir) + assert vertex_select and primitive_select and counts, clamp_ir + assert counts.group(1) == vertex_select.group(1) and counts.group(2) == primitive_select.group(1), clamp_ir + + # The widened mesh integer ALU subset. v_add_co_ci_u32 needs a reaching + # VCC carry word; the mesh entry has none, which is asserted below. + alu = { + "sub_nc": [0x4c120501], # v_sub_nc_u32 v9, v1, v2 + "subrev_nc": [0x4e140501], # v_subrev_nc_u32 v10, v1, v2 + "add_co_ci": [0xbeea0480, 0x50160501], # s_mov_b64 vcc,0; v_add_co_ci_u32 v11, v1, v2 + "lshl2_add": [0x978f0706], # s_lshl2_add_u32 s15, s6, s7 + "cmpk_lg": [0xb2060000], # s_cmpk_lg_i32 s6, 0 + } + alu_ir = {} + for name, inserted in alu.items(): + code = list(MS) + code[0x144 // 4:0x144 // 4] = inserted + result, ir = mesh_compile(args.compile_dxil, dxc, work / ("alu-" + name), code, root, dump_ir=True) + assert not result.returncode, name + ": " + result.stdout + result.stderr + assert (work / ("alu-" + name) / "real.ms57.cso").stat().st_size > 0 + alu_ir[name] = ir + assert re.search(r"shl i32 %t\d+, 2\b", alu_ir["lshl2_add"]), alu_ir["lshl2_add"] + assert not re.search(r"shl i32 %t\d+, 2\b", real_ir), "the baseline must not already shift by two" + + missing_vcc = list(MS) + missing_vcc[0x144 // 4:0x144 // 4] = [0x50160501] + result, _ = mesh_compile(args.compile_dxil, dxc, work / "alu-no-vcc", missing_vcc, root) + expect_refusal(result, "native compute +0x144: unproven mesh entry register 106", + work / "alu-no-vcc" / "real.ms57.cso") + + # Boundary of the same subset: v_sub_co_ci_u32 stays unlisted. + outside = list(MS) + outside[0x144 // 4:0x144 // 4] = [0x52160501] + result, _ = mesh_compile(args.compile_dxil, dxc, work / "alu-outside", outside, root) + expect_refusal(result, "native compute +0x144: unsupported mesh instruction or EXEC state: v_sub_co_ci_u32", + work / "alu-outside" / "real.ms57.cso") + + # The byte-count root V# accepts a nonzero OFFEN offset in the shared + # compute lowering. The raw-load decoder still refuses IDXEN before + # this path, so only the immediate offset widened. + compute_root = root_signature(dxc, work / "compute-root", COMPUTE_ROOT) + + def vsharp(stride_one, fmt=0x31016fac): + code = [0xbe880306, 0xbe890307] # s_mov_b32 s8, s6 ; s_mov_b32 s9, s7 + if stride_one: + code += [0x8809ff09, 0x10000] # s_or_b32 s9, s9, 0x10000 + code += [0xbe8a03c1, 0xbe8b03ff, fmt] # s10=-1 ; s11=format + return code + + def root_load(offen=True, idxen=False, offset=0): + return [0xe0300000 | (0x1000 if offen else 0) | (0x2000 if idxen else 0) | offset, + 0x80020101] # buffer_load_dword v0, v1, s[8:11], soffset 0 + + def compute_compile(case, code): + case.mkdir(parents=True, exist_ok=True) + marker = write_request(case, code, compute_root, COMPUTE_SA) + env = clean_environment() + env["X2W_DUMP_SHADER_IR"] = str(case / "ir.ll") + result = run([str(args.compile_dxil), str(dxc), str(case / "cache"), + "gfx1013-direct-cs@0", str(marker), str(marker.with_suffix(".cso"))], env=env) + ir = (case / "ir.ll").read_text() if (case / "ir.ll").exists() else "" + return result, ir + + for name, stride_one, load in ( + ("offen-stride-one", True, root_load(offset=16)), + ("offen-zero-stride", False, root_load(offset=16))): + code = [*vsharp(stride_one), 0x7e020280, *load, END] + result, ir = compute_compile(work / ("root-" + name), code) + assert not result.returncode, name + ": " + result.stderr + assert re.search(r"add i32 %t\d+, 16", ir), "OFFEN offset is not part of the address:\n" + ir + + for name, load, text in ( + ("no-offen", root_load(offen=False), + "native compute +0x20: unsupported raw root buffer descriptor or offset"), + ("idxen", root_load(idxen=True), + "native compute +0x20: raw buffer load needs a linear byte offset and no modifiers")): + code = [*vsharp(True), 0x7e020280, *load, END] + result, _ = compute_compile(work / ("root-" + name), code) + assert result.returncode, name + ": expected a refusal" + assert result.stderr.strip() == text, result.stderr + + print("real mesh v6 exchange:", (work / "real" / "real.ms57.cso").stat().st_size, "bytes,", + (work / "ps" / "real.ps57.cso").stat().st_size, "PS bytes; partial-zero refused by", + "default and accepted with the switch; clamp, ALU subset and root V# checked") return 0 diff --git a/tools/startup/compile_dxil.cpp b/tools/startup/compile_dxil.cpp index 623f5e9a5d..e55f923a86 100644 --- a/tools/startup/compile_dxil.cpp +++ b/tools/startup/compile_dxil.cpp @@ -331,6 +331,13 @@ static std::optional compile_one(x2w::ShaderDxilCompiler& compiler, source=std::move(program.hlsl); profile=L"ps_6_0"; } + // Test-only observation point: the DXIL text (or the HLSL source for the + // profiles that still emit HLSL) exactly as it reaches the compiler. + if (const char *dump=std::getenv("X2W_DUMP_SHADER_IR")) { + std::ofstream file(dump,std::ios::binary); + file << source; + if (!file) throw std::runtime_error("could not write the shader IR dump"); + } compile_source(compiler,source,profile,destination,cache,direct,report); return manifest; } From e6ee7309ab1d7ca62da590de3977ab804d796b9f Mon Sep 17 00:00:00 2001 From: Roland Edi Date: Mon, 30 Nov 2026 09:39:21 +0000 Subject: [PATCH 9/9] tooling: document the shader IR dump variable --- tools/startup/compile_dxil.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/tools/startup/compile_dxil.cpp b/tools/startup/compile_dxil.cpp index e55f923a86..0347c0e289 100644 --- a/tools/startup/compile_dxil.cpp +++ b/tools/startup/compile_dxil.cpp @@ -1588,6 +1588,7 @@ int wmain(int argc, wchar_t** argv) { "gfx1013-container-rect also emits the rectangle geometry stage.\n" "Use --refusals REQUEST_DIRECTORY [--jobs N] [--json] [--mesh-layout FILE] for a read-only refusal summary.\n" "Use --watch SDK_DIRECTORY CACHE_DIRECTORY REQUEST_DIRECTORY [--jobs N] [--interval MS] [--once] to translate pending requests.\n" + "Set X2W_DUMP_SHADER_IR=FILE to write the HLSL or DXIL text handed to the compiler.\n" "Use --rekey OLD_CACHE_DIR NEW_REQUEST_DIR [--jobs N] to copy requests under the current cache revision keys.\n" "Use --index-graphics-interfaces REVIEWED_DIRECTORY CACHE_DIRECTORY to index native graphics interfaces.\n" "Use --prepare-compute-pack REQUEST_DIRECTORY CACHE_DIRECTORY ENTRY_BYTE_OFFSET for captured compute.\n";