Skip to content

wave64_halves: v_sat_pk_u8_i16 keeps its high half (SHADER-SATPK-HALVES) - #508

Open
milk4sale wants to merge 6 commits into
ProjectLeonida:mainfrom
milk4sale:shader-satpk-halves
Open

milk4sale wants to merge 6 commits into
ProjectLeonida:mainfrom
milk4sale:shader-satpk-halves

Conversation

@milk4sale

@milk4sale milk4sale commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: SHADER-SATPK-HALVES

Depends on: #338

What and why

#265 added v_sat_pk to _FULL32_V16_PREFIXES in tools/wave64_halves.py, so _preserves_dst treats v_sat_pk_u8_i16 as a full 32-bit write. partial_write also read the op's first type, u8, as an 8-bit result. #338 translates the op and writes the two saturated bytes with write_lo16, which keeps the high half in the _e32, _e64 and DPP forms. With both merged, a high half that depends on the lane lost its taint, and a wave64 VS/PS/GS passed as tier A, or tier B through readlane-uniform, although its export changes when the wave is cut into two 32-lane halves. NolanCotter reported this in #14.

This change removes v_sat_pk from the tuple, so the op falls to the _i16 rule, and makes partial_write say partial for it. Per encoding:

  • _e32 (VOP1 0x62), _e64 (VOP3 0x1e2) and DPP keep the high half.
  • SDWA keeps the rest only with a BYTE/WORD dst_sel and UNUSED_PRESERVE; dst_sel:DWORD, UNUSED_PAD and UNUSED_SEXT write the whole register. This was already right.
  • llvm-mc 23.1.4 rejects op_sel on the _e64 form for gfx1030.

References. Neither compiler says the high half is kept on GFX10, and the cited references don't either:

  • Mesa ACO's instr_is_16bit is false for the op before GFX11. It is not in the list, and can_use_opsel falls through to get_gfx11_true16_mask, which lists it next to v_cvt_f16_f32 for GFX11+ only (aco_ir.cpp, Mesa 99abc485).
  • LLVM's VOP1Inst_t16 is the plain VOP1 instruction on GFX10, with the true16 forms GFX11+ only. GCNSubtarget::zeroesHigh16BitsOfDest does not list the op (LLVM e2b0a1e1).

The rule here follows what the translator emits. If RDNA2 turns out to zero the high half, calling the op partial is still the safe side: it costs tier A at most, never exactness. The RDNA2 behaviour is not measured. The contract row in docs/design.md says so.

#338's own v_sat_pk_u8_i16 row cited ACO instr_is_16bit for the kept high half, which the halves row contradicts (review on this PR). This branch rewords that row: the keep is the translator's choice (write_lo16), not measured on RDNA2, with a pointer to the halves row's references. If #338 rewords the row first, take #338's wording when merging. #338's translator comment and the test_alu_sat_pk_u8_i16.py docstring cite ACO the same way; they are left to #338, where this was raised.

The halves analysis runs only for wave64 VS/PS/GS, never for compute or under --wave64-emulate (halves_analysis in rdna2spirv.py). So the GPU fixtures are ps_w64_* ones run by w64_graphics.py, not cs_*:

  • ps_w64_a_sat_pk_hi, tier A: the four encodings that keep the high half, a different high half per pixel, and both halves of SRC0 saturating below 0 and above 255 across the target. Every word is a function of the pixel, so a 32-only GPU checks all of them.
  • ps_w64_c_sat_pk_hi, tier C: the high half is the lane id. It is pinned to tier C, lane-id, in tests/test_wave64_halves_fixtures.py. On a GPU it needs both subgroup sizes, or W64G_REF.

