Skip to content

shader: read the wave size of slot 67 compute requests from .sa - #3006

Open
Leyukaka wants to merge 4 commits into
R0Y00:mainfrom
Leyukaka:wave-bit
Open

Leyukaka wants to merge 4 commits into
R0Y00:mainfrom
Leyukaka:wave-bit

Conversation

@Leyukaka

@Leyukaka Leyukaka commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #2999 (it needs closing by hand, since the base is not the default branch). Part of #2935. Target branch main.

Change (src/shader_compute_request.*, src/gfx1013_compute_native.cpp):

  • read_compute_request sets ComputeRequest::wave32 from bit 20 of .sa dword 4: set means wave32, clear means wave64. A .sa that is not 28 bytes is still refused, as before.
  • The direct DXIL route of translate_compute compiles a wave64 request through the wave64 path that translate_compute_container already uses for a stage without wave32.
  • A wave64 request whose group has at most 32 threads stays on the wave32 model, as before this change. Its one guest wave only has lanes 0..31, so EXEC's high word is zero and v_mbcnt_hi adds nothing for its lanes. One known difference remains: a wave64 compare or carry-out also writes zero to the high word of an SGPR pair destination, and the wave32 model leaves that word as it was.
  • The legacy HLSL profile and the container route are unchanged. The in-process route of runtime: translate slot 67 compute requests in the stand-in #2958 reads requests through the same function, so it gets the bit too.
  • Unlike the plan in shader: read the wave size of slot 67 compute requests (bit 20 of .sa dword 4) instead of assuming wave32 #2999, the request key revision stays at 28cs, so existing Python caches keep their keys. The key hashes the request parts, which do not change. --requests removes and rebuilds every output, the frontend cache is keyed on a hash of the compiler sources, and the in-process route translates again in each process.
  • compile_dxil --watch and the stand-in's reader do reuse a <key>.txt and <key>.cs.cso published by an earlier build. After updating, rerun --requests into that directory or delete those files.
  • The bit's meaning is an inference from captured data, explained in shader: read the wave size of slot 67 compute requests (bit 20 of .sa dword 4) instead of assuming wave32 #2999 and in docs/experiments/native-slot67-wave-size.md.
  • Source-built fixtures that relied on the wave32 default now set the bit, in 15 test files.

Tests. The setup was Windows 11 with an NVIDIA GeForce RTX 5080 and DXC from the Windows SDK 10.0.26100, on cpp-migration e0d83759 plus this commit.

  • native_slot67_wave_size_hardware passed. The same v_mbcnt_lo/v_mbcnt_hi program is built as three requests, and the stored lane indices match a CPU reference with every other buffer word untouched:

    Bit 20 Group size Expected lane index
    clear 64 threads tid % 64
    set 64 threads tid % 32
    clear 32 threads tid % 32
  • ctest -R "native_|startup_compute|startup_shader_cache_rekey": 275 of 276 passed. 4 WARP cases skip (77) as before. native_scalar_lds_hardware fails with "native compute PSO creation failed" on this adapter; it fails the same way on e0d83759 without this change. This PR only sets the bit in that file's fixtures.

Mutation checks, run by hand; each fails the new check:

  • ignoring the bit;
  • inverting the bit;
  • dropping the 32-thread rule, which refuses the 32-thread wave64 request.

Measured on a local capture (counts and instruction shape only) with compile_dxil --jobs 8 gfx1013-direct-cs@36 --requests, before and after:

Requests Compiled before Compiled after Newly compiled No longer compiled
Capture, 2,689 389 393 46 42
One start-up run, 2,718 402 406 46 42
  • The 46 newly compiled requests are wave64 programs with 64-thread groups. In that run they account for 14,404 dispatches.
  • The 42 that no longer compile are wave64 programs that the wave32 model used to accept. They account for 3,161 dispatches. They now stop at "multi-wave64 exchange requires converged group control flow", a limit of the existing wave64 path.
  • 312 compiled requests now use the wave64 path.

AI-assisted.

Bit 20 of .sa dword 4 now sets ComputeRequest::wave32 (set: wave32,
clear: wave64). The direct DXIL route of translate_compute compiles a
wave64 request with more than 32 threads per group through the existing
wave64 path, as translate_compute_container does for a stage without
wave32. A wave64 group of at most 32 threads stays on the wave32 model,
as before: its one guest wave has lanes 0..31 only. The model still
does not write the zero high word that a wave64 compare stores in an
SGPR pair destination. The legacy HLSL profile and the container route
are unchanged.

The bit's meaning is an inference from captured requests (see
docs/experiments/native-slot67-wave-size.md). Source-built fixtures that
relied on the wave32 default now set the bit. A new check compiles one
v_mbcnt_lo/v_mbcnt_hi program as wave64 with 64- and 32-thread groups
and as wave32, and compares the lane indices with a CPU reference on
hardware.

