Skip to content

rdna2spirv: support VOP3P neg_lo/neg_hi on v_dot4_i32_i8 - #7293

Open
DenRakEiw wants to merge 2 commits into
ItsNotRexed:mainfrom
DenRakEiw:feat/rdna2-dot4-i8-neg
Open

DenRakEiw wants to merge 2 commits into
ItsNotRexed:mainfrom
DenRakEiw:feat/rdna2-dot4-i8-neg

Conversation

@DenRakEiw

Copy link
Copy Markdown
Contributor

Support the measured RDNA2 VOP3P neg_lo / neg_hi modifiers on v_dot4_i32_i8 in tools/rdna2spirv.py.

Claim: #7049 (comment)

Summary

  • tools/rdna2spirv.py: Lower neg_lo / neg_hi on v_dot4_i32_i8 following the measured RDNA2 (gfx1036) hardware rule documented in docs/design.md ("Signed-byte dot products") and tests/semantic/test_alu_dot4_i8.py:
    • S0 and S1: neg_lo[k] XORs bit 15 (0x00008000), neg_hi[k] XORs bit 31 (0x80000000).
    • S2 (addend): neg_hi[2] clears bit 31 (& 0x7FFFFFFF) and neg_lo[2] then flips bit 31 (^ 0x80000000), emitted as a single OpBitwiseOr with 0x80000000 when both are set.
    • v_dot4c_i32_i8 (VOP2) and malformed neg_lo / neg_hi fields remain refused with Unsupported.
  • tests/semantic/test_dot4_i8_neg.py: New semantic, binary-decode, and spirv-val test suite covering:
    • All 9 hardware-measured gfx1036 cases (MEASURED_NEG)
    • Exhaustive 64-mask neg_lo[0..2] x neg_hi[0..2] sweep for positive and negative addends
    • Edge byte values (0x80, 0x00, 0x7F, 0xFF) and edge addends (INT32_MIN, INT32_MAX, 0, -1)
    • clamp saturation combined with neg_lo/neg_hi on S0/S1/S2
    • SGPR and 32-bit literal operands
    • Refusal of v_dot4c_i32_i8 neg modifiers and malformed masks
    • llvm-mc -arch=amdgcn -mcpu=gfx1030 -show-encoding -> rdna2_decode round-trip
    • spirv-as + spirv-val --target-env vulkan1.2 validation
  • tests/semantic/test_alu_dot4_i8.py & docs/design.md: Updated the existing neg_lo/neg_hi refusal test to verify MEASURED_NEG against the translator output and updated the design doc row.

Verification (Windows 11 + NVIDIA GeForce RTX 5090)

  • python -I -m unittest discover -s tests/semantic -p test_dot4_i8_neg.py -v (with LLVM_MC="wsl -d Ubuntu-24.04 -e /usr/lib/llvm-21/bin/llvm-mc" and Vulkan SDK 1.4.363.0 spirv-as/spirv-val): 8/8 passed (OK)
  • python -I -m unittest discover -s tests/semantic -p "test_*dot*.py" -v: 90/90 passed (OK)
  • python -I -m compileall -q tools tests: clean
  • git diff --check: clean

AI-assisted (Gemini 4 Neon).

@TillusStark26

Copy link
Copy Markdown

Tested aa85e83b1 on Windows 11 (Python 3.14.7, Vulkan SDK 1.4.363.1 spirv-as/spirv-val, CPU only, synthetic fixtures, no game files, AI-assisted): pass.

  • python -I -m unittest discover -s tests/semantic -p test_alu_dot4_i8.py: 12 tests OK.
  • python -I -m unittest discover -s tests/semantic -p test_dot4_i8_neg.py: 8 tests OK.
  • Negative control (PR test against main 115f3b2 tools/rdna2spirv.py): fails as expected (FAILED (failures=1, errors=62)).

@senyurah

Copy link
Copy Markdown

Tested aa85e83b1 merged onto main at 042c5f378 on Windows 11 (26200), RTX 4060 Ti, NVIDIA 621.24, Vulkan 1.4.357, subgroup 32. vk_harness was built from the unchanged runtime sources with MSYS2 UCRT64 GCC 15.2.0 and CMake 4.4.5.

I translated the nine MEASURED_NEG cases as synthetic fragment shaders, assembled them using tools/spirv_asm.py, and drew each with vs_tri.spv. All nine returned the expected bit pattern in all four channels for all 4,096 pixels. The two semantic test files yielded 18 passes and two tool-availability skips. llvm-mc and spirv-val were unavailable, so this does not cover binary decode or SPIR-V validation. No game files used. AI-assisted (Codex).

@ruvenesh

Copy link
Copy Markdown

