Skip to content

shader: lower v_pack_b32_f16 in the direct C++ DXIL path - #2917

Merged
MasterDomino merged 4 commits into
R0Y00:cpp-migrationfrom
Ybucaille:shader/pack-b32-f16-direct
Nov 26, 2026
Merged

MasterDomino merged 4 commits into
R0Y00:cpp-migrationfrom
Ybucaille:shader/pack-b32-f16-direct

Conversation

@Ybucaille

@Ybucaille Ybucaille commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #2909

Change

  • src/gfx1013_direct_compute.inl: float_alu lowers VOP3 v_pack_b32_f16 (0x311). It is shared by the compute
    and pixel profiles, so the pixel profile failure from the issue goes through the same code.
  • Semantics: for each source, op_sel selects the high or low half, abs clears bit 15 of the half, then neg toggles
    it. D[15:0] is source 0 and D[31:16] is source 1. Only lshr/and/xor/shl/or on i32 are emitted, so signed
    zero and NaN payloads keep their exact bits. Sources are read before the destination is written, so a destination
    overlapping a source is fine. VGPR and SGPR sources are supported.
  • Refused with a named diagnostic: clamp, omod, destination op_sel bit, op_sel[2], abs/neg on source 2, inline
    constants and a literal source (same set as the Python path, plus omod). No approximation.
  • Tests: tests/native_pack_b32_f16_check.py (modes ir and gpu), helper tests/native_pixel_ir_dump.cpp,
    registration in tests/cmake/native_pack_b32_f16.cmake. The model is ref() from
    startup_gfx2hlsl_pack_b32_f16_check.py.

Verified on Linux (no Windows, no GPU)

  • python -B tests/startup_gfx2hlsl_pack_b32_f16_check.py: 2304 cases pass (unchanged).
  • The frontend translation unit compiles with g++ 20 (-fsyntax-only and object code; d3d12.h from the Wine headers
    and a stub wrl/client.h, not part of the change). A small Linux build of native_pixel_ir_dump from
    gfx1013_compute_native.cpp, gfx1013_isa.cpp, gfx1013_compute_ir.cpp and guest_shader_container.cpp ran
    python -B tests/native_pack_b32_f16_check.py ir <dumper>: 261 results match the model (issue words give
    0x77883344 in v4, the control without the pack gives 0x11223344; 64 op_sel/abs/neg forms x 4 value pairs;
    overlap; SGPR sources; nine refusals).
  • Controls: the swapped-half and neg-before-abs variants differ from the emitted bits. A mutated lowering with neg
    applied before abs fails the check (first at op_sel 0, abs 1, neg 1).

Verified on Windows (review of this PR)

RX 9070 XT, driver 32.0.32029.2010, MSVC 19.44.35221, release preset, SDK 10.0.26100 DXC, debug layer off.

Review fix

Workgroups were 64 words apart but each stores 66 words, so two results overlapped the next group. They are now
128 words apart (s_lshl by 9), the offsets are asserted disjoint and inside the 2064-word buffer, and
expected compares every group's SGPR and overlap results without a write-order assumption
(commit 076b950). The unsupported forms and results are also in
docs/experiments/pack-b32-f16-direct.md.

Still open

The GPU readback has no run on any machine; it needs #2915 fixed or an adapter that does not crash.

Lower VOP3 0x311 with integer operations only: pick the half of each
source with op_sel, clear the sign for abs, then toggle it for neg, and
place source 0 low and source 1 high. Signed zeros and NaN payloads keep
their exact bits, and the destination may overlap a source.

Refuse clamp, omod, a destination or third-source op_sel bit, abs/neg on
the third source, inline constants and literals with a named diagnostic.

native_pack_b32_f16_check.py evaluates the emitted IR against the model
of the Python translator (issue repro, overlap, SGPR input, all op_sel,
abs and neg forms, wrong-order controls) and has a compute readback mode
for a GPU run.

Fixes R0Y00#2909

@MasterDomino MasterDomino left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Review of head 0850d32d on cpp-migration 40365770. This includes the Windows run you asked for.

Environment: Windows 11 build 26300.9974, AMD Radeon RX 9070 XT, driver 32.0.32029.2010, MSVC 19.44.35221, release preset, Windows SDK 10.0.26100 DXC, D3D12 debug layer not enabled. The build is clean, native_pixel_ir_dump included.

Lowering: it matches the acceptance list in #2909.

  • op_sel bits 11 and 12 pick each source's half.
  • abs clears bit 15, and neg then toggles it.
  • Source 0 goes low and source 1 high.
  • Only lshr/and/xor/shl/or on i32 are emitted.
  • Clamp, omod, op_sel bits 13 and 14, abs/neg on source 2, inline constants and a literal are refused with named diagnostics.
  • Both sources are read before vector_write, so overlap is safe.

