Skip to content

rdna2spirv: translate v_sat_pk_u8_i16 (SHADER-SATPK-U8) - #338

Open
NolanCotter wants to merge 1 commit into
ProjectLeonida:mainfrom
NolanCotter:shader-satpk-u8
Open

NolanCotter wants to merge 1 commit into
ProjectLeonida:mainfrom
NolanCotter:shader-satpk-u8

Conversation

@NolanCotter

Copy link
Copy Markdown
Contributor

Claimed in #14 as SHADER-SATPK-U8.

Summary

  • tools/rdna2spirv.py: new rule for v_sat_pk_u8_i16 (VOP1 0x62 / VOP3 0x1E2). Each signed i16 half of SRC0 is sign-extended, OpSClamped to [0, 255] and packed into the low 16 bits of VDST; the high half is preserved (GFX9+ partial 16-bit write — LLVM models the def as VOP1Inst_t16, Mesa ACO instr_is_16bit, same class as v_cvt_f16_f32). Source neg/abs, clamp, omod and op_sel raise Unsupported; _sdwa whitelists src0_sel/dst_sel/dst_unused; _dpp goes through non_dpp_mods; _e32 refuses all modifiers.
  • tests/semantic/fixtures/sat_pk_u8_i16.asm + tests/semantic/test_alu_sat_pk_u8_i16.py: hand-written listing, runs the emitted SPIR-V through spirv_mini_interp against a plain-Python reference — lane edge values (-0x8000/-1/0/255/256/0x7FFF), SGPR, literal and inline-constant sources, _sdwa source/destination selects, and refused modifiers.
  • docs/design.md: contract-table row naming the confirmation (AMD machine-readable ISA semantics; LLVM/ACO partial-write reference; CPU-checked test; not GPU-verified).
  • docs/fssm_crosscheck.md: removed from the unhandled-xbsc list.
  • tests/test_translate.py, tests/test_f16_op_sel.py: the still-unsupported canary is repointed from v_sat_pk_u8_i16 to v_mullit_f32 (still unhandled on this base).

Heads-up for reviewers: PR #265's _FULL32_V16_PREFIXES lists v_sat_pk as a full-32-bit write for its wave64-halves liveness fix; this op writes only the low 16 bits and preserves the high half, so whichever of the two lands second should drop that prefix (noted as a finding: in #14).

Test plan

  • python -m unittest discover -s tests/semantic -p test_alu_sat_pk_u8_i16.py — 5 tests pass
  • python -m pytest tests/test_f16_op_sel.py — passes
  • python -I scripts/run_semantic_tests.py tests/semantic — 444 files / 5894 tests; the only 4 failures (test_float_nan_fract, test_image_atomics_guard, test_image_store_d16, test_int32_to_f64) reproduce identically on clean main (the last is a spirv-opt SIGSEGV in the test environment)
  • python -I scripts/check_no_game_data.py on both new files — clean

AI-assisted (Devin). No game files, no GPU, nothing online; semantics confirmed against AMD's ISA spec and the LLVM/ACO tables, not hardware. GPUs tested: none.

Generated with Devin

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>
@Sangonapou

Copy link
Copy Markdown
Contributor

Tested 6446e3c on NVIDIA GeForce RTX 3080 Ti Laptop (Ampere, subgroup 32..32), NVIDIA 621.24, Vulkan 1.4.357, Windows 11 Pro (build 26300): pass, regression check

Runtime built at this head with scripts\build_runtime.ps1 (MSVC 19.51, VS Build Tools 2026); llvm-mc 23.1.6 (conda-forge), SPIRV-Tools 2026.4, Vulkan SDK 1.4.363.0. No game files. This PR adds no GPU fixture, so this checks the translator change on the GPU suite.

@milk4sale

Copy link
Copy Markdown
Contributor

Follow-up for the wave64_halves side of this op is in #508 (SHADER-SATPK-HALVES, Depends on: #338). It takes v_sat_pk out of _FULL32_V16_PREFIXES so the halves analysis keeps the high half this PR's write_lo16 keeps. #508 was stacked on this head plus main, because #265 is not in this branch's base.

One note on the references. The row and the test docstring cite Mesa ACO instr_is_16bit for the GFX10 high-half keep, but instr_is_16bit returns false for v_sat_pk_u8_i16 before GFX11. The op is only in get_gfx11_true16_mask, which can_use_opsel reads for GFX11+ (aco_ir.cpp, Mesa 99abc485). LLVM's zeroesHigh16BitsOfDest does not list it either. So on RDNA2 the keep rests on the analogy with v_cvt_f16_f32 until someone measures it on RDNA2 hardware. It might be worth saying that in the row rather than citing ACO for it.

demoz53 pushed a commit to demoz53/leonida that referenced this pull request Nov 24, 2026
…alves partial-write code of ProjectLeonida#265

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
demoz53 pushed a commit to demoz53/leonida that referenced this pull request Nov 24, 2026
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>
@tiguan26

Copy link
Copy Markdown
Contributor

Checked 6446e3c merged into current main (3aa9754): it still merges cleanly. On Linux, Python 3.13, no GPU: tests/semantic/test_alu_sat_pk_u8_i16.py 5/5 pass, and tests/test_f16_op_sel.py + tests/test_translate.py pass under pytest (40 passed, the rest skipped without llvm-mc). I recomputed the fixture's four words by hand from the ISA definition (each i16 half saturated to [0, 255], packed into D[15:0], D[31:16] kept): cafeff00 dead00ff 00008000 abcdffff, matching.

+1 to @milk4sale's note on the ACO citation: on current Mesa main, v_sat_pk_u8_i16 is in neither instr_is_16bit nor can_use_opsel, only in get_gfx11_true16_mask.

One small test gap: the fixture's _e64 case writes v5, whose high half is already 0x0000, so it cannot tell a kept high half from a zeroed one for the VOP3 encoding. Seeding v5 with e.g. 0x12340000 (expected 0x12348000) would check the VOP3 form the same way as the VOP1 ones, and would be the case to run when someone measures the high-half rule on RDNA2.

AI-assisted (Claude).

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