Tested aa85e83b (CPU only, no GPU): Ubuntu 24.04, Python 3.12.3, llvm-mc 23.1.6 (AMDGPU), spirv-as/spirv-val from the Vulkan SDK tools. AI-assisted (Claude Code).

  • python -I -m unittest discover -s tests/semantic -p test_dot4_i8_neg.py: 8/8 pass, including the llvm-mc gfx1030 round trip and spirv-val --target-env vulkan1.2.
  • test_alu_dot4_i8.py: 12/12 pass.
  • Read the lowering against the docs/design.md row: the S0/S1 bit 15 / bit 31 XORs and the S2 clear-then-flip (neg_hi and, neg_lo xor; both set = OR 0x80000000) match the nine measured gfx1036 sets.

One question, not a blocker: neg_lo/neg_hi combined with a non-default op_sel / op_sel_hi. The XOR is applied to the whole source dword before the op_sel half is picked, and test_dot4_i8_neg.py never sets op_sel (all nine MEASURED_NEG words use the default selects). On VOP3P the neg modifiers belong to the lo/hi operand after selection, so neg_lo:[1,0,0] op_sel:[1,0,0] may not flip the half the dot product reads. If that combination is not measured, either refuse it (Unsupported, like the S2 half-select) or say in the row that it is unmeasured. Happy to run it on a gfx1036 if someone has the hw_oracle setup.

Minor: v_dot4c_i32_i8 now refuses an all-zero neg_lo:[0,0,0] too (previously accepted). VOP2 cannot carry it, so this is fine, only a behaviour change.

I also ran the whole tests/semantic suite on this branch (5,897 tests): 51 failures, 3 skipped, on a box whose environment I have not checked against main yet; I will follow up with the main count so those are not attributed to this PR.

@ruvenesh

Copy link
Copy Markdown