Results here

check result
native_pack_b32_f16_ir passed: 261 results match the model; issue reproduction, control, overlap, SGPR and refusals pass; swapped-half and neg-before-abs variants differ from the emitted bits
issue words through compile_dxil ... gfx1013-container-ps compiles (rc 0), and dxc -dumpbin reads the .cso (rc 0). The control without the pack also compiles
native_pack_b32_f16_hardware not verified: native_store_replay.exe ... pack-readback.bin --direct exits 0xC0000005 with no output. This is the crash in #2915, where 5 of the base branch's native_store_replay --direct tests crash the same way on this adapter. It is not caused by this PR, but it means the readback still has no run on any machine.

Bug in the GPU readback layout (please fix before a GPU run counts). In run_gpu, each workgroup stores len(want) = 66 words: 64 op_sel/abs/neg forms, then the SGPR case and the overlap case. Each store is at byte offset 4 * index, from a per-group base that is 64 words apart (v8 = group * 256 bytes). So group 0's last two stores land on group 1's first two words, at words 72 and 73 of the 2064-word buffer. The final value there depends on which workgroup writes last. expected assigns group 0 and then group 1, so it assumes group 1 wins. On a GPU where group 0 finishes last, the test fails with a correct lowering. Either way, group 0's SGPR and overlap results are never compared. Splitting the cases across two dispatches, or adding a guard band at the edge, would fix it. So would writing at most 64 words per group, or using a wider stride if the root layout allows it.

Minor

  • #2909 asks to document the unsupported modifier forms. They are listed in the code comment and the test docstring. A short docs/experiments/<topic>.md note (see docs/experiments/README.md) would also give the GPU result a place to live once someone has one.
  • native_pack_b32_f16_hardware has no WARP variant, because native_store_replay always takes the default adapter. That is consistent with the other replay tests, but this check stays unexercised until someone runs it on a GPU that does not crash in #2915.

AI-assisted; the commands above were run locally.

Each workgroup stores 66 words (64 modifier forms, the SGPR case and
the overlap case) but groups were 64 words apart, so group 0's last
two results shared words with group 1's first two and the final value
depended on which group wrote last.

