From 6446e3ce76483c5c3ba2b539258ab5d80482369e Mon Sep 17 00:00:00 2001 From: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> Date: Mon, 23 Nov 2026 20:20:42 -0700 Subject: [PATCH 1/3] rdna2spirv: translate v_sat_pk_u8_i16 (SHADER-SATPK-U8) 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> --- docs/design.md | 1 + docs/fssm_crosscheck.md | 2 +- tests/semantic/fixtures/sat_pk_u8_i16.asm | 16 ++++ tests/semantic/test_alu_sat_pk_u8_i16.py | 94 +++++++++++++++++++++++ tests/test_f16_op_sel.py | 2 +- tests/test_translate.py | 2 +- tools/rdna2spirv.py | 29 +++++++ 7 files changed, 143 insertions(+), 3 deletions(-) create mode 100644 tests/semantic/fixtures/sat_pk_u8_i16.asm create mode 100644 tests/semantic/test_alu_sat_pk_u8_i16.py diff --git a/docs/design.md b/docs/design.md index 4578aec2be..cd721dba46 100644 --- a/docs/design.md +++ b/docs/design.md @@ -432,6 +432,7 @@ How the generated SPIR-V expects the runtime to work: | `v_mqsad_u32_u8` | Four masked byte-SADs with full 32-bit accumulators: D[k] = S2[k] + `v_msad_u8` of the 32-bit window S0[8k+31:8k] of the 64-bit S0 against S1, wrapping at 32 bits (S2 and D are four VGPRs). Every source is read before the first write; `clamp` raises `Unsupported`. Source: RDNA ISA pseudocode. CPU-checked by `tests/semantic/test_alu_mqsad_u32.py`. **Hardware-verified on RDNA2** (`gfx1035`, wave32 and wave64 via `tools/int_hw_probe.py --oracle both`, SHADER-137). Measured on native gfx1036 (AMD Radeon Graphics, RDNA2) through tools/hw_oracle across 64-bit sliding window and masked byte values against the 32-bit accumulators: bit-exact with the ISA reference. | | `s_sext_i32_i8`, `s_sext_i32_i16`, `s_flbit_i32` | The first two sign-extend the low byte or the low half of S0 to 32 bits (`OpBitFieldSExtract`). `s_flbit_i32` counts how many bits in a row from the MSB equal the sign bit, and gives −1 when all bits are the same (0 and −1); it is the scalar form of `v_ffbh_i32` and goes through the same code. None of the three writes SCC. Source: RDNA2 ISA SOP1 opcodes 23, 25 and 26. CPU-checked by `tests/semantic/test_salu_sext_flbit.py` (#549): edge words with SCC starting at 0 and at 1, and 30 seeded values with long runs of sign bits. Not in the translated corpus, not GPU-checked. **Hardware-verified on RDNA2** (`gfx1035`, wave32 and wave64 via `tools/int_hw_probe.py --oracle both`, SHADER-137, SCC kept). Measured on native gfx1036 (AMD Radeon Graphics, RDNA2) through tools/hw_oracle and tools/salu_diff.py (500 programs, --oracle translator,hw): 500 pass, 0 mismatches against the reference; confirmed s_flbit_i32 gives 0xFFFFFFFF for 0 and -1. | | `v_lerp_u8` | Per byte i: `D.b[i] = (S0.b[i] + S1.b[i] + (S2.b[i] & 1)) >> 1`, an unsigned average where bit 0 of S2's byte decides whether 0.5 rounds up. The translator uses the per-byte identity `x + y = 2*(x & y) + (x ^ y)`. It clears each byte's low bit before shifting, then adds the rounding bit only to an odd sum. Each result fits in one byte, so the packed additions do not carry across bytes. The 9-bit sum shifted right by 1 fits back in its byte, so nothing carries between bytes. `clamp` raises `Unsupported` (the ISA doesn't define it for this op). Source: RDNA2 ISA VOP3 opcode 333. CPU-checked by `tests/semantic/test_alu_lerp_u8.py` (#577): a 9×9×5 grid of edge and rounding words, 40 seeded cases, an SGPR source with an inline S2, `clamp` rejected. The synthetic `tests/semantic/fixtures/lerp_packed_bytes.asm` gives exact words across a 64x64 draw on an RTX 3070 (NVIDIA 591.86). Not in the translated corpus, not GPU-checked. **Hardware-verified on RDNA2** (`gfx1035`, wave32 and wave64 via `tools/int_hw_probe.py --oracle both`, SHADER-137). Measured on native gfx1036 (AMD Radeon Graphics, RDNA2) through tools/hw_oracle across byte edge values and rounding bit variations: 0 mismatches against the ISA reference. | +| `v_sat_pk_u8_i16` | `D[15:0] = {sat_u8(S0[31:16].i16), sat_u8(S0[15:0].i16)}`: each signed i16 lane of S0 saturates to an unsigned byte and packs into the low 16 bits of D (AMD machine-readable ISA semantics, VOP1 0x62 / VOP3 0x1E2). The 16-bit result keeps the destination's high half, the same partial write as `v_cvt_f16_f32` (Mesa ACO `instr_is_16bit`); under `_sdwa` `dst_sel`/`dst_unused` decide instead. Source `neg`/`abs`, `clamp` and output modifiers raise `Unsupported` rather than being guessed. CPU-checked by `tests/semantic/test_alu_sat_pk_u8_i16.py` (hand-written `tests/semantic/fixtures/sat_pk_u8_i16.asm`, lane edge values, SGPR/literal/inline sources, `_sdwa` source and destination selects, refused modifiers) against a plain-Python reference. Not in the 26,503-shader corpus (listed in `docs/fssm_crosscheck.md`), not GPU-verified. | | Scalar select and pack (`s_cmov_b32`, `s_cmovk_i32`, `s_cmov_b64`, `s_pack_ll/lh/hh_b32_b16`) | `s_cmov_b32` / `s_cmovk_i32`: D = SCC ? S0 : D (`s_cmovk_i32`'s S0 is the 16-bit immediate, sign-extended); SCC is read, not written; the destination must be an SGPR (a lane-mask destination stays `Unsupported`, #210). `s_cmov_b64`: D[63:0] = SCC ? S0[63:0] : D[63:0] on plain 64-bit data pairs (SGPR pair, immediate or `null`), SCC read and not written; a pair that is or has been a lane mask (EXEC, VCC or a mask SGPR) stays `Unsupported` (#210) — conservatively, because a CFG join can drop a register that is a mask on only one path — while 0/-1 EITHER constants are data; selecting a per-lane bit as data would silently return a bool instead of the wave's mask. D[63:0] on plain 64-bit data pairs (SGPR pair, immediate or `null`), SCC read and not written; a pair that is or has been a lane mask stays `Unsupported` (#210). D[63:0], SCC read and not written, on plain 64-bit data pairs (SGPR pair, immediate or `null`); any dword that is or has been a lane mask (EXEC, VCC or a mask SGPR) makes the pair `Unsupported` — conservatively, because a CFG join can drop a register that is a mask on only one path — while 0/-1 EITHER constants are data; selecting a per-lane bit as data would silently return a bool instead of the wave's mask (#210). `s_pack_*`: the low half of D from S0, the high half from S1, the letters naming the half of each (l = 15:0, h = 31:16), S0 first; GFX10 has no `s_pack_hl`; SCC not written. Source: RDNA2 ISA SOP1/SOP2/SOPK and LLVM AMDGPU `SOPInstructions.td` (`S_CMOV_B64 : RDNA2 ISA SOP1/SOP2/SOPK. CPU-checked by `tests/semantic/test_salu_cmov.py` and `test_salu_pack.py`; the translated shaders were run on NVIDIA by reviewers (`s_cmov*`: RDNA2 ISA SOP1/SOP2/SOPK and LLVM AMDGPU [SOPInstructions.td](https://github.com/llvm/llvm-project/blob/2bfedf3338162e2c841a73cd7cd9e98d00add608/llvm/lib/Target/AMDGPU/SOPInstructions.td) (`S_CMOV_B64 : SOP1_64` under `Uses = [SCC]`). CPU-checked by `tests/semantic/test_salu_cmov.py`, `tests/semantic/test_salu_cmov_b64.py` and `test_salu_pack.py`; no corpus count; **not GPU-verified, no corpus count**; the translated shaders were run on NVIDIA by reviewers (`s_cmov_b32/k_i32`: **no corpus count**; Hardware: on native `gfx1036` (Ryzen 9 9950X3D iGPU, Windows 11, driver 26.10.1) `tools/int_hw_probe.py --oracle both --wave both` matches the translator in wave32 and wave64 for all five `salu_cmov_pack` cases and for the 14 `s_cmov_b64` cases (`--group salu_cmov_b64`: GPU-checked for `s_cmov_b32` (`cs_w32_salu64.asm`, SCC = 1 selects S0) and `s_pack_lh_b32_b16` (`cs_w32_int_ext.asm`) on the Radeon iGPU of a Ryzen 9 9950X3D (RDNA2, `gfx1036`, driver 26.10.1) and an RTX 5080 (driver 616.92); `s_cmovk_i32`, `s_cmov_b64` and the other `s_pack_*` forms have no GPU test on main; the translated shaders were also run on NVIDIA by reviewers (`s_cmov_b32/k_i32`: RTX 5090, RTX 4050 and Radeon 740M in #613; `s_pack_*`: RTX 5090 in #449). **Hardware-verified on RDNA2** (`gfx1035`, wave32 and wave64 via `tools/int_hw_probe.py --oracle both`, SHADER-137, SCC kept), and on native gfx1036 (AMD Radeon Graphics, Windows 11, driver 26.10.1) through `tools/hw_oracle`: all five `salu_cmov_pack` cases and the 14 `s_cmov_b64` cases match the translator in wave32 and wave64 (SCC 0 and 1; an SGPR pair, the inline constants -1/-16/64, the literal 0x89abcdef, `null` and the destination itself; SCC = 1 takes S0, 0 keeps D, SCC is never written), `s_cmov_b32` matches under SCC=0 and SCC=1, and `s_pack_ll/lh/hh_b32_b16` is bit-exact on 500 random programs with 0 mismatches (`--oracle translator,hw`). **Hardware-verified on RDNA2** (`gfx1035`, wave32 and wave64 via `tools/int_hw_probe.py --oracle both`, SHADER-137, SCC kept). Also **measured on RDNA2 gfx1036** (Radeon iGPU of a Ryzen 9 9950X3D, Windows 11, driver 26.10.1, `tools/hw_oracle` HIP launcher, rocm-sdk-core clang): `tools/int_hw_probe.py --oracle both --wave both` gives the same words as the translator for all five `salu_cmov_pack` cases in wave32 and wave64, and the 14 `s_cmov_b64` cases of the `salu_cmov_b64` group (`python -I tools/int_hw_probe.py --oracle both --wave both --group salu_cmov_b64`; SCC 0 and 1; source an SGPR pair, the inline constants -1, -16 and 64, the literal 0x89abcdef, `null`, and the destination itself; SCC preset and read back after): SCC preset and read back after) match the translator in wave32 and wave64: SCC = 1 takes S0 (a literal zero-extended, an inline constant sign-extended, `null` gives 0), SCC = 0 keeps D, SCC is never written. Also on `gfx1035` via `int_hw_probe` (SHADER-137, SCC kept), and on `gfx1036` and an RTX 5080 through the fixtures `cs_w32_salu64.asm` (`s_cmov_b32`) and `cs_w32_int_ext.asm` (`s_pack_lh_b32_b16`). `tools/hw_oracle` + `tools/salu_diff.py` verify `s_cmov_b32` under SCC 0/1 and `s_pack_ll/lh/hh_b32_b16` bit-exact on 500 random programs with 0 mismatches. Reviewers ran `s_cmov*` on an RTX 5090, an RTX 4050 and a Radeon 740M (#613) and `s_pack_*` on an RTX 5090 (#449). Measured on native gfx1036 (AMD Radeon Graphics, RDNA2) through tools/hw_oracle and tools/salu_diff.py: s_cmov_b32 verified under SCC=0 and SCC=1 (preserving destination and updating respectively); s_pack_ll/lh/hh_b32_b16 verified bit-exact on 500 random programs with 0 mismatches (--oracle translator,hw). **Not measured on RDNA2.** | | `v_bfi_b32` | `(S0 & S1) \| (~S0 & S2)` (RDNA2 ISA, VOP3 opcode 330). A literal or known same-block SGPR mask of zero or all ones selects S2 or S1 directly. A contiguous mask uses `OpBitFieldInsert` with S1 shifted down by the field offset, since RDNA2 reads the bits in place. For sparse constant masks the translator precomputes the complement. Runtime masks use the full bitwise expression. Checked by the hand-written `bfi_known_mask.asm` and `tests/semantic/test_bfi_known_mask.py`. | | `v_bfm_b32` | The low five bits of S0 give the width and those of S1 give the offset. The result is the low 32 bits of `((1 << width) - 1) << offset` (RDNA2 ISA §12.12, VOP3 opcode 867). Literal and same-block SGPR constants are folded in the translator, including zero width and shifts that discard the high bits. Runtime operands keep their low-five-bit masks. Checked by `tests/semantic/test_bfm_known_operands.py` and `test_alu_int_misc.py`. | diff --git a/docs/fssm_crosscheck.md b/docs/fssm_crosscheck.md index 7b85df1eac..3c2416e470 100644 --- a/docs/fssm_crosscheck.md +++ b/docs/fssm_crosscheck.md @@ -162,7 +162,7 @@ distinguish the readings. SHADER-190 remains open. | The walker is length-aware; base widths plus a literal dword for 1-dword families. | `rdna2_classify.instr_length()` | Base widths are right. Confirmed gaps: `s_setreg_imm32_b32` is 2 dwords but `instr_length()` returns 1; 68 of 100 sampled shaders contain it (150 occurrences), so the walk drifts at the first one. `v_add_f32_e64 v0, v1, 0x12345678` is 3 dwords but VOP3 is always 2. `mimg_nsa()` is a stub and MIMG is always 2 words. `fssm_parse.instructions()` does not use `instr_length()` at all; it uses base widths only. | **Incomplete** | | Branch targets resolve in-range 12,786/12,789 (SCARAB's corpus). | `fssm_parse.branch_report()`, `docs/fssm-analysis.md` | The SOPP branch opcodes (2, 4-9) and `i + 1 + simm16` are correct for gfx10. The check is self-consistent but cannot detect a constant `+0x10` start shift; Leonida's CFG builder additionally rejects a branch target outside the code range. | **Verified logic; not a correctness proof** | | The vendor disassembler (`XBSC_DisassembleRawShader`) is the oracle; all 25,321 streams disassemble with no fault. | `docs/isa/shader-isa-notes.md`, `tools/xbsc_probe_notes.md` | Cannot be re-run here (no vendor DLL in this workspace). The claim is plausible; the output header's `asic(CI)` label is not reconciled with gfx1013 and should not be read as a distinct ISA. | **Unknown (not re-run)** | -| The 1,542-name list documents opcodes the Leonida translator lacks. | `docs/isa/xbsc-isa-mnemonics.txt` | It documents names, not encodings. Names present in the list and **not handled** by `rdna2spirv.py`: `v_sat_pk_u8_i16`, `v_screen_partition_4se_b32`, `v_mad_mix_f32`, `v_dot2c_f32_f16`, `v_dot4c_i32_i8`, `v_mul_hi_i32_i24`, `v_mul_hi_u32_u24`, `s_absdiff_i32`, `v_add_i32`, `v_sub_i32`, `image_msaa_load`, `s_ttracedata_imm`, `s_cbranch_g_fork`, `s_cbranch_i_fork`, `s_cbranch_join`, `s_set_gpr_idx_on`, `s_set_gpr_idx_off`, `s_set_gpr_idx_mode`. None of these is in the translated 26,503-shader corpus (otherwise translation would have rejected the shader), so they are future-proofing, not current blockers. | **Names only; not actionable now** | +| The 1,542-name list documents opcodes the Leonida translator lacks. | `docs/isa/xbsc-isa-mnemonics.txt` | It documents names, not encodings. Names present in the list and **not handled** by `rdna2spirv.py`: `v_screen_partition_4se_b32`, `v_mad_mix_f32`, `v_dot2c_f32_f16`, `v_dot4c_i32_i8`, `v_mul_hi_i32_i24`, `v_mul_hi_u32_u24`, `s_absdiff_i32`, `v_add_i32`, `v_sub_i32`, `image_msaa_load`, `s_ttracedata_imm`, `s_cbranch_g_fork`, `s_cbranch_i_fork`, `s_cbranch_join`, `s_set_gpr_idx_on`, `s_set_gpr_idx_off`, `s_set_gpr_idx_mode`. None of these is in the translated 26,503-shader corpus (otherwise translation would have rejected the shader), so they are future-proofing, not current blockers. | **Names only; not actionable now** | | SCARAB's `rdna2_sweep` locates the first decoder rejection and decodes VOP3 modifier fields. | `src/rdna2_sweep.cpp` | It is a front-end coverage/boundary diagnostic: opcode coverage, word length, VOP3 `abs`/`op_sel`/`clamp`/`omod`/`neg` legality. It does **not** check register liveness, dataflow, control-flow semantics or output values. `spirv-val` checks the output module, not the input stream; neither one proves the translation. The differential-validation plan on Leonida's in-progress `shader-val` branch (`docs/shader_validation.md`, `tools/cpu_ref.py`) is the stronger class of check. | **Useful triage; not a substitute for `spirv-val` or differential tests** | | SCARAB's `rdna2_recompile` produces SPIR-V for the corpus. | `src/rdna2_recompile.cpp` | In practice it always builds a **compute** request (`numThreads 64x1x1`, no header, no memory) even when `--stage` is parsed; the `stage` variable is not used by `BuildComputeRequest`. It cannot validate the VS/PS/GS/mesh ABI. | **Incomplete for this corpus** | | The prior-art note concludes FSSM is inside the DXIL container and is not a direct `rgxe`/`SGD2` descendant; `FSSM`/`0x101`/`0xDEADBEEF` have no public prior art. | `docs/shader-container-prior-art.md` | The nesting is confirmed: `DXBC` precedes `FSSM`, and `FSSM` is the decompressed `XBCH` payload inside the `DXIL` chunk. The lineage and negative prior-art claims were not re-run. | **Nesting verified; lineage unknown** | diff --git a/tests/semantic/fixtures/sat_pk_u8_i16.asm b/tests/semantic/fixtures/sat_pk_u8_i16.asm new file mode 100644 index 0000000000..9d50210617 --- /dev/null +++ b/tests/semantic/fixtures/sat_pk_u8_i16.asm @@ -0,0 +1,16 @@ +; Each signed i16 lane of SRC0 saturates to an unsigned byte and packs into D[15:0]; the +; destination's high half is kept (the GFX10 16-bit partial write, as v_cvt_f16_f32). +; Expected MRT0 words: cafeff00 dead00ff 00008000 abcdffff. + v_mov_b32 v1, 0x0100fed4 + v_mov_b32 v2, 0xcafebabe + v_sat_pk_u8_i16 v2, v1 + v_mov_b32 v3, 0x80407fff + v_mov_b32 v4, 0xdeadbeef + v_sat_pk_u8_i16 v4, v3 + v_mov_b32 v5, 0x00000000 + v_mov_b32 v6, 0x0080ff00 + v_sat_pk_u8_i16_e64 v5, v6 + v_mov_b32 v7, 0xabcd0000 + v_sat_pk_u8_i16 v7, 0x00ff0100 + exp mrt0 v2, v4, v5, v7 done vm + s_endpgm diff --git a/tests/semantic/test_alu_sat_pk_u8_i16.py b/tests/semantic/test_alu_sat_pk_u8_i16.py new file mode 100644 index 0000000000..aadd760a0d --- /dev/null +++ b/tests/semantic/test_alu_sat_pk_u8_i16.py @@ -0,0 +1,94 @@ +"""v_sat_pk_u8_i16: each signed i16 lane of SRC0 saturates into a u8, packed into D[15:0]. + +Semantics from the AMD machine-readable RDNA2 ISA specification: D.b16 = {SAT8(S0[31:16].i16), +SAT8(S0[15:0].i16)} with SAT8(n) = 0 for n <= 0, 255 for n >= 255 and n[7:0] otherwise. On GFX10 the +16-bit result keeps the destination's high half, the same partial write as v_cvt_f16_f32 (Mesa ACO +instr_is_16bit); under _sdwa the dst_sel/dst_unused rule decides instead. Hand-written pixel-shader +listings are translated by tools/rdna2spirv.py and run by spirv_mini_interp.py; expected words are +hand-computed and checked against a plain-Python reference. +""" + +from pathlib import Path +import unittest + +from support import HERE, mrt0_words, translate_lines, translate_listing # noqa: F401 +import rdna2spirv # noqa: E402 (sys.path set by support above) + + +def ref_sat_pk_u8_i16(s0): + def sat8(x): + if x & 0x8000: + x -= 0x10000 + return 0 if x <= 0 else (255 if x >= 255 else x) + + return sat8(s0 & 0xFFFF) | (sat8((s0 >> 16) & 0xFFFF) << 8) + + +class SatPkU8I16(unittest.TestCase): + def test_fixture(self): + fixture = Path(HERE) / "fixtures" / "sat_pk_u8_i16.asm" + text = translate_listing("ps_sat_pk_u8_i16", fixture.read_bytes()) + self.assertEqual(mrt0_words(text), [0xCAFEFF00, 0xDEAD00FF, 0x00008000, 0xABCDFFFF]) + self.assertEqual(text.count("OpExtInst %int %glsl SClamp"), 8) + + def test_saturates_each_signed_lane_to_u8_and_keeps_high_half(self): + def run(s0, seed): + lines = [ + f"v_mov_b32 v1, 0x{s0:08x}", + f"v_mov_b32 v2, 0x{seed:08x}", + "v_sat_pk_u8_i16 v2, v1", + "exp mrt0 v2, v2, v2, v2 done vm", + "s_endpgm", + ] + return mrt0_words(translate_lines("ps_sat_pk", lines))[0] + + for s0 in (0x00000000, 0x00FF0080, 0x0100FE00, 0x80007FFF, + 0x7FFF8000, 0x00FFFEFE, 0x12345678): + for seed in (0x00000000, 0xDEADBEEF): + self.assertEqual(run(s0, seed), (seed & 0xFFFF0000) | ref_sat_pk_u8_i16(s0), + f"s0={s0:#010x} seed={seed:#010x}") + + def test_sgpr_and_literal_sources(self): + text = translate_lines("ps_sat_pk_sgpr", [ + "s_mov_b32 s0, 0x8040ff00", + "s_mov_b32 s8, m0", + "v_mov_b32 v1, 0x11111111", + "v_mov_b32 v2, 0x22222222", + "v_sat_pk_u8_i16 v1, s0", + "v_sat_pk_u8_i16 v2, 0x00ff0100", + "v_sat_pk_u8_i16 v3, -1", + "v_sat_pk_u8_i16 v4, 5", + "exp mrt0 v1, v2, v3, v4 done vm", + "s_endpgm", + ]) + self.assertEqual(mrt0_words(text), [0x11110000, 0x2222FFFF, 0x00000000, 0x00000005]) + + def test_sdwa_source_and_destination(self): + text = translate_lines("ps_sat_pk_sdwa", [ + "v_mov_b32 v1, 0x1234fed4", + "v_mov_b32 v2, 0x0008ff40", + "v_mov_b32 v3, 0x12345678", + "v_sat_pk_u8_i16_sdwa v4, v1 dst_sel:DWORD dst_unused:UNUSED_PAD src0_sel:WORD_1", + "v_sat_pk_u8_i16_sdwa v3, v2 dst_sel:WORD_1 dst_unused:UNUSED_PRESERVE src0_sel:DWORD", + "v_sat_pk_u8_i16_sdwa v5, v1 dst_sel:DWORD dst_unused:UNUSED_PAD src0_sel:BYTE_0", + "exp mrt0 v4, v3, v5, v5 done vm", + "s_endpgm", + ]) + # WORD_1 zero-extends 0x1234: {sat(0x1234)=255, sat(0)=0} into a full DWORD write. + # WORD_1 dst keeps v3's low half: {sat(8)=8, sat(-192)=0} -> 0x0800 into bits 31:16. + # BYTE_0 zero-extends 0xd4: {sat(212)=212, sat(0)=0} into a full DWORD write. + self.assertEqual(mrt0_words(text), [0x000000FF, 0x08005678, 0x000000D4, 0x000000D4]) + + def test_modifiers_are_refused_not_guessed(self): + for line in ("v_sat_pk_u8_i16_e64 v2, -v1", + "v_sat_pk_u8_i16_e64 v2, |v1|", + "v_sat_pk_u8_i16_e64 v2, v1 clamp", + "v_sat_pk_u8_i16_e64 v2, v1 mul:2", + "v_sat_pk_u8_i16_sdwa v2, v1 dst_sel:DWORD dst_unused:UNUSED_PAD " + "src0_sel:WORD_1 clamp"): + with self.assertRaises(rdna2spirv.Unsupported, msg=line): + translate_lines("ps_sat_pk_bad", [line, "exp mrt0 v2, v2, v2, v2 done vm", "s_endpgm"]) + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/test_f16_op_sel.py b/tests/test_f16_op_sel.py index 64041e7e7a..ac07c82030 100644 --- a/tests/test_f16_op_sel.py +++ b/tests/test_f16_op_sel.py @@ -98,4 +98,4 @@ def test_translation_is_deterministic(tmp_path): def test_other_unsupported_instructions_are_still_rejected(tmp_path): with pytest.raises(rdna2spirv.Unsupported): - translate(tmp_path, "v_sat_pk_u8_i16 v3, v1") + translate(tmp_path, "v_mullit_f32 v3, v1, v1, v1") diff --git a/tests/test_translate.py b/tests/test_translate.py index d151c36e28..186d14419c 100644 --- a/tests/test_translate.py +++ b/tests/test_translate.py @@ -124,7 +124,7 @@ def test_translation_is_deterministic(make_shader): def test_unsupported_instruction_is_reported_not_guessed(make_shader): """Anything outside the supported set must raise Unsupported rather than emit a wrong program.""" - shader = make_shader(program("v_mov_b32 v1, 1.0\nv_sat_pk_u8_i16 v0, v1")) + shader = make_shader(program("v_mov_b32 v1, 1.0\nv_mullit_f32 v0, v1, v1, v1")) with pytest.raises(rdna2spirv.Unsupported): shader.translate() diff --git a/tools/rdna2spirv.py b/tools/rdna2spirv.py index dbe3668971..bd0ef5baec 100644 --- a/tools/rdna2spirv.py +++ b/tools/rdna2spirv.py @@ -11402,6 +11402,35 @@ def f64(sv): self.emit(f"{sh} = OpShiftLeftLogical %uint {halves[1]} {self.cu(16)}") self.emit(f"{r} = OpBitwiseOr %uint {halves[0]} {sh}") self.dst(a[0], r) + elif op == "v_sat_pk_u8_i16": + # D.b16 = {sat_u8(S0.i16 hi), sat_u8(S0.i16 lo)}: each signed 16-bit lane of S0 saturates to + # an unsigned byte and packs into the low 16 bits of D (AMD machine-readable ISA semantics). + # On GFX10 the 16-bit result keeps the destination's high half, as v_cvt_f16_f32 (Mesa ACO + # instr_is_16bit); under _sdwa the dst_sel/dst_unused rule decides instead. neg/abs on the + # integer source, clamp and omod are refused rather than guessed. + if a[1].get("neg") or a[1].get("abs"): + raise Unsupported(op + " modifiers") + if ins["op"].endswith("_sdwa"): + bad_mod = any( + m.split(":", 1)[0] not in ("src0_sel", "dst_sel", "dst_unused") for m in mods) + elif ins["op"].endswith("_dpp"): + bad_mod = bool(self.non_dpp_mods(ins["op"], mods)) + else: + bad_mod = bool(mods) + if bad_mod: + raise Unsupported(op + " modifiers") + x = self.src_u(a[1]) + bytes_ = [] + for half in range(2): + s, b = self.tmp(), self.tmp() + self.emit(f"{s} = OpExtInst %int %glsl SClamp {self.i16_half(x, half)} " + f"{self.as_int(self.cu(0))} {self.as_int(self.cu(0xFF))}") + self.emit(f"{b} = OpBitcast %uint {s}") + bytes_.append(b) + sh, r = self.tmp(), self.tmp() + self.emit(f"{sh} = OpShiftLeftLogical %uint {bytes_[1]} {self.cu(8)}") + self.emit(f"{r} = OpBitwiseOr %uint {bytes_[0]} {sh}") + self.write_lo16(a[0], r) elif op in ("v_cvt_pknorm_i16_f32", "v_cvt_pknorm_u16_f32"): # D = {norm16(S1), norm16(S0)}: each f32 clamped to [-1, 1] (i16) or [0, 1] (u16), scaled by 32767 or # 65535 and rounded to nearest even. Mesa ACO emits these for NIR pack_snorm_2x16 / pack_unorm_2x16, From 423444931dbe04789601eac5f580bb4d19e7fbf1 Mon Sep 17 00:00:00 2001 From: milk4sale <53350507+milk4sale@users.noreply.github.com> Date: Tue, 24 Nov 2026 00:29:08 -0600 Subject: [PATCH 2/3] wave64_halves: v_sat_pk_u8_i16 keeps its high half (SHADER-SATPK-HALVES) #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 #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 --- docs/design.md | 1 + runtime/test/shaders/ps_w64_a_sat_pk_hi.asm | 49 +++++++ runtime/test/shaders/ps_w64_c_sat_pk_hi.asm | 35 +++++ runtime/test/shaders/w64_graphics.py | 23 +++ tests/semantic/fixtures/w64_sat_pk_hi.asm | 23 +++ tests/semantic/test_wave64_halves_sat_pk.py | 149 ++++++++++++++++++++ tests/test_wave64_halves_fixtures.py | 10 ++ tools/wave64_halves.py | 8 +- 8 files changed, 296 insertions(+), 2 deletions(-) create mode 100644 runtime/test/shaders/ps_w64_a_sat_pk_hi.asm create mode 100644 runtime/test/shaders/ps_w64_c_sat_pk_hi.asm create mode 100644 tests/semantic/fixtures/w64_sat_pk_hi.asm create mode 100644 tests/semantic/test_wave64_halves_sat_pk.py diff --git a/docs/design.md b/docs/design.md index 82d052802f..decd25a097 100644 --- a/docs/design.md +++ b/docs/design.md @@ -435,6 +435,7 @@ How the generated SPIR-V expects the runtime to work: | `s_sext_i32_i8`, `s_sext_i32_i16`, `s_flbit_i32` | The first two sign-extend the low byte or the low half of S0 to 32 bits (`OpBitFieldSExtract`). `s_flbit_i32` counts how many bits in a row from the MSB equal the sign bit, and gives −1 when all bits are the same (0 and −1); it is the scalar form of `v_ffbh_i32` and goes through the same code. None of the three writes SCC. Source: RDNA2 ISA SOP1 opcodes 23, 25 and 26. CPU-checked by `tests/semantic/test_salu_sext_flbit.py` (#549): edge words with SCC starting at 0 and at 1, and 30 seeded values with long runs of sign bits. Not in the translated corpus, not GPU-checked. **Hardware-verified on RDNA2** (`gfx1035`, wave32 and wave64 via `tools/int_hw_probe.py --oracle both`, SHADER-137, SCC kept). Measured on native gfx1036 (AMD Radeon Graphics, RDNA2) through tools/hw_oracle and tools/salu_diff.py (500 programs, --oracle translator,hw): 500 pass, 0 mismatches against the reference; confirmed s_flbit_i32 gives 0xFFFFFFFF for 0 and -1. | | `v_lerp_u8` | Per byte i: `D.b[i] = (S0.b[i] + S1.b[i] + (S2.b[i] & 1)) >> 1`, an unsigned average where bit 0 of S2's byte decides whether 0.5 rounds up. The translator uses the per-byte identity `x + y = 2*(x & y) + (x ^ y)`. It clears each byte's low bit before shifting, then adds the rounding bit only to an odd sum. Each result fits in one byte, so the packed additions do not carry across bytes. The 9-bit sum shifted right by 1 fits back in its byte, so nothing carries between bytes. `clamp` raises `Unsupported` (the ISA doesn't define it for this op). Source: RDNA2 ISA VOP3 opcode 333. CPU-checked by `tests/semantic/test_alu_lerp_u8.py` (#577): a 9×9×5 grid of edge and rounding words, 40 seeded cases, an SGPR source with an inline S2, `clamp` rejected. The synthetic `tests/semantic/fixtures/lerp_packed_bytes.asm` gives exact words across a 64x64 draw on an RTX 3070 (NVIDIA 591.86). Not in the translated corpus, not GPU-checked. **Hardware-verified on RDNA2** (`gfx1035`, wave32 and wave64 via `tools/int_hw_probe.py --oracle both`, SHADER-137). Measured on native gfx1036 (AMD Radeon Graphics, RDNA2) through tools/hw_oracle across byte edge values and rounding bit variations: 0 mismatches against the ISA reference. | | `v_sat_pk_u8_i16` | `D[15:0] = {sat_u8(S0[31:16].i16), sat_u8(S0[15:0].i16)}`: each signed i16 lane of S0 saturates to an unsigned byte and packs into the low 16 bits of D (AMD machine-readable ISA semantics, VOP1 0x62 / VOP3 0x1E2). The 16-bit result keeps the destination's high half, the same partial write as `v_cvt_f16_f32` (Mesa ACO `instr_is_16bit`); under `_sdwa` `dst_sel`/`dst_unused` decide instead. Source `neg`/`abs`, `clamp` and output modifiers raise `Unsupported` rather than being guessed. CPU-checked by `tests/semantic/test_alu_sat_pk_u8_i16.py` (hand-written `tests/semantic/fixtures/sat_pk_u8_i16.asm`, lane edge values, SGPR/literal/inline sources, `_sdwa` source and destination selects, refused modifiers) against a plain-Python reference. Not in the 26,503-shader corpus (listed in `docs/fssm_crosscheck.md`), not GPU-verified. | +| `v_sat_pk_u8_i16` on the wave64 halves path (SHADER-SATPK-HALVES) | `tools/wave64_halves.py` reads the old destination of `v_sat_pk_u8_i16` wherever the translator keeps its high half: the `_e32`, `_e64` and DPP forms (the 16-bit rule of `_preserves_dst`, and `partial_write`, which had taken the op's first type, `u8`, for an 8-bit result), and SDWA with a `BYTE`/`WORD` `dst_sel` and `UNUSED_PRESERVE`; SDWA with `dst_sel:DWORD`, `UNUSED_PAD` or `UNUSED_SEXT` writes the whole register. The op used to be in `_FULL32_V16_PREFIXES`, so a high half that depends on the lane lost its taint and the shader passed as tier A (or tier B through `readlane-uniform`), though its export changes when the wave is cut in two. The rule follows what the translator emits (`write_lo16`, row above); if RDNA2 turns out to zero the high half, calling the op a partial write stays safe and costs tier A at most. **References:** neither compiler says the high half is kept on GFX10. Mesa ACO's `instr_is_16bit` is false for the op before GFX11: it is not in the function's list, and `can_use_opsel` falls through to `get_gfx11_true16_mask`, which gives it 0x8 (the destination bit, as for `v_cvt_f16_f32`) for GFX11 and later only (`src/amd/compiler/aco_ir.cpp`, Mesa `99abc485`). LLVM defines it with `VOP1Inst_t16` (`VOP1Instructions.td`), which on GFX10 is the plain VOP1 instruction with an i16 result (`VOP_I16_I32`; the `_t16`/`_fake16` forms are GFX11 and later), and `GCNSubtarget::zeroesHigh16BitsOfDest` does not list it, so LLVM assumes neither a kept nor a zeroed high half (`llvm/lib/Target/AMDGPU`, LLVM `e2b0a1e1`). What the RDNA2 instruction does with the high half is **not measured**. CPU-checked by `tests/semantic/test_wave64_halves_sat_pk.py` (the hand-written `fixtures/w64_sat_pk_hi.asm`, whose high half is the lane id, run on the strict wave interpreter as 64 lanes and as two waves of 32 in every encoding, plus the `readlane-uniform` case) and by `tests/test_wave64_halves_fixtures.py` (`ps_w64_c_sat_pk_hi` must be tier C, `lane-id`). GPU: `ps_w64_a_sat_pk_hi` (tier A: the four encodings that keep the high half, a different high half per pixel, both halves of SRC0 saturating below 0 and above 255 across the target) gives its derived words on an NVIDIA RTX 4070 Ti (driver 620.70.06, Linux). NVIDIA runs the translated SPIR-V, so this confirms the translator's semantics, not the AMD instruction: the expected low half is the ISA pseudocode's SAT8 packing, the expected high half is the translator's rule. `ps_w64_c_sat_pk_hi` is a `SKIP` there (32 is the only fragment subgroup size; tier C needs a device that can require both 64 and 32, or `W64G_REF`). | | Scalar select and pack (`s_cmov_b32`, `s_cmovk_i32`, `s_cmov_b64`, `s_pack_ll/lh/hh_b32_b16`) | `s_cmov_b32` / `s_cmovk_i32`: D = SCC ? S0 : D (`s_cmovk_i32`'s S0 is the 16-bit immediate, sign-extended); SCC is read, not written; the destination must be an SGPR (a lane-mask destination stays `Unsupported`, #210). `s_cmov_b64`: D[63:0] = SCC ? S0[63:0] : D[63:0] on plain 64-bit data pairs (SGPR pair, immediate or `null`), SCC read and not written; a pair that is or has been a lane mask (EXEC, VCC or a mask SGPR) stays `Unsupported` (#210) — conservatively, because a CFG join can drop a register that is a mask on only one path — while 0/-1 EITHER constants are data; selecting a per-lane bit as data would silently return a bool instead of the wave's mask. D[63:0] on plain 64-bit data pairs (SGPR pair, immediate or `null`), SCC read and not written; a pair that is or has been a lane mask stays `Unsupported` (#210). D[63:0], SCC read and not written, on plain 64-bit data pairs (SGPR pair, immediate or `null`); any dword that is or has been a lane mask (EXEC, VCC or a mask SGPR) makes the pair `Unsupported` — conservatively, because a CFG join can drop a register that is a mask on only one path — while 0/-1 EITHER constants are data; selecting a per-lane bit as data would silently return a bool instead of the wave's mask (#210). `s_pack_*`: the low half of D from S0, the high half from S1, the letters naming the half of each (l = 15:0, h = 31:16), S0 first; GFX10 has no `s_pack_hl`; SCC not written. Source: RDNA2 ISA SOP1/SOP2/SOPK and LLVM AMDGPU `SOPInstructions.td` (`S_CMOV_B64 : RDNA2 ISA SOP1/SOP2/SOPK. CPU-checked by `tests/semantic/test_salu_cmov.py` and `test_salu_pack.py`; the translated shaders were run on NVIDIA by reviewers (`s_cmov*`: RDNA2 ISA SOP1/SOP2/SOPK and LLVM AMDGPU [SOPInstructions.td](https://github.com/llvm/llvm-project/blob/2bfedf3338162e2c841a73cd7cd9e98d00add608/llvm/lib/Target/AMDGPU/SOPInstructions.td) (`S_CMOV_B64 : SOP1_64` under `Uses = [SCC]`). CPU-checked by `tests/semantic/test_salu_cmov.py`, `tests/semantic/test_salu_cmov_b64.py` and `test_salu_pack.py`; no corpus count; **not GPU-verified, no corpus count**; the translated shaders were run on NVIDIA by reviewers (`s_cmov_b32/k_i32`: **no corpus count**; Hardware: on native `gfx1036` (Ryzen 9 9950X3D iGPU, Windows 11, driver 26.10.1) `tools/int_hw_probe.py --oracle both --wave both` matches the translator in wave32 and wave64 for all five `salu_cmov_pack` cases and for the 14 `s_cmov_b64` cases (`--group salu_cmov_b64`: GPU-checked for `s_cmov_b32` (`cs_w32_salu64.asm`, SCC = 1 selects S0) and `s_pack_lh_b32_b16` (`cs_w32_int_ext.asm`) on the Radeon iGPU of a Ryzen 9 9950X3D (RDNA2, `gfx1036`, driver 26.10.1) and an RTX 5080 (driver 616.92); `s_cmovk_i32`, `s_cmov_b64` and the other `s_pack_*` forms have no GPU test on main; the translated shaders were also run on NVIDIA by reviewers (`s_cmov_b32/k_i32`: RTX 5090, RTX 4050 and Radeon 740M in #613; `s_pack_*`: RTX 5090 in #449). **Hardware-verified on RDNA2** (`gfx1035`, wave32 and wave64 via `tools/int_hw_probe.py --oracle both`, SHADER-137, SCC kept), and on native gfx1036 (AMD Radeon Graphics, Windows 11, driver 26.10.1) through `tools/hw_oracle`: all five `salu_cmov_pack` cases and the 14 `s_cmov_b64` cases match the translator in wave32 and wave64 (SCC 0 and 1; an SGPR pair, the inline constants -1/-16/64, the literal 0x89abcdef, `null` and the destination itself; SCC = 1 takes S0, 0 keeps D, SCC is never written), `s_cmov_b32` matches under SCC=0 and SCC=1, and `s_pack_ll/lh/hh_b32_b16` is bit-exact on 500 random programs with 0 mismatches (`--oracle translator,hw`). **Hardware-verified on RDNA2** (`gfx1035`, wave32 and wave64 via `tools/int_hw_probe.py --oracle both`, SHADER-137, SCC kept). Also **measured on RDNA2 gfx1036** (Radeon iGPU of a Ryzen 9 9950X3D, Windows 11, driver 26.10.1, `tools/hw_oracle` HIP launcher, rocm-sdk-core clang): `tools/int_hw_probe.py --oracle both --wave both` gives the same words as the translator for all five `salu_cmov_pack` cases in wave32 and wave64, and the 14 `s_cmov_b64` cases of the `salu_cmov_b64` group (`python -I tools/int_hw_probe.py --oracle both --wave both --group salu_cmov_b64`; SCC 0 and 1; source an SGPR pair, the inline constants -1, -16 and 64, the literal 0x89abcdef, `null`, and the destination itself; SCC preset and read back after): SCC preset and read back after) match the translator in wave32 and wave64: SCC = 1 takes S0 (a literal zero-extended, an inline constant sign-extended, `null` gives 0), SCC = 0 keeps D, SCC is never written. Also on `gfx1035` via `int_hw_probe` (SHADER-137, SCC kept), and on `gfx1036` and an RTX 5080 through the fixtures `cs_w32_salu64.asm` (`s_cmov_b32`) and `cs_w32_int_ext.asm` (`s_pack_lh_b32_b16`). `tools/hw_oracle` + `tools/salu_diff.py` verify `s_cmov_b32` under SCC 0/1 and `s_pack_ll/lh/hh_b32_b16` bit-exact on 500 random programs with 0 mismatches. Reviewers ran `s_cmov*` on an RTX 5090, an RTX 4050 and a Radeon 740M (#613) and `s_pack_*` on an RTX 5090 (#449). Measured on native gfx1036 (AMD Radeon Graphics, RDNA2) through tools/hw_oracle and tools/salu_diff.py: s_cmov_b32 verified under SCC=0 and SCC=1 (preserving destination and updating respectively); s_pack_ll/lh/hh_b32_b16 verified bit-exact on 500 random programs with 0 mismatches (--oracle translator,hw). **Not measured on RDNA2.** | | `v_bfi_b32` | `(S0 & S1) \| (~S0 & S2)` (RDNA2 ISA, VOP3 opcode 330). A literal or known same-block SGPR mask of zero or all ones selects S2 or S1 directly. A contiguous mask uses `OpBitFieldInsert` with S1 shifted down by the field offset, since RDNA2 reads the bits in place. For sparse constant masks the translator precomputes the complement. Runtime masks use the full bitwise expression. Checked by the hand-written `bfi_known_mask.asm` and `tests/semantic/test_bfi_known_mask.py`. | | `v_bfm_b32` | The low five bits of S0 give the width and those of S1 give the offset. The result is the low 32 bits of `((1 << width) - 1) << offset` (RDNA2 ISA §12.12, VOP3 opcode 867). Literal and same-block SGPR constants are folded in the translator, including zero width and shifts that discard the high bits. Runtime operands keep their low-five-bit masks. Checked by `tests/semantic/test_bfm_known_operands.py` and `test_alu_int_misc.py`. | diff --git a/runtime/test/shaders/ps_w64_a_sat_pk_hi.asm b/runtime/test/shaders/ps_w64_a_sat_pk_hi.asm new file mode 100644 index 0000000000..c2e22aba8f --- /dev/null +++ b/runtime/test/shaders/ps_w64_a_sat_pk_hi.asm @@ -0,0 +1,49 @@ +; SHADER-SATPK-HALVES tier A (docs/design.md, "v_sat_pk_u8_i16"): v_sat_pk_u8_i16 writes the low half of its VGPR and +; keeps the high half, in each encoding that keeps it (e32, e64, DPP, SDWA dst_sel:WORD_0 with UNUSED_PRESERVE). Every +; word is a function of the pixel, x = floor(u * 64), y = floor(v * 64) on the 64x64 target of vs_ids, so the shader +; gives the same exports when its 64 lanes are cut into two 32-lane subgroups (tools/wave64_halves.py: tier A). +; SRC0 = hi << 16 | lo with lo = 8x - 128 (-128..376 across x) and hi = 300 - 8y (300..-204 across y), 16 bits each: +; both signed halves saturate below 0 and above 255 across the target. s = sat8(hi) << 8 | sat8(lo), where sat8 clamps +; a signed 16-bit value to 0..255 (the ISA pseudocode). p = 64 * y + x. Each destination is seeded with its own high +; half (and a low half the write must replace): +; r0 = v_sat_pk_u8_i16_e32 into p << 16 | 0xa5a5: p << 16 | s +; r1 = v_sat_pk_u8_i16_e64 into (0xff00 - p) << 16 | 0x1234: (0xff00 - p) << 16 | s +; r2 = v_sat_pk_u8_i16_dpp (quad_perm identity, all rows and banks) +; into (p ^ 0x5555) << 16 | 0x0f0f: (p ^ 0x5555) << 16 | s +; r3 = v_sat_pk_u8_i16_sdwa dst_sel:WORD_0 dst_unused:UNUSED_PRESERVE +; into x << 24 | y << 16 | 0xffff: x << 24 | y << 16 | s +; The high halves are what the translator's write_lo16 keeps; expected on any Vulkan GPU, since the GPU runs the +; translated SPIR-V (this checks the translator, not the RDNA2 instruction itself). +; Run: vk_harness vs_ids.spv ps_w64_a_sat_pk_hi.spv out.raw, with VKH_SUBGROUP_SIZE=64 and 32 where both can be +; required: the two images equal each other and the words above. +; mattr: +wavefrontsize64,-wavefrontsize32 +v_interp_p1_f32_e32 v2, v0, attr0.x +v_interp_p2_f32_e32 v2, v1, attr0.x +v_interp_p1_f32_e32 v3, v0, attr0.y +v_interp_p2_f32_e32 v3, v1, attr0.y +v_mul_f32_e32 v2, 0x42800000, v2 +v_mul_f32_e32 v3, 0x42800000, v3 +v_cvt_u32_f32_e32 v2, v2 +v_cvt_u32_f32_e32 v3, v3 +s_mov_b64 s[10:11], exec +v_lshlrev_b32_e32 v4, 3, v2 +v_subrev_nc_u32_e32 v4, 0x80, v4 +v_and_b32_e32 v4, 0xffff, v4 +v_lshlrev_b32_e32 v5, 3, v3 +v_sub_nc_u32_e32 v5, 0x12c, v5 +v_lshl_or_b32 v4, v5, 16, v4 +v_lshl_add_u32 v10, v3, 6, v2 +v_lshl_or_b32 v6, v10, 16, 0xa5a5 +v_sub_nc_u32_e32 v7, 0xff00, v10 +v_lshl_or_b32 v7, v7, 16, 0x1234 +v_xor_b32_e32 v8, 0x5555, v10 +v_lshl_or_b32 v8, v8, 16, 0xf0f +v_lshlrev_b32_e32 v9, 8, v2 +v_or_b32_e32 v9, v9, v3 +v_lshl_or_b32 v9, v9, 16, 0xffff +v_sat_pk_u8_i16_e32 v6, v4 +v_sat_pk_u8_i16_e64 v7, v4 +v_sat_pk_u8_i16_dpp v8, v4 quad_perm:[0,1,2,3] row_mask:0xf bank_mask:0xf bound_ctrl:1 +v_sat_pk_u8_i16_sdwa v9, v4 dst_sel:WORD_0 dst_unused:UNUSED_PRESERVE src0_sel:DWORD +exp mrt0 v6, v7, v8, v9 done vm +s_endpgm diff --git a/runtime/test/shaders/ps_w64_c_sat_pk_hi.asm b/runtime/test/shaders/ps_w64_c_sat_pk_hi.asm new file mode 100644 index 0000000000..8ba6991a20 --- /dev/null +++ b/runtime/test/shaders/ps_w64_c_sat_pk_hi.asm @@ -0,0 +1,35 @@ +; SHADER-SATPK-HALVES tier C: v_sat_pk_u8_i16 keeps the high half of its VGPR, and here the high half is the raw lane +; number 0..63 (v_mbcnt_lo/hi of -1), so the export depends on how the wave is cut and tools/wave64_halves.py must call +; the shader tier C (it read the old destination of the write only after v_sat_pk left _FULL32_V16_PREFIXES). x, y, +; SRC0 and s as in ps_w64_a_sat_pk_hi (lo = 8x - 128, hi = 300 - 8y, s = sat8(hi) << 8 | sat8(lo)). +; r0 = lane id << 16 | s (v_sat_pk_u8_i16_e32 into lane id << 16 | 0xa5a5) +; r1 = r0 >> 16 (the kept high half read back: the lane id) +; r2 = r0 & 0xffff = s (the same on any partition) +; r3 = 0 +; Run: vk_harness vs_ids.spv ps_w64_c_sat_pk_hi.spv out.raw with VKH_SUBGROUP_SIZE=64 and 32: r2 and r3 equal, r0 and +; r1 differ (on 32-lane subgroups the lane id is 0..31 twice). +; mattr: +wavefrontsize64,-wavefrontsize32 +v_interp_p1_f32_e32 v2, v0, attr0.x +v_interp_p2_f32_e32 v2, v1, attr0.x +v_interp_p1_f32_e32 v3, v0, attr0.y +v_interp_p2_f32_e32 v3, v1, attr0.y +v_mul_f32_e32 v2, 0x42800000, v2 +v_mul_f32_e32 v3, 0x42800000, v3 +v_cvt_u32_f32_e32 v2, v2 +v_cvt_u32_f32_e32 v3, v3 +s_mov_b64 s[10:11], exec +v_lshlrev_b32_e32 v4, 3, v2 +v_subrev_nc_u32_e32 v4, 0x80, v4 +v_and_b32_e32 v4, 0xffff, v4 +v_lshlrev_b32_e32 v5, 3, v3 +v_sub_nc_u32_e32 v5, 0x12c, v5 +v_lshl_or_b32 v4, v5, 16, v4 +v_mbcnt_lo_u32_b32 v10, -1, 0 +v_mbcnt_hi_u32_b32 v10, -1, v10 +v_lshl_or_b32 v6, v10, 16, 0xa5a5 +v_sat_pk_u8_i16_e32 v6, v4 +v_lshrrev_b32_e32 v7, 16, v6 +v_and_b32_e32 v8, 0xffff, v6 +v_mov_b32_e32 v9, 0 +exp mrt0 v6, v7, v8, v9 done vm +s_endpgm diff --git a/runtime/test/shaders/w64_graphics.py b/runtime/test/shaders/w64_graphics.py index 649fc4630b..6f56084184 100644 --- a/runtime/test/shaders/w64_graphics.py +++ b/runtime/test/shaders/w64_graphics.py @@ -119,6 +119,27 @@ def _c_vote_stale(i): return (None, int(_x(i) == 0 and _y(i) == 0), 0, 0) +def _sat8(h): + """A signed 16-bit value saturated to an unsigned byte (v_sat_pk_u8_i16, ISA pseudocode).""" + h = h - 0x10000 if h & 0x8000 else h + return 0 if h <= 0 else min(h, 255) + + +def _sat_pk(i): + """s of the sat_pk fixtures: SRC0 lo = 8x - 128, hi = 300 - 8y, each half saturated to a byte.""" + return _sat8((300 - 8 * _y(i)) & 0xFFFF) << 8 | _sat8((8 * _x(i) - 128) & 0xFFFF) + + +def _a_sat_pk_hi(i): + x, y, s = _x(i), _y(i), _sat_pk(i) + p = 64 * y + x + return (p << 16 | s, ((0xFF00 - p) & 0xFFFF) << 16 | s, (p ^ 0x5555) << 16 | s, x << 24 | y << 16 | s) + + +def _c_sat_pk_hi(i): + return (None, None, _sat_pk(i), 0) # r0, r1 hold the lane id in the kept high half + + def _on(i): return int(_x(i) == 5 and _y(i) == 7) @@ -189,6 +210,7 @@ def _gs_a_ring(i): # gs_w64_* fixtur "ps_w64_a_uniform_vote": ("A", _a_uniform_vote, {"VKH_SGPRS": "0,0,0,0,9"}), "ps_w64_a_discard": ("A", _a_discard, {}), + "ps_w64_a_sat_pk_hi": ("A", _a_sat_pk_hi, {}), "ps_w64_b_waterfall": ("B", _b_waterfall, {}), "ps_w64_b_readfirstlane": ("B", _b_readfirstlane, {"VKH_SGPRS": "0,0,0,0,123"}), "ps_w64_b_readlane_uniform": ("B", _b_readlane_uniform, {"VKH_SGPRS": "0,0,0,0,123"}), @@ -202,6 +224,7 @@ def _gs_a_ring(i): # gs_w64_* fixtur "ps_w64_c_vote_else": ("C", _c_vote_else, {}), "ps_w64_c_scc_vote": ("C", _c_scc_vote, {}), "ps_w64_c_execz_sgpr": ("C", _c_execz_sgpr, {}), + "ps_w64_c_sat_pk_hi": ("C", _c_sat_pk_hi, {}), "vs_w64_a_skip": ("A", _vs_a_skip, VS_ENV), "vs_w64_b_waterfall": ("B", _b_waterfall, VS_ENV), "vs_w64_b_readlane_uniform": ("B", _vs_b_readlane_uniform, dict(VS_ENV, VKH_SGPRS=VS_ENV["VKH_SGPRS"] + ",123")), diff --git a/tests/semantic/fixtures/w64_sat_pk_hi.asm b/tests/semantic/fixtures/w64_sat_pk_hi.asm new file mode 100644 index 0000000000..1ad3bc4032 --- /dev/null +++ b/tests/semantic/fixtures/w64_sat_pk_hi.asm @@ -0,0 +1,23 @@ +; vs_w64_sat_pk_hi: hand-written wave64 vertex shader (llvm-mc -triple=amdgcn -mcpu=gfx1030 -show-encoding). +; v_sat_pk_u8_i16 writes the low half of v6, whose high half holds the lane id (0..63 in the wave), and the high half +; is read back afterwards (v7) and exported. SRC0's two signed halves come from the vertex index k (the global lane, +; the same however the wave is cut): lo = 8k - 128 (-128..376), hi = 300 - 8k (300..-204), so both halves saturate +; below 0 and above 255 across the lanes. +; param0 of lane k: v6 = lane_id << 16 | sat8(hi) << 8 | sat8(lo), v7 = lane_id, v1 = SRC0, 0. With all 64 lanes in one +; wave lane_id = k; cut into two waves of 32 it is k % 32, so the export differs for k >= 32 and the program is tier C +; (tools/wave64_halves.py must keep v6's old value as a source of the write). + s_mov_b64 s[10:11], exec ; encoding: [0x7e,0x04,0x8a,0xbe] + v_mbcnt_lo_u32_b32 v10, -1, 0 ; encoding: [0x0a,0x00,0x65,0xd7,0xc1,0x00,0x01,0x02] + v_mbcnt_hi_u32_b32 v10, -1, v10 ; encoding: [0x0a,0x00,0x66,0xd7,0xc1,0x14,0x02,0x02] + v_lshlrev_b32_e32 v2, 3, v0 ; encoding: [0x83,0x00,0x04,0x34] + v_subrev_nc_u32_e32 v3, 0x80, v2 ; encoding: [0xff,0x04,0x06,0x4e,0x80,0x00,0x00,0x00] + v_sub_nc_u32_e32 v4, 0x12c, v2 ; encoding: [0xff,0x04,0x08,0x4c,0x2c,0x01,0x00,0x00] + v_and_b32_e32 v3, 0xffff, v3 ; encoding: [0xff,0x06,0x06,0x36,0xff,0xff,0x00,0x00] + v_lshl_or_b32 v1, v4, 16, v3 ; encoding: [0x01,0x00,0x6f,0xd7,0x04,0x21,0x0d,0x04] + v_lshl_or_b32 v6, v10, 16, 0xa5a5 ; encoding: [0x06,0x00,0x6f,0xd7,0x0a,0x21,0xfd,0x03,0xa5,0xa5,0x00,0x00] + v_sat_pk_u8_i16_e32 v6, v1 ; encoding: [0x01,0xc5,0x0c,0x7e] + v_lshrrev_b32_e32 v7, 16, v6 ; encoding: [0x90,0x0c,0x0e,0x2c] + v_mov_b32_e32 v5, 0 ; encoding: [0x80,0x02,0x0a,0x7e] + exp pos0, v5, v5, v5, v5 done ; encoding: [0xcf,0x08,0x00,0xf8,0x05,0x05,0x05,0x05] + exp param0, v6, v7, v1, v5 ; encoding: [0x0f,0x02,0x00,0xf8,0x06,0x07,0x01,0x05] + s_endpgm ; encoding: [0x00,0x00,0x81,0xbf] diff --git a/tests/semantic/test_wave64_halves_sat_pk.py b/tests/semantic/test_wave64_halves_sat_pk.py new file mode 100644 index 0000000000..61627b5600 --- /dev/null +++ b/tests/semantic/test_wave64_halves_sat_pk.py @@ -0,0 +1,149 @@ +"""v_sat_pk_u8_i16 on the wave64 halves path keeps its destination's high half (tools/wave64_halves.py). + +The op saturates the two signed i16 halves of SRC0 to bytes and writes them to the low 16 bits of VDST. The +translator writes that result with write_lo16, which keeps the high half in the _e32, _e64 and DPP forms; under SDWA +dst_sel/dst_unused decide (a sub-dword dst_sel with UNUSED_PRESERVE keeps the rest, DWORD or UNUSED_PAD do not). The +halves analysis has to read the old destination wherever the translator keeps it: when it took the op for a full +32-bit write (it was in _FULL32_V16_PREFIXES) a lane-dependent high half lost its taint and the program passed as tier +A, though its export changes when the wave is cut in two. + +The program is the hand-written fixtures/w64_sat_pk_hi.asm, one line of it swapped for each encoding. It runs on the +strict wave interpreter as one wave of 64 lanes and as two waves of 32 (test_wave64_halves.run_cut); the expected +words come from the plain-Python sat8 reference below. No llvm-mc, GPU or game data. + + python -m unittest discover -s tests/semantic -p test_wave64_halves_sat_pk.py +""" +import os +import sys +import unittest + +HERE = os.path.dirname(os.path.abspath(__file__)) +sys.path.insert(0, HERE) +import support # noqa: E402,F401 (sets the tools path) +from support import translate_listing # noqa: E402 +import rdna2spirv # noqa: E402 +import wave64_halves # noqa: E402 +from test_wave64_halves import meta_of, out_param, run_cut, vs # noqa: E402 + +FIXTURE = os.path.join(HERE, "fixtures", "w64_sat_pk_hi.asm") +E32_LINE = "\tv_sat_pk_u8_i16_e32 v6, v1 ; encoding: [0x01,0xc5,0x0c,0x7e]\n" + +# The fixture's v_sat_pk_u8_i16 line in each encoding (text and bytes from llvm-mc -mcpu=gfx1030), and whether it keeps +# v6's high half. +FORMS = { + "e32": ("v_sat_pk_u8_i16_e32 v6, v1", "0x01,0xc5,0x0c,0x7e", True), + "e64": ("v_sat_pk_u8_i16_e64 v6, v1", "0x06,0x00,0xe2,0xd5,0x01,0x01,0x01,0x02", True), + "dpp": ("v_sat_pk_u8_i16_dpp v6, v1 quad_perm:[0,1,2,3] row_mask:0xf bank_mask:0xf bound_ctrl:1", + "0xfa,0xc4,0x0c,0x7e,0x01,0xe4,0x08,0xff", True), + "sdwa_word0_preserve": ("v_sat_pk_u8_i16_sdwa v6, v1 dst_sel:WORD_0 dst_unused:UNUSED_PRESERVE src0_sel:DWORD", + "0xf9,0xc4,0x0c,0x7e,0x01,0x14,0x06,0x00", True), + "sdwa_word0_pad": ("v_sat_pk_u8_i16_sdwa v6, v1 dst_sel:WORD_0 dst_unused:UNUSED_PAD src0_sel:DWORD", + "0xf9,0xc4,0x0c,0x7e,0x01,0x04,0x06,0x00", False), + "sdwa_dword_preserve": ("v_sat_pk_u8_i16_sdwa v6, v1 dst_sel:DWORD dst_unused:UNUSED_PRESERVE src0_sel:DWORD", + "0xf9,0xc4,0x0c,0x7e,0x01,0x16,0x06,0x00", False), +} + + +def sat8(h): + """A signed 16-bit value saturated to an unsigned byte.""" + h = h - 0x10000 if h & 0x8000 else h + return 0 if h <= 0 else min(h, 255) + + +def src0(k): + """SRC0 of vertex k in the fixture: hi = 300 - 8k, lo = 8k - 128, each as 16 bits.""" + return ((300 - 8 * k) & 0xFFFF) << 16 | ((8 * k - 128) & 0xFFFF) + + +def sat_pk(s): + return sat8(s >> 16) << 8 | sat8(s & 0xFFFF) + + +def program(form): + with open(FIXTURE, "rb") as f: + text = f.read().decode("utf-8") + assert text.count(E32_LINE) == 1, "fixture lost its v_sat_pk_u8_i16_e32 line" + line, enc, _ = FORMS[form] + return text.replace(E32_LINE, f"\t{line} ; encoding: [{enc}]\n").encode("utf-8") + + +def tier_and_runs(name, data): + asm = translate_listing(name, data) + w = rdna2spirv.translate.last_gen.w64g + tier, reasons = (w["tier"], w["reasons"]) if w else (None, []) + return tier, reasons, out_param(run_cut(asm, 64)), out_param(run_cut(asm, 32)) + + +class SatPkHighHalfOnHalvesPath(unittest.TestCase): + def test_reference_saturates_both_halves_both_ways(self): + # the fixture's lanes reach below 0 and above 255 in each half of SRC0, and the in-range values in between + lo = [(src0(k) & 0xFFFF) for k in range(64)] + hi = [(src0(k) >> 16) for k in range(64)] + for half in (lo, hi): + got = {sat8(h) for h in half} + self.assertIn(0, got) + self.assertIn(255, got) + self.assertTrue(any(0 < sat8(h) < 255 for h in half)) + self.assertTrue(any(h & 0x8000 for h in half)) # negative inputs + self.assertTrue(any(0xFF < h < 0x8000 for h in half)) # above 255 + + def test_kept_high_half_is_tier_c_and_differs_when_cut(self): + for form, (_, _, keeps) in FORMS.items(): + if not keeps: + continue + with self.subTest(form=form): + tier, reasons, whole, halves = tier_and_runs(f"vs_w64_sat_pk_hi_{form}", program(form)) + self.assertEqual(whole, [((k << 16) | sat_pk(src0(k)), k, src0(k), 0) for k in range(64)]) + self.assertEqual(halves, [((k % 32) << 16 | sat_pk(src0(k)), k % 32, src0(k), 0) + for k in range(64)]) + self.assertEqual(tier, "C", f"{form}: reasons={reasons}") + self.assertIn("lane-id", reasons, form) + + def test_full_sdwa_write_stays_tier_a(self): + for form, (_, _, keeps) in FORMS.items(): + if keeps: + continue + with self.subTest(form=form): + tier, reasons, whole, halves = tier_and_runs(f"vs_w64_sat_pk_full_{form}", program(form)) + want = [(sat_pk(src0(k)), 0, src0(k), 0) for k in range(64)] + self.assertEqual(whole, want) + self.assertEqual(halves, want) + self.assertEqual(tier, "A", f"{form}: reasons={reasons}") + + def test_kept_high_half_blocks_uniform_readlane(self): + # high half of v10 = vertex index, low half from a uniform SGPR: v10 is not uniform, so v_readlane_b32 of lane 0 + # is a cross-lane read, and the second half's lane 0 (global lane 32) gives another word + for form in ("e32", "e64"): + with self.subTest(form=form): + (tier, reasons, idioms), asm = meta_of(f"vs_w64_sat_pk_readlane_{form}", vs([ + "v_lshlrev_b32_e32 v10, 16, v0", + f"v_sat_pk_u8_i16_{form} v10, s4", + "v_readlane_b32 s6, v10, 0", + "v_mov_b32_e32 v6, s6", + ])) + self.assertEqual(tier, "C", f"reasons={reasons}, idioms={idioms}") + self.assertIn("cross-lane", reasons) + self.assertNotIn("readlane-uniform", idioms) + whole = [w[0] for w in out_param(run_cut(asm, 64))] + halves = [w[0] for w in out_param(run_cut(asm, 32))] + self.assertEqual(whole, [whole[0]] * 64) + self.assertEqual(halves, [whole[0]] * 32 + [whole[0] | 32 << 16] * 32) + + def test_operands_and_partial_write_per_encoding(self): + v6 = {"k": "vgpr", "r": 6, "n": 1} + v1 = {"k": "vgpr", "r": 1, "n": 1} + cases = [(line.split()[0], line.split(" ", 3)[3].split() if line.count(" ") > 2 else [], keeps) + for line, _, keeps in FORMS.values()] + cases.append(("v_sat_pk_u8_i16_sdwa", ["dst_sel:WORD_1", "dst_unused:UNUSED_PRESERVE", "src0_sel:DWORD"], + True)) + cases.append(("v_sat_pk_u8_i16_dpp", ["quad_perm:[1,0,3,2]", "row_mask:0xf", "bank_mask:0xf"], True)) + for op, mods, keeps in cases: + with self.subTest(op=op, mods=mods): + ins = {"op": op, "args": [v6, v1], "mods": mods} + _, srcs, _, _ = wave64_halves.operands(ins) + self.assertEqual(any(x is v6 for x in srcs), keeps) + self.assertEqual(wave64_halves.partial_write(ins, wave64_halves.base_op(op)), keeps) + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/test_wave64_halves_fixtures.py b/tests/test_wave64_halves_fixtures.py index 40b04f5789..f9962e48c9 100644 --- a/tests/test_wave64_halves_fixtures.py +++ b/tests/test_wave64_halves_fixtures.py @@ -38,6 +38,7 @@ "ps_w64_a_uniform_vote": ("A", [], []), "ps_w64_a_discard": ("A", [], []), + "ps_w64_a_sat_pk_hi": ("A", [], []), "ps_w64_b_waterfall": ("B", [], ["waterfall"]), "ps_w64_b_readfirstlane": ("B", [], ["readfirstlane-uniform"]), "ps_w64_b_readlane_uniform": ("B", [], ["readlane-uniform"]), @@ -51,6 +52,7 @@ "ps_w64_c_vote_else": ("C", ["unsafe-vote-branch"], None), "ps_w64_c_scc_vote": ("C", ["cross-lane"], None), "ps_w64_c_execz_sgpr": ("C", ["stale-scalar", "unsafe-execz-branch"], None), + "ps_w64_c_sat_pk_hi": ("C", ["lane-id"], None), "vs_w64_a_skip": ("A", [], []), "vs_w64_b_waterfall": ("B", [], ["waterfall"]), "vs_w64_b_readlane_uniform": ("B", [], ["readfirstlane-uniform", "readlane-uniform"]), @@ -173,6 +175,14 @@ def test_expected_words_of_the_gpu_fixtures(): assert w64_graphics._vs_a_skip(6 + 64 * 2) == (1006, 0, 2000 + 2 + 9, 3) assert w64_graphics._vs_b_readlane_uniform(70) == (6 + 123, 200, 6 + 123, 277) assert w64_graphics._vs_c_lane_id(700) == (None, None, 700, 0) + # sat_pk: x = 0, y = 0: lo = -128 -> 0, hi = 300 -> 255; x = 20, y = 40: lo = 32, hi = -20 -> 0; + # x = 63, y = 5: lo = 376 -> 255, hi = 260 -> 255; x = 17, y = 31: lo = 8, hi = 52 + assert w64_graphics._a_sat_pk_hi(0) == (0x0000FF00, 0xFF00FF00, 0x5555FF00, 0x0000FF00) + p = 64 * 40 + 20 + assert w64_graphics._a_sat_pk_hi(p) == (p << 16 | 0x20, (0xFF00 - p) << 16 | 0x20, (p ^ 0x5555) << 16 | 0x20, + 20 << 24 | 40 << 16 | 0x20) + assert w64_graphics._a_sat_pk_hi(63 + 64 * 5)[0] == (63 + 64 * 5) << 16 | 0xFFFF + assert w64_graphics._c_sat_pk_hi(17 + 64 * 31) == (None, None, 52 << 8 | 8, 0) def test_vs_probe_and_radv_perftest(monkeypatch): diff --git a/tools/wave64_halves.py b/tools/wave64_halves.py index 9b5305344b..f083d0080d 100644 --- a/tools/wave64_halves.py +++ b/tools/wave64_halves.py @@ -124,6 +124,8 @@ def partial_write(ins, op): name = op.split("_e32")[0].split("_e64")[0].split("_dpp")[0] if name.startswith(("v_pk_", "v_cvt_pk", "v_sad_")): return False + if name == "v_sat_pk_u8_i16": # two u8 results packed into the low half: a 16-bit result whose first type is u8 + return True first = next((p for p in name.split("_")[1:] if _TYPE.match(p)), None) if not (first and _TYPE16.match(first)): return False @@ -136,9 +138,11 @@ def partial_write(ins, op): "v_mul_lo_u16", "v_max_u16", "v_min_u16", "v_max_i16", "v_min_i16", "v_and_b16", "v_or_b16", "v_xor_b16", "v_add_i16", "v_sub_i16", ) -# 16-bit-suffixed or -prefixed mnemonics that write the full 32-bit destination register. +# 16-bit-suffixed or -prefixed mnemonics that write the full 32-bit destination register. Not v_sat_pk_u8_i16: its +# two saturated bytes are a 16-bit result, and rdna2spirv writes them with write_lo16, which keeps the high half in +# the _e32, _e64 and DPP forms (design.md, "v_sat_pk_u8_i16"), so it falls to the _i16 rule below. _FULL32_V16_PREFIXES = ( - "v_pk_", "v_pack_", "v_cvt_pk", "v_sat_pk", "v_sad_", "v_msad_", "v_qsad_", "v_mqsad_", + "v_pk_", "v_pack_", "v_cvt_pk", "v_sad_", "v_msad_", "v_qsad_", "v_mqsad_", "v_cmp", "v_cmpx", "v_interp_p1", "v_dot", "v_cvt_f32_", "v_cvt_u32_", "v_cvt_i32_", "v_cvt_f64_", "v_mad_i32_", "v_mad_u32_", ) From 232a2ce6da0cc4f4b62631f4085fb3cefa64e879 Mon Sep 17 00:00:00 2001 From: milk4sale <53350507+milk4sale@users.noreply.github.com> Date: Tue, 24 Nov 2026 09:10:49 -0600 Subject: [PATCH 3/3] design.md: the v_sat_pk_u8_i16 high-half keep is the translator's choice (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 #508). Co-Authored-By: Claude Opus 5.5 --- docs/design.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/design.md b/docs/design.md index decd25a097..29715494d3 100644 --- a/docs/design.md +++ b/docs/design.md @@ -434,7 +434,7 @@ How the generated SPIR-V expects the runtime to work: | `v_mqsad_u32_u8` | Four masked byte-SADs with full 32-bit accumulators: D[k] = S2[k] + `v_msad_u8` of the 32-bit window S0[8k+31:8k] of the 64-bit S0 against S1, wrapping at 32 bits (S2 and D are four VGPRs). Every source is read before the first write; `clamp` raises `Unsupported`. Source: RDNA ISA pseudocode. CPU-checked by `tests/semantic/test_alu_mqsad_u32.py`. **Hardware-verified on RDNA2** (`gfx1035`, wave32 and wave64 via `tools/int_hw_probe.py --oracle both`, SHADER-137). Measured on native gfx1036 (AMD Radeon Graphics, RDNA2) through tools/hw_oracle across 64-bit sliding window and masked byte values against the 32-bit accumulators: bit-exact with the ISA reference. | | `s_sext_i32_i8`, `s_sext_i32_i16`, `s_flbit_i32` | The first two sign-extend the low byte or the low half of S0 to 32 bits (`OpBitFieldSExtract`). `s_flbit_i32` counts how many bits in a row from the MSB equal the sign bit, and gives −1 when all bits are the same (0 and −1); it is the scalar form of `v_ffbh_i32` and goes through the same code. None of the three writes SCC. Source: RDNA2 ISA SOP1 opcodes 23, 25 and 26. CPU-checked by `tests/semantic/test_salu_sext_flbit.py` (#549): edge words with SCC starting at 0 and at 1, and 30 seeded values with long runs of sign bits. Not in the translated corpus, not GPU-checked. **Hardware-verified on RDNA2** (`gfx1035`, wave32 and wave64 via `tools/int_hw_probe.py --oracle both`, SHADER-137, SCC kept). Measured on native gfx1036 (AMD Radeon Graphics, RDNA2) through tools/hw_oracle and tools/salu_diff.py (500 programs, --oracle translator,hw): 500 pass, 0 mismatches against the reference; confirmed s_flbit_i32 gives 0xFFFFFFFF for 0 and -1. | | `v_lerp_u8` | Per byte i: `D.b[i] = (S0.b[i] + S1.b[i] + (S2.b[i] & 1)) >> 1`, an unsigned average where bit 0 of S2's byte decides whether 0.5 rounds up. The translator uses the per-byte identity `x + y = 2*(x & y) + (x ^ y)`. It clears each byte's low bit before shifting, then adds the rounding bit only to an odd sum. Each result fits in one byte, so the packed additions do not carry across bytes. The 9-bit sum shifted right by 1 fits back in its byte, so nothing carries between bytes. `clamp` raises `Unsupported` (the ISA doesn't define it for this op). Source: RDNA2 ISA VOP3 opcode 333. CPU-checked by `tests/semantic/test_alu_lerp_u8.py` (#577): a 9×9×5 grid of edge and rounding words, 40 seeded cases, an SGPR source with an inline S2, `clamp` rejected. The synthetic `tests/semantic/fixtures/lerp_packed_bytes.asm` gives exact words across a 64x64 draw on an RTX 3070 (NVIDIA 591.86). Not in the translated corpus, not GPU-checked. **Hardware-verified on RDNA2** (`gfx1035`, wave32 and wave64 via `tools/int_hw_probe.py --oracle both`, SHADER-137). Measured on native gfx1036 (AMD Radeon Graphics, RDNA2) through tools/hw_oracle across byte edge values and rounding bit variations: 0 mismatches against the ISA reference. | -| `v_sat_pk_u8_i16` | `D[15:0] = {sat_u8(S0[31:16].i16), sat_u8(S0[15:0].i16)}`: each signed i16 lane of S0 saturates to an unsigned byte and packs into the low 16 bits of D (AMD machine-readable ISA semantics, VOP1 0x62 / VOP3 0x1E2). The 16-bit result keeps the destination's high half, the same partial write as `v_cvt_f16_f32` (Mesa ACO `instr_is_16bit`); under `_sdwa` `dst_sel`/`dst_unused` decide instead. Source `neg`/`abs`, `clamp` and output modifiers raise `Unsupported` rather than being guessed. CPU-checked by `tests/semantic/test_alu_sat_pk_u8_i16.py` (hand-written `tests/semantic/fixtures/sat_pk_u8_i16.asm`, lane edge values, SGPR/literal/inline sources, `_sdwa` source and destination selects, refused modifiers) against a plain-Python reference. Not in the 26,503-shader corpus (listed in `docs/fssm_crosscheck.md`), not GPU-verified. | +| `v_sat_pk_u8_i16` | `D[15:0] = {sat_u8(S0[31:16].i16), sat_u8(S0[15:0].i16)}`: each signed i16 lane of S0 saturates to an unsigned byte and packs into the low 16 bits of D (AMD machine-readable ISA semantics, VOP1 0x62 / VOP3 0x1E2). The translator writes the 16-bit result with `write_lo16`, which keeps the destination's high half, the same partial write as `v_cvt_f16_f32`; under `_sdwa` `dst_sel`/`dst_unused` decide instead. Keeping the high half is the translator's choice, **not measured** on RDNA2: neither Mesa ACO nor LLVM says what GFX10 does with it (next row, **References**). Source `neg`/`abs`, `clamp` and output modifiers raise `Unsupported` rather than being guessed. CPU-checked by `tests/semantic/test_alu_sat_pk_u8_i16.py` (hand-written `tests/semantic/fixtures/sat_pk_u8_i16.asm`, lane edge values, SGPR/literal/inline sources, `_sdwa` source and destination selects, refused modifiers) against a plain-Python reference. Not in the 26,503-shader corpus (listed in `docs/fssm_crosscheck.md`), not GPU-verified. | | `v_sat_pk_u8_i16` on the wave64 halves path (SHADER-SATPK-HALVES) | `tools/wave64_halves.py` reads the old destination of `v_sat_pk_u8_i16` wherever the translator keeps its high half: the `_e32`, `_e64` and DPP forms (the 16-bit rule of `_preserves_dst`, and `partial_write`, which had taken the op's first type, `u8`, for an 8-bit result), and SDWA with a `BYTE`/`WORD` `dst_sel` and `UNUSED_PRESERVE`; SDWA with `dst_sel:DWORD`, `UNUSED_PAD` or `UNUSED_SEXT` writes the whole register. The op used to be in `_FULL32_V16_PREFIXES`, so a high half that depends on the lane lost its taint and the shader passed as tier A (or tier B through `readlane-uniform`), though its export changes when the wave is cut in two. The rule follows what the translator emits (`write_lo16`, row above); if RDNA2 turns out to zero the high half, calling the op a partial write stays safe and costs tier A at most. **References:** neither compiler says the high half is kept on GFX10. Mesa ACO's `instr_is_16bit` is false for the op before GFX11: it is not in the function's list, and `can_use_opsel` falls through to `get_gfx11_true16_mask`, which gives it 0x8 (the destination bit, as for `v_cvt_f16_f32`) for GFX11 and later only (`src/amd/compiler/aco_ir.cpp`, Mesa `99abc485`). LLVM defines it with `VOP1Inst_t16` (`VOP1Instructions.td`), which on GFX10 is the plain VOP1 instruction with an i16 result (`VOP_I16_I32`; the `_t16`/`_fake16` forms are GFX11 and later), and `GCNSubtarget::zeroesHigh16BitsOfDest` does not list it, so LLVM assumes neither a kept nor a zeroed high half (`llvm/lib/Target/AMDGPU`, LLVM `e2b0a1e1`). What the RDNA2 instruction does with the high half is **not measured**. CPU-checked by `tests/semantic/test_wave64_halves_sat_pk.py` (the hand-written `fixtures/w64_sat_pk_hi.asm`, whose high half is the lane id, run on the strict wave interpreter as 64 lanes and as two waves of 32 in every encoding, plus the `readlane-uniform` case) and by `tests/test_wave64_halves_fixtures.py` (`ps_w64_c_sat_pk_hi` must be tier C, `lane-id`). GPU: `ps_w64_a_sat_pk_hi` (tier A: the four encodings that keep the high half, a different high half per pixel, both halves of SRC0 saturating below 0 and above 255 across the target) gives its derived words on an NVIDIA RTX 4070 Ti (driver 620.70.06, Linux). NVIDIA runs the translated SPIR-V, so this confirms the translator's semantics, not the AMD instruction: the expected low half is the ISA pseudocode's SAT8 packing, the expected high half is the translator's rule. `ps_w64_c_sat_pk_hi` is a `SKIP` there (32 is the only fragment subgroup size; tier C needs a device that can require both 64 and 32, or `W64G_REF`). | | Scalar select and pack (`s_cmov_b32`, `s_cmovk_i32`, `s_cmov_b64`, `s_pack_ll/lh/hh_b32_b16`) | `s_cmov_b32` / `s_cmovk_i32`: D = SCC ? S0 : D (`s_cmovk_i32`'s S0 is the 16-bit immediate, sign-extended); SCC is read, not written; the destination must be an SGPR (a lane-mask destination stays `Unsupported`, #210). `s_cmov_b64`: D[63:0] = SCC ? S0[63:0] : D[63:0] on plain 64-bit data pairs (SGPR pair, immediate or `null`), SCC read and not written; a pair that is or has been a lane mask (EXEC, VCC or a mask SGPR) stays `Unsupported` (#210) — conservatively, because a CFG join can drop a register that is a mask on only one path — while 0/-1 EITHER constants are data; selecting a per-lane bit as data would silently return a bool instead of the wave's mask. D[63:0] on plain 64-bit data pairs (SGPR pair, immediate or `null`), SCC read and not written; a pair that is or has been a lane mask stays `Unsupported` (#210). D[63:0], SCC read and not written, on plain 64-bit data pairs (SGPR pair, immediate or `null`); any dword that is or has been a lane mask (EXEC, VCC or a mask SGPR) makes the pair `Unsupported` — conservatively, because a CFG join can drop a register that is a mask on only one path — while 0/-1 EITHER constants are data; selecting a per-lane bit as data would silently return a bool instead of the wave's mask (#210). `s_pack_*`: the low half of D from S0, the high half from S1, the letters naming the half of each (l = 15:0, h = 31:16), S0 first; GFX10 has no `s_pack_hl`; SCC not written. Source: RDNA2 ISA SOP1/SOP2/SOPK and LLVM AMDGPU `SOPInstructions.td` (`S_CMOV_B64 : RDNA2 ISA SOP1/SOP2/SOPK. CPU-checked by `tests/semantic/test_salu_cmov.py` and `test_salu_pack.py`; the translated shaders were run on NVIDIA by reviewers (`s_cmov*`: RDNA2 ISA SOP1/SOP2/SOPK and LLVM AMDGPU [SOPInstructions.td](https://github.com/llvm/llvm-project/blob/2bfedf3338162e2c841a73cd7cd9e98d00add608/llvm/lib/Target/AMDGPU/SOPInstructions.td) (`S_CMOV_B64 : SOP1_64` under `Uses = [SCC]`). CPU-checked by `tests/semantic/test_salu_cmov.py`, `tests/semantic/test_salu_cmov_b64.py` and `test_salu_pack.py`; no corpus count; **not GPU-verified, no corpus count**; the translated shaders were run on NVIDIA by reviewers (`s_cmov_b32/k_i32`: **no corpus count**; Hardware: on native `gfx1036` (Ryzen 9 9950X3D iGPU, Windows 11, driver 26.10.1) `tools/int_hw_probe.py --oracle both --wave both` matches the translator in wave32 and wave64 for all five `salu_cmov_pack` cases and for the 14 `s_cmov_b64` cases (`--group salu_cmov_b64`: GPU-checked for `s_cmov_b32` (`cs_w32_salu64.asm`, SCC = 1 selects S0) and `s_pack_lh_b32_b16` (`cs_w32_int_ext.asm`) on the Radeon iGPU of a Ryzen 9 9950X3D (RDNA2, `gfx1036`, driver 26.10.1) and an RTX 5080 (driver 616.92); `s_cmovk_i32`, `s_cmov_b64` and the other `s_pack_*` forms have no GPU test on main; the translated shaders were also run on NVIDIA by reviewers (`s_cmov_b32/k_i32`: RTX 5090, RTX 4050 and Radeon 740M in #613; `s_pack_*`: RTX 5090 in #449). **Hardware-verified on RDNA2** (`gfx1035`, wave32 and wave64 via `tools/int_hw_probe.py --oracle both`, SHADER-137, SCC kept), and on native gfx1036 (AMD Radeon Graphics, Windows 11, driver 26.10.1) through `tools/hw_oracle`: all five `salu_cmov_pack` cases and the 14 `s_cmov_b64` cases match the translator in wave32 and wave64 (SCC 0 and 1; an SGPR pair, the inline constants -1/-16/64, the literal 0x89abcdef, `null` and the destination itself; SCC = 1 takes S0, 0 keeps D, SCC is never written), `s_cmov_b32` matches under SCC=0 and SCC=1, and `s_pack_ll/lh/hh_b32_b16` is bit-exact on 500 random programs with 0 mismatches (`--oracle translator,hw`). **Hardware-verified on RDNA2** (`gfx1035`, wave32 and wave64 via `tools/int_hw_probe.py --oracle both`, SHADER-137, SCC kept). Also **measured on RDNA2 gfx1036** (Radeon iGPU of a Ryzen 9 9950X3D, Windows 11, driver 26.10.1, `tools/hw_oracle` HIP launcher, rocm-sdk-core clang): `tools/int_hw_probe.py --oracle both --wave both` gives the same words as the translator for all five `salu_cmov_pack` cases in wave32 and wave64, and the 14 `s_cmov_b64` cases of the `salu_cmov_b64` group (`python -I tools/int_hw_probe.py --oracle both --wave both --group salu_cmov_b64`; SCC 0 and 1; source an SGPR pair, the inline constants -1, -16 and 64, the literal 0x89abcdef, `null`, and the destination itself; SCC preset and read back after): SCC preset and read back after) match the translator in wave32 and wave64: SCC = 1 takes S0 (a literal zero-extended, an inline constant sign-extended, `null` gives 0), SCC = 0 keeps D, SCC is never written. Also on `gfx1035` via `int_hw_probe` (SHADER-137, SCC kept), and on `gfx1036` and an RTX 5080 through the fixtures `cs_w32_salu64.asm` (`s_cmov_b32`) and `cs_w32_int_ext.asm` (`s_pack_lh_b32_b16`). `tools/hw_oracle` + `tools/salu_diff.py` verify `s_cmov_b32` under SCC 0/1 and `s_pack_ll/lh/hh_b32_b16` bit-exact on 500 random programs with 0 mismatches. Reviewers ran `s_cmov*` on an RTX 5090, an RTX 4050 and a Radeon 740M (#613) and `s_pack_*` on an RTX 5090 (#449). Measured on native gfx1036 (AMD Radeon Graphics, RDNA2) through tools/hw_oracle and tools/salu_diff.py: s_cmov_b32 verified under SCC=0 and SCC=1 (preserving destination and updating respectively); s_pack_ll/lh/hh_b32_b16 verified bit-exact on 500 random programs with 0 mismatches (--oracle translator,hw). **Not measured on RDNA2.** | | `v_bfi_b32` | `(S0 & S1) \| (~S0 & S2)` (RDNA2 ISA, VOP3 opcode 330). A literal or known same-block SGPR mask of zero or all ones selects S2 or S1 directly. A contiguous mask uses `OpBitFieldInsert` with S1 shifted down by the field offset, since RDNA2 reads the bits in place. For sparse constant masks the translator precomputes the complement. Runtime masks use the full bitwise expression. Checked by the hand-written `bfi_known_mask.asm` and `tests/semantic/test_bfi_known_mask.py`. |