AI-assisted.

@R0Y00 R0Y00 left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The cold-cache lane-index fixture verifies the new wave-size decoding. Issue #2999 also calls for rebuilding cached translations on update. The PR documents that --watch and the stand-in can still reuse the old <key>.txt / <key>.cs.cso, so a warm native cache can keep running the old wave32 result for a wave64 request. Please add an upgrade control that prepopulates the old output, then show that the updated consumer refreshes or rejects that output. A native output compatibility check could preserve legacy Python request keys while doing this.

…ler revision

Native compute manifests written by compile_dxil now end with
'native <revision>' (src/native_compute_output.h, revision 1). The
stand-in refuses a native manifest with another revision, or an
unmarked manifest without a <key>.cs.hlsl sidecar (a native output
from before this change), with a 'rebuild it' reason, and retries it
after a second like an absent manifest. compile_dxil --watch treats
both as pending and rebuilds them. Unmarked outputs with a .cs.hlsl
sidecar are the Python translator's and are used as before, so request
keys keep revision 28cs.

AI-assisted.
…g Python bytecode in --watch

A native compute publish now removes <key>.cs.hlsl, so a rebuilt pair is
never read as a Python output. In --watch, an unmarked manifest with a
.cs.hlsl sidecar goes on to the usual bytecode check instead of being
skipped, so a Python output with incomplete bytecode is still rebuilt.
The doc names the one case left undetected (a directory an earlier
compile_dxil republished over Python outputs) and that the reload of a
refused manifest is not covered by a test.

AI-assisted.
@Leyukaka

Copy link
Copy Markdown
Contributor Author

Thanks, done in the new commits, without changing request keys. The branch also merges cpp-migration 999e17cd, since #3003 changed the same --watch check.

Native output revision. compile_dxil now ends each native compute manifest with native <revision>. The revision is a constant shared with the stand-in, native_compute_output_revision in src/native_compute_output.h, starting at 1. It is meant to be bumped whenever a native translation of an unchanged request can change meaning.

Stand-in. Before reading bindings or bytecode:

  • a manifest with another native revision is refused with "native translation from another compiler revision; rebuild it";
  • a manifest with no native line and no <key>.cs.hlsl is a native output from before this change. It is refused with "native translation without a revision; rebuild it";
  • an unmarked manifest with a <key>.cs.hlsl is the Python translator's output and is used as before;
  • both refusals use the one-second retry of an absent manifest, so a running process can pick up a rebuilt output. That reload follows from the code path; no test covers it, since each check starts a new process.

compile_dxil.

  • --watch treats a stale or unmarked native output as pending and rebuilds it.
  • An unmarked output that has a .cs.hlsl goes on to the existing bytecode check, so a Python output with incomplete bytecode is still rebuilt.
  • --requests already rebuilds every output.
  • A native compile in either mode now first removes a <key>.cs.hlsl left by the Python translator, so the new pair cannot pass as a Python output. An unreadable manifest goes on to the bytecode check instead of counting as complete.

One case stays undetected: a directory where an earlier compile_dxil already republished a Python-translated key. That key's old .cs.hlsl sits next to an unmarked native manifest, and nothing tells the two apart. Such a directory needs one --requests rerun; the doc says so.

Upgrade controls:

  • native_compile_dxil_watch seeds an unmarked native output and a native 0 output, each with a dummy .cs.cso. It checks that --watch replaces the manifest and the bytecode of both. An unmarked Python-style output with a .cs.hlsl keeps all three files byte for byte, with their timestamps. The same output with truncated bytecode is rebuilt, and its .cs.hlsl is removed.
  • startup_compute_dispatch_warp and _hardware check both refusal strings with the output untouched. They then check the output words for a native 1 manifest and for a Python-style one.
  • native_startup_request_cache_check checks the exact native manifest and that no .cs.hlsl is written.

On the RTX 5080 and WARP, after the merge, ctest -R "native_|startup_compute|startup_shader_cache_rekey|compile_dxil" passed 280 of 282. The two failures fail the same way on 999e17cd without this PR: native_scalar_lds_hardware with "native compute PSO creation failed", and native_cvt_pk_u8_hardware with words 8, 24, 40 and 56 reading 0xa5a5a501 where 0xa5a5a500 is expected. Four manual mutants each fail a new control:

  • letting the stand-in use an unmarked native output fails both dispatch checks;
  • letting --watch keep an unmarked native output fails the watch check;
  • skipping the bytecode check for a Python output fails the watch check;
  • keeping the .cs.hlsl on a native publish fails the watch check.

One interaction to handle on merge: the portable fixture of #2975 writes a hand-made manifest without a native line, and its HLSL as <key>.hlsl. Under this check, that fixture needs its HLSL saved as <key>.cs.hlsl. I will make that change in whichever of the two PRs lands second.