Tested

  • tests/semantic/test_wave64_halves_sat_pk.py with the hand-written tests/semantic/fixtures/w64_sat_pk_hi.asm: 5 OK. It runs on the strict wave interpreter as 64 lanes and as two waves of 32, covers every encoding and the readlane-uniform case, and checks operands() and partial_write per encoding.
  • python -I scripts/run_semantic_tests.py tests/semantic: 467 files, 6114 tests, 0 failed (70 s, 24 jobs).
  • python -m pytest tests/test_wave64_halves_fixtures.py tests/test_w64_graphics_args.py tests/test_w64_graphics_probe.py tests/test_w64_graphics_reference.py: 22 passed. Without the fix, test_fixtures_are_classified_as_their_headers_say fails: ps_w64_c_sat_pk_hi is tier A instead of C.
  • tests/test_design_contract_rows.py, tests/test_docs_design_rows.py, tests/test_text_write_line_endings.py and tests/test_semantic_translator_use.py pass. So do python -m compileall -q make_game.py tools tests and python -I scripts/check_no_game_data.py on every added or changed file.
  • After the row rewording (232a2ce6d, docs only): the four tests above, tests/test_wave64_halves_fixtures.py and the two sat_pk semantic test files pass again, and check_no_game_data.py docs/design.md exits 0.
  • w64_graphics.py on the RTX 4070 Ti: ps_w64_a_sat_pk_hi A ok, ps_w64_c_sat_pk_hi C SKIP (32 is the only fragment subgroup size). The verdicts are the same without the fix: a 32-only GPU cannot see the classification, so the tests above catch it. NVIDIA runs the translated SPIR-V, so this confirms the translator's semantics, not the AMD instruction. The expected low half comes from the ISA pseudocode's SAT8 packing; the expected high half is the translator's rule.
  • python -I scripts/run_gpu_tests.py on the RTX 4070 Ti, rdna2spirv: translate v_sat_pk_u8_i16 (SHADER-SATPK-U8) #338's head against this branch, each with its own runtime build. Both: 257 shaders, 257 translated, 257 pass spirv-val.

GPUs tested: NVIDIA GeForce RTX 4070 Ti, driver 620.70.06, Vulkan 1.4.357, CachyOS Linux (kernel 7.3.3), pinned with VK_LOADER_DRIVERS_SELECT=*nvidia* and LEONIDA_VK_DEVICE=4070. llvm-mc 23.1.4, SPIRV-Tools v2026.4.

AI-assisted: yes (Claude Code)

Checklist

🤖 Generated with Claude Code

devin-ai-integration Bot and others added 3 commits November 23, 2026 20:21
The VOP1/VOP3 op saturates each signed i16 half of SRC0 to unsigned
8-bit and packs the pair into the low 16 bits of VDST, preserving the
high half (GFX9+ partial write; LLVM VOP1Inst_t16, Mesa ACO
instr_is_16bit). Neg/abs/clamp/omod are refused; _sdwa restricts to the
documented source/destination selects; _e32 rejects all modifiers.

Adds a hand-written fixture and a semantic test driving the SPIR-V
through spirv_mini_interp against a plain-Python reference (lane edge
values, SGPR/literal sources, sdwa selects, refused modifiers), the
docs/design.md contract row, and drops the op from the
fssm_crosscheck unhandled list. The two tests that used the mnemonic
as the still-unsupported canary now use v_mullit_f32.

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
…alves partial-write code of ProjectLeonida#265

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
ProjectLeonida#265 put v_sat_pk in _FULL32_V16_PREFIXES, so _preserves_dst took
v_sat_pk_u8_i16 for a whole 32-bit write, and partial_write read its first
type, u8, as an 8-bit result. The translator from ProjectLeonida#338 writes the two
saturated bytes with write_lo16, which keeps the high half in the _e32, _e64
and DPP forms, so a high half that depends on the lane lost its taint: the
shader passed as tier A (or tier B through readlane-uniform) although its
export changes when the wave is cut in two. SDWA was already right
(dst_sel/dst_unused decide).

Neither Mesa ACO nor LLVM says the high half is kept on GFX10: ACO's
instr_is_16bit is false for the op before GFX11 (only get_gfx11_true16_mask
has it), and LLVM's zeroesHigh16BitsOfDest does not list it. The rule follows
what the translator emits, and calling the op partial is the safe side if
RDNA2 zeroes the high half. The RDNA2 behaviour is not measured.

- tests/semantic/test_wave64_halves_sat_pk.py with the hand-written
  fixtures/w64_sat_pk_hi.asm: high half = lane id, 64 lanes against two
  waves of 32 on the wave interpreter, every encoding, the readlane-uniform
  case, operands()/partial_write per encoding.
- runtime/test/shaders/ps_w64_a_sat_pk_hi (tier A, judged on 32-only GPUs)
  and ps_w64_c_sat_pk_hi (tier C, pinned in test_wave64_halves_fixtures).
- docs/design.md contract row.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@pwu2775-dev pwu2775-dev 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.

Code read of 4234449 against main. One docs point, not blocking.

The two docs/design.md rows this PR adds disagree. The first row (v_sat_pk_u8_i16) gives Mesa ACO's instr_is_16bit as the reason the destination's high half is kept; the second row (the wave64 halves path) says that function is false for this opcode before GFX11 and that what RDNA2 does with the high half is not measured. The translator behaviour and the wave64 classification are consistent with each other and the second row is candid, but the first row's citation does not establish the hardware behaviour. Suggest rewording the first row to say the preservation is the translator's choice (unverified on RDNA2) and point to the second row.