Space the groups 128 words (512 bytes) apart: s_lshl by 9 instead of 8.
The expected buffer is built from per-group offsets that are asserted
disjoint and inside the 2064-word buffer, so every group's SGPR and
overlap results are compared with no write-order assumption.
Record the lowering semantics, the refused modifier forms, the Linux IR
verification and the Windows result from the review of R0Y00#2917. The GPU
readback has no run yet because native_store_replay --direct crashes
with 0xC0000005 (R0Y00#2915).
@Ybucaille

Copy link
Copy Markdown
Contributor Author

Thank you for the review and for running it on Windows.

  • Readback layout: fixed in 076b950. Workgroups are now 128 words apart instead of 64, so the 66 results of a group
    no longer reach the next one. expected is built from per-group offsets that are asserted disjoint and inside
    the 2064-word buffer, so both groups' SGPR and overlap results are compared and nothing depends on write order.
  • Note: added docs/experiments/pack-b32-f16-direct.md in 21408d7 with the semantics, the refused forms, the Linux
    IR check and your Windows results.

The readback itself still has no run, since native_store_replay --direct crashes on your adapter (#2915). I
will leave it open until someone can run it on a GPU that does not hit that.

@sydney-0o

Copy link
Copy Markdown
Contributor

Tested PR head 0850d32d7dfa0631591f214238bfc8e9665c88db in a clean worktree on Windows 10 Pro 22H2 (build 19045.6456). The machine's physical adapter is an NVIDIA GeForce GTX 1080 Ti, driver 32.0.15.8278 (a Virtual Display Driver is also installed). The hardware fixture requests the default D3D12 adapter and does not print the selected adapter, so the adapter used by the capability check was not independently verified. The fixture does not explicitly enable the D3D12 debug layer; external debug-layer state was not verified.

Configured with cmake --preset dev -DINTAKE_PYTHON=<Python 3.12 environment>, then built the relevant targets with cmake --build --preset dev --target native_pixel_ir_dump native_store_replay compile_dxil -j 4 (passed; MSVC 19.31.31105, DXC 1.8.2502.11). Ran serially:

ctest --preset dev -R 'native_pack_b32_f16|native_direct_container|native_scalar_lds' -V
  • native_direct_container: passed.
  • native_pack_b32_f16_ir: passed; 261 results matched the model, including the fixture's negative controls.
  • native_scalar_lds_hardware: capability skip (77): requires shader model 6.6 with wave32.
  • native_pack_b32_f16_hardware: capability skip (77): requires shader model 6.6 with wave32.

CTest exited 0: 2 passed, 2 skipped, 0 failed. The hardware checks stopped at the combined capability gate before GPU readback; the diagnostic does not identify which requirement was missing. These results do not establish hardware correctness for the skipped fixtures.

@MasterDomino MasterDomino left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Follow-up on head 21408d7e. My requested change is addressed.

  • Layout (076b950d): the shader moves the stride as well as expected. scalar(0x1e, 7, 5, 137) is s_lshl_b32 s7, s5, 9, so each workgroup's base is group * 512 bytes, and that matches STRIDE = 128 words. Group 0 writes words 8–73 and group 1 writes 136–201, both inside the 2064-word buffer. group_offsets asserts that the groups are disjoint and in range, and expected is built from the same offsets, so the result no longer depends on which group writes last. A small nit, not blocking: the shift 137 (inline 9) and STRIDE are two separate constants. Deriving the shift from STRIDE, or asserting 4 * STRIDE == 1 << 9, would keep them from drifting apart.
  • Note (21408d7e): docs/experiments/pack-b32-f16-direct.md matches the code and the runs. It reports the Windows results I posted, and it states that the GPU readback has produced no result anywhere.
  • The lowering (src/gfx1013_direct_compute.inl) and the IR path are unchanged since 0850d32d, where native_pack_b32_f16_ir passed here, so I did not rebuild for this read.

Still open, as the note says: native_pack_b32_f16_hardware has no GPU result. It crashes in #2915 on the RX 9070 XT. On @sydney-0o's GTX 1080 Ti it skipped (77) at the SM 6.6 with wave32 gate. That needs an adapter with SM 6.6, wave32 and no #2915 crash, for example R0Y00's RTX 4060. That is a measurement still to make, not a defect in the PR.

AI-assisted.

@Ybucaille
Ybucaille marked this pull request as ready for review November 26, 2026 21:29
@JB2WIN

JB2WIN commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

GPU readback on an adapter that does not hit #2915:

  • native_pack_b32_f16_hardware: passed 3/3 — pack_b32_f16 GPU: 528 packed words matched the model bit for bit
  • native_pack_b32_f16_ir: passed — 261 results match the model

Setup: cpp-migration ee76dfa + #2912 (432db95) + this PR (74537dc), dev preset, NVIDIA GeForce RTX 3080 driver 32.0.16.2124, Windows 11 build 26200, MSVC toolset 14.51.36231, Windows SDK 10.0.26100 DXC, D3D12 debug layer not enabled.

ctest --preset dev --no-tests=error -R "^native_pack_b32_f16_(hardware|ir)$" -V

AI-assisted.

@MasterDomino MasterDomino left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Approving at 74537dc6 and merging into cpp-migration.

  • Since my approval at 21408d7e, the only change is 74537dc6, which ties the readback shift to STRIDE (SHIFT = 9, assert 4 * STRIDE == 1 << SHIFT, 128 + SHIFT in the s_lshl_b32). That settles my nit.
  • The lowering in src/ is unchanged since 0850d32d, where native_pack_b32_f16_ir passed here on Windows and the issue's words compiled with compile_dxil and were read by dxc -dumpbin.
  • The missing GPU run is now in: @JB2WIN's RTX 3080 (driver 32.0.16.2124, Windows 11 26200, dev) on this head gives native_pack_b32_f16_hardware 3/3, with 528 packed words matched the model bit for bit, and native_pack_b32_f16_ir passes too. @sydney-0o's GTX 1080 Ti skipped (77) at the SM 6.6 with wave32 gate, and the RX 9070 XT here still hits #2915, so this NVIDIA run is the readback evidence.
  • It merges cleanly on the current cpp-migration (ee76dfa8). .github/workflows/windows.yml does not run for this base, so the runs above stand in for CI.

AI-assisted.

@MasterDomino
MasterDomino merged commit 16099d7 into R0Y00:cpp-migration Nov 26, 2026
MasterDomino added a commit that referenced this pull request Nov 26, 2026
#2931)

This reverts commit ee76dfa. Those are the 17 XMem commits of #2911, which
is still under review against main, plus two fixture changes. Keeping them
off cpp-migration keeps the branch to the native compiler work. When #2911
lands on main, merging main into cpp-migration brings in the reviewed
version.

Every file #2918 touched is restored to its state at cd752a8. The later
#2917 touches none of them.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
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.

4 participants