AI-assisted.

@KelboLive KelboLive left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR #3006 by @Leyukaka — shader: read the wave size of slot 67 compute requests from .sa

What works

  • Clean extraction of bit 20 from sa[18] with correct little-endian dword mapping (byte 18 is d4[2], bit 4 of that byte = global bit 20)
  • Default wave32=true preserves backwards compatibility: old .sa files without the bit still get wave32 behavior
  • Wave64 with <=32 threads stays on wave32 model: good fallback since lane 0..31 is valid either way
  • The known difference (wave64 compare/carry-out writes zero to high word of SGPR pair) is documented transparently
  • Key revision stays 28cs: Python caches preserved, no key hash changes needed
  • native_compute_output.h provides a clean versioning mechanism for native translations
  • Manifest retry mechanism (incompatible/unmarked -> retry after 1 second) supports live rebuilds
  • native_compute_output_revision bump to 1 is correct for the new version scheme
  • Test covers all three cases: bit clear/64 threads, bit set/64 threads, bit clear/32 threads
  • Mutation checks (ignore bit, invert bit, drop 32-thread rule) all correctly fail
  • 275/276 tests pass; 4 WARP skips, 1 adapter limitation unrelated to this change
  • compile_dxil --watch test properly handles old manifests, unmarked manifests, and new manifests
  • 15 test files updated to set the wave32 bit in their fixtures

Issues

  • sa[18] index is fragile: if .sa file structure changes (e.g., dword 4 offset shifts), this silently reads the wrong bit. Consider adding a comment with the full bit position: sa dword 4 bit 20 = sa[18] bit 4 in little-endian.
  • The native_compute_output parser uses a simple line-based search which could match partial lines (e.g., "nativex" would match the substring check). The line != "native" guard helps but substr(0,7) check on "native " should also verify the char at position 7 is a digit or the line ends after position 7.
  • load_compute_translation sets tr.retry_manifest_at in the incompatible case but the caller needs to know the retry is for manifest version, not just absence. The comment in the struct was updated to "absent and stale manifests can become ready later" which is good.
  • The wave64 threads > 32 check in gfx1013_direct_compute.inl is duplicated logic: the same check appears in both translation paths (direct DXIL and container). Moving to a single decision point would be cleaner.

Recommendations

  • Add a static_assert on sizeof(sa) == 28 or sa.size() == 28 in read_compute_request to catch malformed files early.
  • In native_compute_output, add a check that line[7] is a digit: if (line.size() == 7 || !isdigit(line[7])) return incompatible.
  • Consider adding a debug assertion in translate_program: assert(wave64 || threads <= 32) to catch any edge cases where threads > 32 but wave64 is false on a direct_dxil path.
  • The 15 fixture files updated should include a note in each file linking back to the capture analysis (#2999).

Overall

Well-executed inference-driven addition. The bit extraction is correct, the wave32 model fallback for small groups is a good safety net, and the manifest versioning provides a clean path forward for future native translation changes. The key question for merge readiness is whether the capture analysis in #2999 has sufficient evidence to confirm bit 20 as the wave size marker.

@R0Y00 R0Y00 left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Independent Release /O2 on exact head 5c49bbe38b803d13bdaccfd4e2584cc91aaffc34, Windows 11 build 26200.9974 and SDK 10.0.26100. I built startup_stream_consumer d3d12_xs_shim compile_dxil native_store_replay and ran native_compile_dxil_watch, native_startup_request_cache_controls, startup_compute_dispatch_(warp|hardware) and native_slot67_wave_size_hardware: 5 passed, no skips. WARP 10.0.26100.9818 and RTX 4060 Laptop 32.0.16.1656 matched all 320 dispatch words for the current and Python-style manifests, preserving sentinels in the stale cases. RTX also matched 128/128/64 lane indices for the three slot-67 cases. The marker keeps the existing request key, but the .cs.hlsl heuristic still cannot identify an old native output over a prior Python publication; the documented one-time --requests rebuild still applies to mixed directories. Please keep the reported #2975 sidecar rename with whichever PR lands second.

R0Y00 commented Nov 27, 2026

Copy link
Copy Markdown
Owner

Retargeted to main. A trial merge at 1ec2c938 conflicts in gfx1013_compute_native.cpp, shader_compute_request.cpp, d3d12_compute_dispatch.inl and startup_compute_dispatch_check.py. Please preserve the DPP changes from #3010, revision-29 request keys with revision-28 archive support, and ManifestRetry timing for both absent and stale manifests. Carry the #2975 sidecar naming resolution as discussed. #3014 still follows this PR because both touch the request reader and compile_dxil; we'll validate the wave-size and cache-rebuild path on the rebased head before requesting the rekey rebase.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

shader: read the wave size of slot 67 compute requests (bit 20 of .sa dword 4) instead of assuming wave32

3 participants