From 0e29f16331bdb60a8e779fe4e2b53ec7e372ed5c Mon Sep 17 00:00:00 2001 From: RavenCurseBurg <360931527+RavenCurseBurg@users.noreply.github.com> Date: Thu, 26 Nov 2026 14:55:04 +0800 Subject: [PATCH 1/2] Guard compute uniform-load proofs across EXEC-changing loops --- docs/experiments.md | 2 +- docs/startup-runtime.md | 5 ++-- src/startup_stubs/d3d12_draw.inl | 2 +- .../startup_compute_request_offline_check.py | 4 +-- tests/startup_gfx2hlsl_compute_check.py | 26 +++++++++++++++++++ ...hlsl_readfirstlane_load_recompute_check.py | 8 ++++++ tools/startup/gfx2hlsl.py | 23 ++++++++++++++-- 7 files changed, 62 insertions(+), 8 deletions(-) diff --git a/docs/experiments.md b/docs/experiments.md index 2e93287b17..f521f5f97d 100644 --- a/docs/experiments.md +++ b/docs/experiments.md @@ -15905,7 +15905,7 @@ Past the entry block of a program with branches, the read does not copy the VGPR A lane that was inactive at the definition keeps an older VGPR value; the recomputed expression gives every lane the first active lane's value instead. `exec` need not be known at the definition. A conditional branch between the definition and the read is a fall-through, not a join, and is accepted. The one case where hardware can differ is a read with no active lane: it returns lane 0's VGPR, which the program cannot rely on. `tests/startup_gfx2hlsl_readfirstlane_recompute_check.py` checks the accepted forms (including one-path scalars and uniform loop counters both inside and after loops) and refuses each condition above, including a definition under a narrower `exec` than the read, a rewritten scalar input, a loop back edge, a divergent `else`-arm write before a join, a non-uniform loop back-edge and a join where two definitions reach. -A compute buffer load through a read-only descriptor and uniform address is also accepted past the entry block when the entry EXEC mask is still known. The lowering saves the uniform value in a function-scope temporary so the read uses the value held when the load ran, even if the offset scalar later changes. A narrowed mask may be empty. In that case RDNA 2 reads the previous lane-zero VGPR, which an unguarded reload cannot reproduce. Branch targets, EXEC writes and VGPR overwrites invalidate the proof between the load and read. The source-built controls cover scalar and vector addresses, the second dword, formatted loads, and a lane-zero sentinel followed by empty EXEC. +A compute buffer load through a read-only descriptor and uniform address is also accepted past the entry block when the entry EXEC mask is still known. The lowering saves the uniform value in a function-scope temporary so the read uses the value held when the load ran, even if the offset scalar later changes. A narrowed mask may be empty. In that case RDNA 2 reads the previous lane-zero VGPR, which an unguarded reload cannot reproduce. Branch targets, EXEC writes and VGPR overwrites invalidate the proof between the load and read. A loop may revisit the load after changing EXEC. The lowering follows control-flow edges from EXEC writers to catch this case, including detours outside the loop's instruction range. A loop that changes EXEC only after it exits still qualifies. The source-built controls cover scalar and vector addresses, the second dword, formatted loads, and a lane-zero sentinel followed by empty EXEC. [`tests/startup_compute_readfirstlane_check.py`](../tests/startup_compute_readfirstlane_check.py) pairs that unit coverage with a source-built gfx1013 compute fixture (`(4, 1, 1)` local threads x `(2, 1, 1)` groups) containing both a one-path SGPR across a uniform forward branch join and a four-iteration uniform loop counter read via `v_readfirstlane_b32` inside the loop and after exit, machine-code refusal patches (non-uniform `s_cbranch_vccnz` back-edge, divergent `else`/`then` write before a join, lane-mask loop update) and 320-word GPU readback with pinned `dropped-one-path`, `dropped-loop-read` and `dropped-post-loop` negative controls. Verified with zero mismatches and zero D3D12 debug-layer errors or warnings on Windows 11 25H2 (build 26200.9974) across WARP 10.0.26100.9818 and an AMD Radeon RX 7900 XTX (driver 32.0.32029.2010). diff --git a/docs/startup-runtime.md b/docs/startup-runtime.md index 9f36135d59..fa248a9c04 100644 --- a/docs/startup-runtime.md +++ b/docs/startup-runtime.md @@ -209,8 +209,9 @@ The translation is refused when: - the code reads an attribute the table does not list. The shader cache revision is `x2w-startup-shader-20`. Revision 20 decodes MTBUF/MUBUF format 50 (`R10G10B10A2_UNORM`) in the run-time FORMAT switch, so cached shaders from revision 19 are not reused. Revision 19 folds `s_mov_b32` from a scalar register that -holds a plain constant, so later reads of the copy use the constant word. The matching compute -(`x2w-startup-shader-20cs`) key advances because that stage uses the shared lowering. The fixed slot 57 mesh +holds a plain constant, so later reads of the copy use the constant word. Compute revision +`x2w-startup-shader-21cs` also invalidates shaders that assumed the entry EXEC mask at a load reached +on a later loop iteration after an EXEC write. The fixed slot 57 mesh (`x2w-startup-mesh-57-3`) key stays, because that shader is not translated from guest code. Revisions before 19 read a register copied from a constant as a run-time value. Revision 18 preserves the sign of zero in floating-point min, max, min3, max3 and med3. diff --git a/src/startup_stubs/d3d12_draw.inl b/src/startup_stubs/d3d12_draw.inl index 00a7b5e8a8..b95b8382f2 100644 --- a/src/startup_stubs/d3d12_draw.inl +++ b/src/startup_stubs/d3d12_draw.inl @@ -474,7 +474,7 @@ u64 publish_compute_request(const void* desc) std::vector fields(sizeof(words)); words[0] = words[1] = words[3] = words[5] = words[7] = words[9] = words[10] = 0; std::memcpy(fields.data(), words, sizeof(words)); - constexpr char compute_revision[] = "x2w-startup-shader-20cs"; // tools/startup/gfx2hlsl.py compute_key + constexpr char compute_revision[] = "x2w-startup-shader-21cs"; // tools/startup/gfx2hlsl.py compute_key u64 key = fnv_bytes(compute_revision, sizeof(compute_revision) - 1); const std::vector* order[] = {&code, &data, &root_blob, &fields, &state_a, &state_b}; for (const auto* part : order) key = fnv_part(*part, key); diff --git a/tests/startup_compute_request_offline_check.py b/tests/startup_compute_request_offline_check.py index 3b6c7fec31..46042f18ff 100644 --- a/tests/startup_compute_request_offline_check.py +++ b/tests/startup_compute_request_offline_check.py @@ -25,12 +25,12 @@ DESC = struct.pack('<12Q', 0, 0, 24, 0, 20, 0, len(CODE), 0, len(DATA), 0, 0, 11) STATE_A = struct.pack('<3Q', 1, 0x2c, 0xfff0000000000003) STATE_B = struct.pack('<5I', 0x1234abcd, 0xffff0000, 0, 0, 0) -KEY = 0x88e751913fcbf7a8 # framed key at revision 20cs +KEY = 0x90041d02ef75b4e9 # framed key at revision 21cs def framed_key(): h = 1469598103934665603 - for b in b'x2w-startup-shader-20cs': + for b in b'x2w-startup-shader-21cs': h = ((h ^ b) * 1099511628211) & 0xffffffffffffffff for part in (CODE, DATA, ROOT, DESC, STATE_A, STATE_B): for b in struct.pack(' Date: Thu, 26 Nov 2026 15:06:23 +0800 Subject: [PATCH 2/2] Keep the graphics and compute cache revision paired at 21 --- docs/startup-runtime.md | 9 +++++---- src/startup_stubs/d3d12_draw.inl | 2 +- tests/startup_gfx2hlsl_buffer_store_check.py | 4 ++-- tests/startup_gfx2hlsl_const_copy_join_check.py | 2 +- tests/startup_gfx2hlsl_fract_check.py | 4 ++-- tests/startup_indirect_draw_indexed.cpp | 2 +- tests/startup_packed_position_provenance_check.py | 2 +- tests/startup_resource_retire_title.cpp | 2 +- tests/startup_shader_cache_key_check.py | 6 +++--- tools/startup/gfx2hlsl.py | 2 +- 10 files changed, 18 insertions(+), 17 deletions(-) diff --git a/docs/startup-runtime.md b/docs/startup-runtime.md index fa248a9c04..b12ebe5cdb 100644 --- a/docs/startup-runtime.md +++ b/docs/startup-runtime.md @@ -208,10 +208,11 @@ The translation is refused when: - either header has another size; - the code reads an attribute the table does not list. -The shader cache revision is `x2w-startup-shader-20`. Revision 20 decodes MTBUF/MUBUF format 50 (`R10G10B10A2_UNORM`) in the run-time FORMAT switch, so cached shaders from revision 19 are not reused. Revision 19 folds `s_mov_b32` from a scalar register that -holds a plain constant, so later reads of the copy use the constant word. Compute revision -`x2w-startup-shader-21cs` also invalidates shaders that assumed the entry EXEC mask at a load reached -on a later loop iteration after an EXEC write. The fixed slot 57 mesh +Graphics and compute use cache revisions `x2w-startup-shader-21` and `x2w-startup-shader-21cs`. +Revision 21 invalidates compute shaders that assumed the entry EXEC mask at a load reached on a later +loop iteration after an EXEC write. The two cache keys advance together. +Revision 20 decodes MTBUF/MUBUF format 50 (`R10G10B10A2_UNORM`) in the run-time FORMAT switch. +Revision 19 folds `s_mov_b32` from a scalar register that holds a plain constant, so later reads of the copy use the constant word. The fixed slot 57 mesh (`x2w-startup-mesh-57-3`) key stays, because that shader is not translated from guest code. Revisions before 19 read a register copied from a constant as a run-time value. Revision 18 preserves the sign of zero in floating-point min, max, min3, max3 and med3. diff --git a/src/startup_stubs/d3d12_draw.inl b/src/startup_stubs/d3d12_draw.inl index b95b8382f2..04078a452e 100644 --- a/src/startup_stubs/d3d12_draw.inl +++ b/src/startup_stubs/d3d12_draw.inl @@ -353,7 +353,7 @@ Translation* cached_translation(const void* a, std::size_t a_bytes, const void* { // Keep pre-fix desktop bytecode and manifests out of this ABI's cache. // tools/startup/gfx2hlsl.py hashes the same bytes, without a trailing NUL. - constexpr char revision[] = "x2w-startup-shader-20"; + constexpr char revision[] = "x2w-startup-shader-21"; const u64 seed = fnv_bytes(revision, sizeof(revision) - 1); u64 key = fnv_bytes(root_blob.data(), root_blob.size(), fnv_bytes(b, b_bytes, fnv_bytes(a, a_bytes, seed))); // A stream's input layout (stream_input_layout) feeds the vertex fetch, so it is part of the key. diff --git a/tests/startup_gfx2hlsl_buffer_store_check.py b/tests/startup_gfx2hlsl_buffer_store_check.py index 1e2146ea83..1def88ebb1 100644 --- a/tests/startup_gfx2hlsl_buffer_store_check.py +++ b/tests/startup_gfx2hlsl_buffer_store_check.py @@ -212,8 +212,8 @@ def main(): '%s %s' % (result, fail_reason(name))) # 8. The cache revision moved, so a translation from before the store lowering is never loaded. - check('cache key uses revision 19', G.pipeline_key(b'a', b'b', ROOT) == - G.fnv(ROOT, G.fnv(b'b', G.fnv(b'a', G.fnv(b'x2w-startup-shader-20'))))) + check('cache key uses revision 21', G.pipeline_key(b'a', b'b', ROOT) == + G.fnv(ROOT, G.fnv(b'b', G.fnv(b'a', G.fnv(b'x2w-startup-shader-21'))))) if failures: print('FAILED: ' + ', '.join(failures)) diff --git a/tests/startup_gfx2hlsl_const_copy_join_check.py b/tests/startup_gfx2hlsl_const_copy_join_check.py index b1756e1102..c771b7ca27 100644 --- a/tests/startup_gfx2hlsl_const_copy_join_check.py +++ b/tests/startup_gfx2hlsl_const_copy_join_check.py @@ -1,6 +1,6 @@ """`s_mov_b32` copy of a plain constant SGPR in tools/startup/gfx2hlsl.py: run-time write and join. -The copy folds the constant into later reads (revision x2w-startup-shader-20), and these two cases pin what must +The copy folds the constant into later reads (revision x2w-startup-shader-21), and these two cases pin what must stay true next to the fold: - the copy still assigns its register at run time (`s12 = ...;` is emitted), so a reader the fold does not reach (a branch target after a join) sees a real value; diff --git a/tests/startup_gfx2hlsl_fract_check.py b/tests/startup_gfx2hlsl_fract_check.py index 246f1f6610..45b7491ba2 100644 --- a/tests/startup_gfx2hlsl_fract_check.py +++ b/tests/startup_gfx2hlsl_fract_check.py @@ -170,8 +170,8 @@ def float_capped(word): repr((seed and seed.group(1), literal and literal.group(1)))) check('the two literals are identical', seed and literal and seed.group(1) == literal.group(1), repr((seed and seed.group(1), literal and literal.group(1)))) -check('the revision is x2w-startup-shader-20, after format 50 (R10G10B10A2_UNORM) joined the FORMAT switch', - bool(seed and seed.group(1) == 'x2w-startup-shader-20'), repr(seed and seed.group(1))) +check('the revision is x2w-startup-shader-21, after the compute loop entry-EXEC guard', + bool(seed and seed.group(1) == 'x2w-startup-shader-21'), repr(seed and seed.group(1))) def key_from_seed(word): diff --git a/tests/startup_indirect_draw_indexed.cpp b/tests/startup_indirect_draw_indexed.cpp index 1169681257..514f9d566d 100644 --- a/tests/startup_indirect_draw_indexed.cpp +++ b/tests/startup_indirect_draw_indexed.cpp @@ -295,7 +295,7 @@ ComPtr translated_pipeline(ID3D12Device* guest, ID3D12RootSignature* r bool needs_start, bool table_buffer = false, bool indexed = false, bool rect = false) { // The opaque guest programs identify synthetic cache entries. The actual bound shaders are source-built DXBC. - constexpr char revision[] = "x2w-startup-shader-20"; + constexpr char revision[] = "x2w-startup-shader-21"; static std::uint32_t vertex_ids[]{0xe0000101, 0xe0000102, 0xe0000103}; static std::uint32_t pixel_ids[]{0xe0000202, 0xe0000203, 0xe0000204}; auto& vertex_id = vertex_ids[indexed ? 2 : needs_start]; diff --git a/tests/startup_packed_position_provenance_check.py b/tests/startup_packed_position_provenance_check.py index 8620af940c..c701eb64ec 100644 --- a/tests/startup_packed_position_provenance_check.py +++ b/tests/startup_packed_position_provenance_check.py @@ -206,7 +206,7 @@ def refused(label, cache, key, reason): cache = Path(tmp) bare = G.pipeline_key(VS_WORDS, ps_words(4), root_blob()) check('without a description the key is unchanged', - bare == G.fnv(root_blob(), G.fnv(ps_words(4), G.fnv(VS_WORDS, G.fnv(b'x2w-startup-shader-20'))))) + bare == G.fnv(root_blob(), G.fnv(ps_words(4), G.fnv(VS_WORDS, G.fnv(b'x2w-startup-shader-21'))))) keys = [put_request(cache, ps_words(4), masks) for masks in (TARGET, SAME_V4, DECOY)] check('same stages, root and layout with three mask pairs: three request identities', len(set(keys)) == 3 and bare not in keys diff --git a/tests/startup_resource_retire_title.cpp b/tests/startup_resource_retire_title.cpp index 979ba9a44f..f750b74376 100644 --- a/tests/startup_resource_retire_title.cpp +++ b/tests/startup_resource_retire_title.cpp @@ -403,7 +403,7 @@ constexpr UINT draw_side = 4; // 4x4 R8G8B8A8_UNORM target constexpr UINT draw_covered = 3; // scissor leaves the last row and column untouched as the sentinel constexpr int draw_seed = 77; constexpr UINT draw_clear_pixel = 0xffff00ffu; // ClearRenderTargetView({1, 0, 1, 1}) in R8G8B8A8_UNORM -constexpr char draw_revision[] = "x2w-startup-shader-20"; // d3d12_draw.inl cached_translation +constexpr char draw_revision[] = "x2w-startup-shader-21"; // d3d12_draw.inl cached_translation constexpr char draw_vertex_shader[] = R"( struct Varying { float4 position : SV_Position; }; diff --git a/tests/startup_shader_cache_key_check.py b/tests/startup_shader_cache_key_check.py index fd44bba115..8c6fccb3b9 100644 --- a/tests/startup_shader_cache_key_check.py +++ b/tests/startup_shader_cache_key_check.py @@ -49,10 +49,10 @@ def check_request(blobs, key, diagnostic=None, rtv_request=None, layout=None): def main(): # Independently executed C++ runtime fnv_bytes/cached_translation known-answer vector. - # Revision x2w-startup-shader-20 excludes NUL. Stored order is a, b, root, then optional input layout. + # Revision x2w-startup-shader-21 excludes NUL. Stored order is a, b, root, then optional input layout. vector = (b"\x00VS\xff", b"PS\x00\x80", b"RTS0\x00") - assert gfx2hlsl.pipeline_key(*vector) == 0x4564b4bb15aa0b9b - assert gfx2hlsl.pipeline_key(*vector, b"ia\x00\xfe") == 0x8a4952a88d16d6e3 + assert gfx2hlsl.pipeline_key(*vector) == 0x93a74e652d95daf6 + assert gfx2hlsl.pipeline_key(*vector, b"ia\x00\xfe") == 0x1ff6b039237188d6 blobs = (VERTEX, PIXEL, ROOT) canonical = gfx2hlsl.pipeline_key(*blobs) check_request(blobs, canonical) diff --git a/tools/startup/gfx2hlsl.py b/tools/startup/gfx2hlsl.py index 6c72b41052..cd0115433b 100644 --- a/tools/startup/gfx2hlsl.py +++ b/tools/startup/gfx2hlsl.py @@ -9881,7 +9881,7 @@ def pipeline_key(vs_code, ps_code, root_blob, layout=b'', ps_inputs=None): SPI_PS_INPUT_ENA and SPI_PS_INPUT_ADDR as little-endian u32. Two pipelines with the same stages, root signature and input layout but different masks are then two requests with their own results. Without a description the key is unchanged.""" - key = fnv(root_blob, fnv(ps_code, fnv(vs_code, fnv(b'x2w-startup-shader-20')))) + key = fnv(root_blob, fnv(ps_code, fnv(vs_code, fnv(b'x2w-startup-shader-21')))) key = fnv(layout, key) if layout else key return key if ps_inputs is None else fnv(b'psin1' + struct.pack('