I did not find other problems in the changed translator and tool code.

AI-assisted: review by Codex (gpt-6.1-terra); the docs point was checked against both rows before posting.

@pwu2775-dev

Copy link
Copy Markdown
Contributor

Tested 4234449 on NVIDIA GeForce RTX 4070, driver 617.42, Windows 11 10.0.26300, MSVC (VS 2022), Vulkan SDK 1.4.363.0, Ninja RelWithDebInfo: pass for what I could run, with one gap.

One docs point (not blocking): the two new docs/design.md rows disagree about the Mesa ACO citation; details in my review: #508 (review)

AI-assisted: review by Codex (gpt-6.1-terra).

demoz53 added a commit to demoz53/leonida that referenced this pull request Nov 24, 2026
…ice (SHADER-SATPK-HALVES)

The op's row cited Mesa ACO instr_is_16bit for the kept high half, but the
next row (the wave64 halves path) records that instr_is_16bit is false for
the op before GFX11 and that RDNA2's behaviour is not measured. Say that
write_lo16 keeps it by the translator's choice and point to that row's
references (review on ProjectLeonida#508).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Sangonapou

Copy link
Copy Markdown
Contributor

Tested 232a2ce on NVIDIA GeForce RTX 3080 Ti Laptop (Ampere, subgroup 32..32), NVIDIA 621.24, Vulkan 1.4.357, Windows 11 Pro (build 26300): pass for what a 32-only device can run

Runtime built at this head with scripts\build_runtime.ps1 (MSVC 19.51); shaders from runtime/test/shaders/build.py at this head (llvm-mc 23.1.6, SPIRV-Tools 2026.4); then python -I runtime/test/shaders/w64_graphics.py <spirv dir> runtime\build, exit 0. No game files.

  • The new tier A fixture passes on two 32-lane halves: ps_w64_a_sat_pk_hi A ok.
  • All tier A (10/10) and tier B (8/8) fixtures ok.
  • Tier C (13, including the new ps_w64_c_sat_pk_hi) all SKIP (tier C needs a device that can require both 64 and 32, or W64G_REF), so the tier C half of this change needs a 64-and-32 device or a W64G_REF capture.

@milk4sale

Copy link
Copy Markdown
Contributor Author

@pwu2775-dev thanks, you were right that the first row's citation did not establish it. Reworded in 232a2ce: the v_sat_pk_u8_i16 row no longer cites ACO instr_is_16bit for the kept high half. It now says the translator writes the result with write_lo16, that keeping the high half is the translator's choice and is not measured on RDNA2, and it points to the next row's references. The two rows agree now.

@Sangonapou thanks for the run at 232a2ce. The tier C half is still unrun: it needs a device that can require both 64 and 32, or a W64G_REF capture, as you say.

AI-assisted.

@milk4sale

Copy link
Copy Markdown
Contributor Author

The red windows, linux-gpu-tools and macos jobs come from main as it was when this run was created, not from this change. Their checkout step says Merge 232a2ce6d into a28927849, and a28927849 is older than two fixes:

This pull request touches none of the files #877 and #892 change, and it merges into current main (58be9cdf8) without conflict. Re-running the failed jobs would test the same old merge, so I'm leaving 232a2ce6d, the head tested above, as it is rather than pushing just to restart CI. I haven't found a finished run of these jobs on a main that has both fixes yet.

AI-assisted.

Brings the branch up to date with main 68d1331. No conflicts; the merged tree was tested before the push (every standalone
test file under tests/ passes except tests/test_ci_registration.py, which fails on main alone since ProjectLeonida#930).

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@potatoe0100

Copy link
Copy Markdown
Contributor

Tested head 38623a957 merged onto main 57ba40bd6 (clean merge) on NVIDIA GeForce RTX 4060 (Ada, subgroup 32 only), driver 621.14, Windows 11 Pro 26300, VS 2022, Vulkan SDK 1.4.363.0: pass.

  • build.py on the 34 wave64 graphics fixtures: 34 translated, 34 pass spirv-val.
  • w64_graphics.py: 19 ok, 13 tier C SKIP (expected on a 32-only device), 0 failed. ps_w64_a_sat_pk_hi ok; ps_w64_c_sat_pk_hi SKIP.
  • pytest: test_alu_sat_pk_u8_i16.py 5, test_wave64_halves_sat_pk.py 5, test_f16_op_sel.py 40, test_wave64_halves_fixtures.py 10 passed. test_translate.py 707 passed, 2 failed (cs_dpp_cmp.asm, the same 2 fail on plain main).

No game files. AI-assisted (Claude Code), run for a non-developer tester.

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