Follow-up to my check above: the same 5,889-test tests/semantic run on main (8575d36e) also has 51 failures and 3 skipped (this PR: 5,897 tests, 51 failures, 3 skipped), so the 51 are not from this PR (they come from this box's tool versions; I have not triaged them). The 8 extra tests are this PR's own, all passing. AI-assisted (Claude Code).

@javdc

javdc commented Nov 23, 2026

Copy link
Copy Markdown

Tested aa85e83 on Steam Deck LCD (Jupiter), AMD Custom GPU 0405 "Van Gogh", RADV Mesa 26.2.0-devel (git-5c65d4fc31), Vulkan 1.4.350, SteamOS 3.9.5 (holo), kernel 7.2.7-valve2-2-neptune-72-g31504582a073: pass

  • pytest tests/semantic/test_dot4_i8_neg.py tests/semantic/test_alu_dot4_i8.py -q — 20 passed, 104 subtests passed
  • python3 -I scripts/run_gpu_tests.py --build /home/deck/opencode/Leonida/runtime/build — 217/239 passed, 16 skipped, 0 failed (expected skips)

AI-assisted (OpenCode).

@lucasgillet2009-lab lucasgillet2009-lab left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The arithmetic passed independent CPU and GPU checks at aa85e83b1. I’m requesting one change before approval: please reject duplicate neg_lo or neg_hi fields in a textual listing before emitting SPIR-V.

Reproduction

With v1 = 0x04030201, v2 = 0x08070605, and v3 = 100, try each instruction separately:

v_dot4_i32_i8 v8, v1, v2, v3 neg_lo:[1,0,0] neg_lo:[0,1,0]
v_dot4_i32_i8 v8, v1, v2, v3 neg_hi:[1,0,0] neg_hi:[0,1,0]

LLVM AMDGPU 23.1.6 rejects both lines for gfx1030. The translator on main (86a7807dd) also rejects them. On this PR, bits() takes the first matching field and silently ignores the second. The translated shaders return 0xfffffdaa and 0xfffffcaa, respectively. Running rdna2spirv.py end to end confirms the impact: both modules pass spirv-val and appear in index.json alongside the valid control.

Could you check that each mask field occurs at most once, validate its three binary entries before converting them, and raise Unsupported for malformed fields? I tried that change in a separate local checkout. A regression test checks the exception, and a CLI test checks that only the valid control appears in the index and has a generated .spv. Both tests passed with the proposed change, as did all 90 dot-product tests.

Validation

  • The PR passed all 8 new tests and 90/90 dot-product tests. The original suite on main passed 82/82. Applying the 8 new tests to main reproduced the missing modifier support.
  • The CPU interpreter matched an independent integer reference for 1,408/1,408 output words.
  • On an AMD Radeon RX 7600, all 8,192/8,192 GPU readbacks matched the reference: 64 masks × 64 operand triples × clamp off/on. This included 256 positive-overflow and 252 negative-overflow comparisons. All eight SPIR-V modules passed spirv-val. These were Vulkan executions of the translated shaders on Windows 11 25H2, AMD driver 26.11.2 (LLPC), Vulkan 1.4.359.

Open RDNA2 question

What does native RDNA2 produce when op_sel_hi broadcasts the low half of S0 and neg_lo is set? One discriminating encoding is 08 40 16 cc 01 05 0e 34, with S0=0, S1=0x01000100, and S2=0. The PR produces −256 by flipping the sign bit before duplication. Flipping it only for the low pair after selection would give −128. The RX 7600 runs the translation on RDNA3, so it cannot settle which result native RDNA2 gives. This is an open measurement question; the duplicate-mask reproduction above is the reason for requesting changes.

Testing was assisted by Codex and used synthetic shaders and data throughout.

@carlos-seiffert

Copy link
Copy Markdown

Tested aa85e83 (merged onto main 86a7807) on NVIDIA GeForce RTX 2060 (Turing), NVIDIA 621.24, Vulkan 1.4.357, Windows 11 (10.0.26300), subgroup size 32: pass (no regression)

What I ran: python -I scripts/run_gpu_tests.py with the Vulkan tools built from 86a7807 (llvm-mingw + Ninja, -DLEONIDA_BUILD_WINDOWS_RUNTIME=OFF, Vulkan-Headers 6802bb47), llvm-mc 23.1.6 and SPIRV-Tools from conda-forge.

main 86a7807 this PR on main
full GPU suite 191/240 passed, 48 skipped, 1 fail 191/240 passed, 48 skipped, 1 fail (same as main)

No GPU test changed status against main. This PR adds no GPU fixture of its own, so the run checks that the translator change breaks none of the existing GPU tests; it does not exercise the new rule directly.

Unit tests changed by the PR: tests/semantic/test_alu_dot4_i8.py, test_dot4_i8_neg.py: OK.

The one failure on both sides is ms_wave_info_s3, which fails on main on this GPU (10/10 runs) and is fixed by #7136 / #5741 / #5855; it is unrelated to this PR.

AI-assisted run (Claude Code); the numbers are from my machine.

@Sangonapou

Copy link
Copy Markdown

Tested aa85e83 on NVIDIA GeForce RTX 3080 Ti Laptop (Ampere, subgroup 32), NVIDIA 621.24, Vulkan 1.4.357, Windows 11 Pro (build 26300): pass, including a GPU readback

Runtime built with scripts\build_runtime.ps1 (MSVC 19.51); llvm-mc 23.1.6 (conda-forge), SPIRV-Tools 2026.4, Vulkan SDK 1.4.363.0.

GPU readback. I ran v_dot4_i32_i8 v8, v0, v1, v2 <mods> through tools/vk_fuzz_run.run_programs (one lane of a wave32 compute dispatch, vk_compute_test), with the operands seeded by v_mov_b32:

  • the 9 MEASURED_NEG gfx1036 cases: 9/9 match the measured words;
  • every neg_lo x neg_hi mask (64), with and without clamp, over 6 operand sets (including 0x80/0x7F/0xFF bytes and INT32_MIN/INT32_MAX/-1 addends), compared with ref_dot4_i8_neg: 768/768 match.

On the base 8575d36e the same programs give 1/9 and 12/768: only the forms without neg modifiers translate, the rest are refused.

Two local changes were needed to run vk_fuzz_run here, both outside this PR and not committed: it assembles for gfx1013, which has no v_dot4_i32_i8, so I loaded it with gfx1030 (the target your tests use). And vk_compute_test now dumps a 64-thread group for wave32, which fails its exact-size check (4 * words * wave), so I let it read the first wave. Without the second change, every program comes back as vk_compute_test exit 0 on main too, a trivial v_add_nc_u32 included.

Suite. run_gpu_tests.py on base and branch: native 190/239 passed, 48 skipped; LEONIDA_W64_EMULATE=1 221/239 passed, 17 skipped; 245/245 shaders translated and pass spirv-val. Identical on both. The one failure, ms_wave_info_s3, is a known failure on main.

CPU. tests/semantic/test_dot4_i8_neg.py 8 OK, test_alu_dot4_i8.py 12 OK.

@DenRakEiw

Copy link
Copy Markdown
Contributor Author

@lucasgillet2009-lab Thanks for the thorough CPU/GPU verification and catch! Addressed in 24aa3a428:

  • Updated bits() in tools/rdna2spirv.py for v_dot4_i32_i8 to reject duplicate neg_lo / neg_hi / op_sel / op_sel_hi fields and validate the bracketed 3-entry binary mask before int() conversion, raising Unsupported(f"{op} neg") or Unsupported(f"{op} op_sel").
  • Added regression cases in tests/semantic/test_dot4_i8_neg.py covering duplicate neg_lo/neg_hi/op_sel/op_sel_hi and malformed mask entries (non-integer / wrong length).
  • Re-ran python -I -m unittest discover -s tests/semantic -p "test_*dot*.py" -v (with llvm-mc 21 and spirv-val 1.4.363.0): 90/90 OK (0 skipped), plus compileall and git diff --check.

AI-assisted (Gemini 4 Neon).

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.

8 participants