Skip to content

Translator: saturate RDNA2 float-to-int conversions - #300

Merged
ItsNotRexed merged 13 commits into
ItsNotRexed:mainfrom
FreddyxS:fix/rdna2-float-to-int-saturation
Nov 21, 2026
Merged

ItsNotRexed merged 13 commits into
ItsNotRexed:mainfrom
FreddyxS:fix/rdna2-float-to-int-saturation

Conversation

@FreddyxS

Copy link
Copy Markdown
Contributor

Claimed in #156. AI-assisted (Codex).

What and why

v_cvt_u32_f32 and v_cvt_i32_f32 currently emit a raw SPIR-V conversion, whose result is undefined outside the destination range. AMD’s RDNA 2 ISA (VOP1 opcodes 7 and 8) calls for truncation toward zero, saturation at the integer bounds (including infinities), and zero for NaN.

Classify the binary32 input as an integer word, feed the SPIR-V conversion a representable operand, and select the RDNA2 result. The synthetic compute shader writes both results into an integer buffer so the regression checks the exact 32-bit words.

Tested

  • Windows, Python 3.13.13, CPU only, based on 36ace7135. No GPU or game data used.
  • python -B -m unittest discover -s tests/semantic: 389 tests passed. The new fixture covers 16 inputs. With the unmodified translator, 11 reach an out-of-range or non-finite SPIR-V conversion.
  • python -B tests/semantic/regen_fixtures.py --check passed.
  • SPIRV-Tools validation, Vulkan execution, and the retail shader corpus were not run.

Checklist

  • No game data, keys, or decryption.
  • The translator contract in docs/design.md records the ISA reference and regression test.

@FreddyxS

Copy link
Copy Markdown
Contributor Author

Resolved the fixture-list conflict with main and kept both cs_fmt_1010102 and cs_cvt_f32_sat. The PR now uses the float conversion opcodes implemented by the upstream CPU interpreter. On Windows/Python 3.13.13, the 16-case saturation regression passes and regen_fixtures.py --check passes for all 15 fixtures. AI-assisted (Codex).

@tylerstraub

Copy link
Copy Markdown
Contributor

Tested 56c4594243a99f81a284bc8f96f473a9d9e46468 on NVIDIA GeForce RTX 5090, 595.99.02 (Linux).

The translated cs_cvt_f32_sat compute shader matched all 16 cases from tests/semantic/test_float_to_int.py on Vulkan. All 32 unsigned and signed output words matched bit-for-bit, and the six adjacent sentinel words retained their initial values after each dispatch.

I used this head’s translator, disassembly fixture and synthetic header from the semantic test. The resulting module assembled and passed spirv-val --target-env vulkan1.2. In a temporary copy of this head’s vk_compute_test, I set the output descriptor length to 8 bytes and supplied each input bit pattern in push constant s4. I dispatched the same module once per case on Ubuntu 26.04.1 LTS (subgroup 32).

For example, input 4f000000 (+2³¹) returned 80000000 / 7fffffff (unsigned / signed), negative infinity returned 00000000 / 80000000, and both tested NaN encodings returned zero for both conversions. This check covers the synthetic fixture on the translated NVIDIA path. Native RDNA2 execution and corpus coverage remain unmeasured here.

AI-assisted verification.

@EchoTD

EchoTD commented Nov 21, 2026 •

Copy link
Copy Markdown

Tested 56c4594 on AMD Radeon RX 6800 (RDNA2, RADV NAVI21, Mesa 26.2.5, Vulkan 1.4.354), Linux: pass

Edited: my first version concluded that the raw conversion doesn't saturate on RDNA2/RADV. That was wrong. On this setup it only fails for compile-time-constant inputs (first column below).

check.py doesn't run this shader and vk_compute_test leaves s4 at 0. So I made 16 variants per mode, one per input from test_float_to_int.py, built them with build.py (llvm-mc 23.1.3) and ran vk_compute_test <spv> 1 dump. The control is the same tree with only this PR's tools/rdna2spirv.py hunk reverse-applied.

constant input (s_mov_b32 s4, <bits>) runtime input (buffer_load of the 0xEE fill, XORed to <bits>; the input is stored back and checked)
this PR 16/16 16/16
control (no fix) 10/16 16/16

Control with a constant input gives 0 instead of the saturated value:

  • i32 at 0x4f000000, 0x4f7fffff, 0xcf000001 and 0xff800000;
  • both u32 and i32 at 0x4f800000 and 0x7f800000.

Why, from RADV_DEBUG=shaders on the 0x4f000000 control variants:

  • Constant input: no conversion instruction is emitted. ACO stores the folded constant pair 0x80000000, 0 (i32 folded to 0).
  • Runtime input: ACO emits v_cvt_u32_f32 / v_cvt_i32_f32, and the hardware returns 0x80000000, 0x7fffffff (saturated).

So on this card and driver the hardware conversion already saturates. The unfixed translator only fails where the compiler folds an out-of-range conversion, which SPIR-V leaves undefined. This PR gives the expected words in both modes. I haven't tested other drivers or vendors.

AI-assisted: the test runs and this comment were done with Claude Code; I reviewed the results.

@troythedecoyx

Copy link
Copy Markdown
Contributor

Tested at 56c4594 merged onto main 8bd0087, on NVIDIA GeForce RTX 4060 Ti (Ada, subgroup 32..32), driver 32.0.16.2124, Windows 11 Home 26300; LLVM 21.1.8 (WSL), Vulkan SDK 1.4.363, MSVC 2022.

  • cs_cvt_f32_sat translates and passes spirv-val, but neither check.py nor run_gpu_tests.py runs it, and vk_compute_test has no way to seed s4 (the input word), so it isn't checked on hardware yet; the new semantic tests pass.
  • run_gpu_tests.py: 59/59 translated and spirv-val valid; 32/56 passed, 24 skipped natively and 56/56 with LEONIDA_W64_EMULATE=1, none failed. Every other test's PASS/SKIP is identical to main 8bd0087 in both modes.
  • ctest 15/15, tests/test_*.py all pass, tests/semantic 424 OK.

AI-assisted.

@ItsNotRexed
ItsNotRexed merged commit 273e53c into ItsNotRexed:main Nov 21, 2026
@NWX10

NWX10 commented Nov 21, 2026

Copy link
Copy Markdown
Contributor

Tested 56c4594 on RTX 3060 Ti (Ampere), NVIDIA 621.24, Windows 11, Vulkan SDK 1.4.363.1, llvm-mc 21.1.8: pass, with one gap.

  • python -I -m unittest discover -s tests/semantic: 424 OK. regen_fixtures.py --check: OK.
  • Full GPU suite: 59 shaders translated, 59 pass spirv-val, including cs_cvt_f32_sat. Native 32/56 passed, 24 skipped. LEONIDA_W64_EMULATE=1: 56/56. Same as main.
  • Gap: cs_cvt_f32_sat has no GPU check in run_gpu_tests.py or check.py (--filter cs_cvt_f32_sat gives 0/0), and vk_compute_test sets the push constants itself, so the 16 inputs were not executed on the GPU. It was only translated and validated here.

AI-assisted (Claude Code).

@troythedecoyx

Copy link
Copy Markdown
Contributor

Follow-up to my note above: #398 adds VKC_SGPRS=w4,w5,... to vk_compute_test (sets s4 onward; s[0:3] stay the output V#), so cs_cvt_f32_sat can run on a GPU without hand-made variants: VKC_SGPRS=0x4f000000 vk_compute_test cs_cvt_f32_sat.spv 1 dump, once per input from test_float_to_int.py. Happy to run that sweep on the RTX 4060 Ti once both are in, or you can wire it into the runners on top of #398.

AI-assisted.

troythedecoyx added a commit to troythedecoyx/Leonida that referenced this pull request Nov 22, 2026
… an input word

vk_compute_test always put the output V# in s[0:3] and zero in every
other user SGPR, so a compute test shader could not be given an input
word: ItsNotRexed#300's cs_cvt_f32_sat reads its float from s4 and could only be
checked on a GPU by building one shader variant per input by hand.

- VKC_SGPRS=w4,w5,... sets s4, s5, ... up to s31 (s[0:3] stay the output
  V#), with the same syntax and checks as vk_harness's VKH_SGPRS: decimal
  or 0x hex, no sign, nothing above 32 bits, no trailing comma, at most
  28 words; anything else exits 2 with a message.
- cs_user_sgprs.asm declares eight user SGPRs (RSRC2 0x8090) and stores
  s4..s7 back. Both runners seed it with 0x4f000000,0xdeadbeef,7,
  0xffffffff (check.USER_SGPRS) and expect those words, then the
  0xEEEEEEEE fill.
- check.compute_test() takes an env for vk_compute_test.
- tests/test_shader_checks.py: the fake harness only answers
  cs_user_sgprs with the seeded words when VKC_SGPRS was passed, and no
  other test may get VKC_SGPRS.

AI-assisted (Claude Code). Tested on NVIDIA GeForce RTX 4060 Ti (Ada,
subgroup 32), driver 32.0.16.2124, Windows 11, on main d937b7f:
- run_gpu_tests.py cs_user_sgprs PASS natively and with
  LEONIDA_W64_EMULATE=1; check.py cs_user_sgprs ok
- vk_compute_test directly: unset -> s4..s7 read 0; "5" sets s4 only;
  28 words accepted; "1,2,", "-1", "0x1ffffffff", "abc" and 29 words
  exit 2
- test_shader_checks.py fails (7) if check.py stops passing VKC_SGPRS
- full suite: see the PR

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
troythedecoyx added a commit to troythedecoyx/Leonida that referenced this pull request Nov 22, 2026
…h VKC_SGPRS

cs_cvt_f32_sat (ItsNotRexed#300) takes its binary32 input in s4, so neither runner
ran it. Now both do: one dispatch per input of
tests/semantic/test_float_to_int.py (check.CVT_F32_SAT_CASES), each
judged on both words (v_cvt_u32_f32 in out[0], v_cvt_i32_f32 in out[1]).

- check.cvt_f32_sat_test() and the run_gpu_tests.py branch report
  "16/16 inputs exact", or how many were wrong and the first one.
- tests/test_shader_checks.py: the fake device answers from the word in
  VKC_SGPRS; every input must be sent, one wrong input must fail and be
  named, and the table must equal the interpreter test's CASES.

On NVIDIA this checks the fixed translation, but it cannot catch the old
bug: with the translator from before ItsNotRexed#300 (273e53c^1) the RTX 4060 Ti
also gives 16/16, since SPIR-V leaves an out-of-range conversion
undefined and this hardware saturates anyway. A driver that does not
(@EchoTD's RADV case on ItsNotRexed#300, constant inputs) is where it can fail;
tests/semantic/test_float_to_int.py pins the rule itself.

AI-assisted (Claude Code). Tested on NVIDIA GeForce RTX 4060 Ti (Ada,
subgroup 32), driver 32.0.16.2124, Windows 11, on main d937b7f:
run_gpu_tests.py cs_cvt_f32_sat "16/16 inputs exact" natively and with
LEONIDA_W64_EMULATE=1; check.py cs_cvt_f32_sat ok;
tests/test_shader_checks.py 18/18.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
troythedecoyx added a commit to troythedecoyx/Leonida that referenced this pull request Nov 22, 2026
… an input word

vk_compute_test always put the output V# in s[0:3] and zero in every
other user SGPR, so a compute test shader could not be given an input
word: ItsNotRexed#300's cs_cvt_f32_sat reads its float from s4 and could only be
checked on a GPU by building one shader variant per input by hand.

- VKC_SGPRS=w4,w5,... sets s4, s5, ... up to s31 (s[0:3] stay the output
  V#), with the same syntax and checks as vk_harness's VKH_SGPRS: decimal
  or 0x hex, no sign, nothing above 32 bits, no trailing comma, at most
  28 words; anything else exits 2 with a message.
- cs_user_sgprs.asm declares eight user SGPRs (RSRC2 0x8090) and stores
  s4..s7 back. Both runners seed it with 0x4f000000,0xdeadbeef,7,
  0xffffffff (check.USER_SGPRS) and expect those words, then the
  0xEEEEEEEE fill.
- check.compute_test() takes an env for vk_compute_test.
- tests/test_shader_checks.py: the fake harness only answers
  cs_user_sgprs with the seeded words when VKC_SGPRS was passed, and no
  other test may get VKC_SGPRS.

AI-assisted (Claude Code). Tested on NVIDIA GeForce RTX 4060 Ti (Ada,
subgroup 32), driver 32.0.16.2124, Windows 11, on main d937b7f:
- run_gpu_tests.py cs_user_sgprs PASS natively and with
  LEONIDA_W64_EMULATE=1; check.py cs_user_sgprs ok
- vk_compute_test directly: unset -> s4..s7 read 0; "5" sets s4 only;
  28 words accepted; "1,2,", "-1", "0x1ffffffff", "abc" and 29 words
  exit 2
- test_shader_checks.py fails (7) if check.py stops passing VKC_SGPRS
- full suite: see the PR

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
troythedecoyx added a commit to troythedecoyx/Leonida that referenced this pull request Nov 22, 2026
…h VKC_SGPRS

cs_cvt_f32_sat (ItsNotRexed#300) takes its binary32 input in s4, so neither runner
ran it. Now both do: one dispatch per input of
tests/semantic/test_float_to_int.py (check.CVT_F32_SAT_CASES), each
judged on both words (v_cvt_u32_f32 in out[0], v_cvt_i32_f32 in out[1]).

- check.cvt_f32_sat_test() and the run_gpu_tests.py branch report
  "16/16 inputs exact", or how many were wrong and the first one.
- tests/test_shader_checks.py: the fake device answers from the word in
  VKC_SGPRS; every input must be sent, one wrong input must fail and be
  named, and the table must equal the interpreter test's CASES.

On NVIDIA this checks the fixed translation, but it cannot catch the old
bug: with the translator from before ItsNotRexed#300 (273e53c^1) the RTX 4060 Ti
also gives 16/16, since SPIR-V leaves an out-of-range conversion
undefined and this hardware saturates anyway. A driver that does not
(@EchoTD's RADV case on ItsNotRexed#300, constant inputs) is where it can fail;
tests/semantic/test_float_to_int.py pins the rule itself.

AI-assisted (Claude Code). Tested on NVIDIA GeForce RTX 4060 Ti (Ada,
subgroup 32), driver 32.0.16.2124, Windows 11, on main d937b7f:
run_gpu_tests.py cs_cvt_f32_sat "16/16 inputs exact" natively and with
LEONIDA_W64_EMULATE=1; check.py cs_cvt_f32_sat ok;
tests/test_shader_checks.py 18/18.

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

Copy link
Copy Markdown
Contributor Author

Post-merge GPU validation on main at 91b36cf86cb92a95836eed8af73abc0671a61e31 passed on an AMD Radeon RX 9070 XT (Windows, AMD driver 26.10.1 / LLPC, Vulkan 1.4.359).

  • Translated the synthetic cs_cvt_f32_sat fixture once and dispatched it separately for all 16 inputs from tests/semantic/test_float_to_int.py. All 32 signed and unsigned output words matched bit-for-bit, and the adjacent sentinels kept their original values after every dispatch.
  • The module passed spirv-val --target-env vulkan1.2 with SPIRV-Tools 2026.4. A temporary local change to vk_compute_test seeded s4 with each input's raw bits. The shader and translator were unmodified.
  • The targeted CPU conversion tests passed. The full semantic run reproduced the separate shift-test failure tracked in spirv_mini_interp: OpBitcast and float outputs quiet signalling-NaN bit patterns (bit 22 flips) #509.

AI-assisted (Codex).

troythedecoyx added a commit to troythedecoyx/Leonida that referenced this pull request Nov 22, 2026
… an input word

vk_compute_test always put the output V# in s[0:3] and zero in every
other user SGPR, so a compute test shader could not be given an input
word: ItsNotRexed#300's cs_cvt_f32_sat reads its float from s4 and could only be
checked on a GPU by building one shader variant per input by hand.

- VKC_SGPRS=w4,w5,... sets s4, s5, ... up to s31 (s[0:3] stay the output
  V#), with the same syntax and checks as vk_harness's VKH_SGPRS: decimal
  or 0x hex, no sign, nothing above 32 bits, no trailing comma, at most
  28 words; anything else exits 2 with a message.
- cs_user_sgprs.asm declares eight user SGPRs (RSRC2 0x8090) and stores
  s4..s7 back. Both runners seed it with 0x4f000000,0xdeadbeef,7,
  0xffffffff (check.USER_SGPRS) and expect those words, then the
  0xEEEEEEEE fill.
- check.compute_test() takes an env for vk_compute_test.
- tests/test_shader_checks.py: the fake harness only answers
  cs_user_sgprs with the seeded words when VKC_SGPRS was passed, and no
  other test may get VKC_SGPRS.

AI-assisted (Claude Code). Tested on NVIDIA GeForce RTX 4060 Ti (Ada,
subgroup 32), driver 32.0.16.2124, Windows 11, on main d937b7f:
- run_gpu_tests.py cs_user_sgprs PASS natively and with
  LEONIDA_W64_EMULATE=1; check.py cs_user_sgprs ok
- vk_compute_test directly: unset -> s4..s7 read 0; "5" sets s4 only;
  28 words accepted; "1,2,", "-1", "0x1ffffffff", "abc" and 29 words
  exit 2
- test_shader_checks.py fails (7) if check.py stops passing VKC_SGPRS
- full suite: see the PR

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
troythedecoyx added a commit to troythedecoyx/Leonida that referenced this pull request Nov 22, 2026
…h VKC_SGPRS

cs_cvt_f32_sat (ItsNotRexed#300) takes its binary32 input in s4, so neither runner
ran it. Now both do: one dispatch per input of
tests/semantic/test_float_to_int.py (check.CVT_F32_SAT_CASES), each
judged on both words (v_cvt_u32_f32 in out[0], v_cvt_i32_f32 in out[1]).

- check.cvt_f32_sat_test() and the run_gpu_tests.py branch report
  "16/16 inputs exact", or how many were wrong and the first one.
- tests/test_shader_checks.py: the fake device answers from the word in
  VKC_SGPRS; every input must be sent, one wrong input must fail and be
  named, and the table must equal the interpreter test's CASES.

On NVIDIA this checks the fixed translation, but it cannot catch the old
bug: with the translator from before ItsNotRexed#300 (273e53c^1) the RTX 4060 Ti
also gives 16/16, since SPIR-V leaves an out-of-range conversion
undefined and this hardware saturates anyway. A driver that does not
(@EchoTD's RADV case on ItsNotRexed#300, constant inputs) is where it can fail;
tests/semantic/test_float_to_int.py pins the rule itself.

AI-assisted (Claude Code). Tested on NVIDIA GeForce RTX 4060 Ti (Ada,
subgroup 32), driver 32.0.16.2124, Windows 11, on main d937b7f:
run_gpu_tests.py cs_cvt_f32_sat "16/16 inputs exact" natively and with
LEONIDA_W64_EMULATE=1; check.py cs_cvt_f32_sat ok;
tests/test_shader_checks.py 18/18.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
troythedecoyx added a commit to troythedecoyx/Leonida that referenced this pull request Nov 22, 2026
… an input word

vk_compute_test always put the output V# in s[0:3] and zero in every
other user SGPR, so a compute test shader could not be given an input
word: ItsNotRexed#300's cs_cvt_f32_sat reads its float from s4 and could only be
checked on a GPU by building one shader variant per input by hand.

- VKC_SGPRS=w4,w5,... sets s4, s5, ... up to s31 (s[0:3] stay the output
  V#), with the same syntax and checks as vk_harness's VKH_SGPRS: decimal
  or 0x hex, no sign, nothing above 32 bits, no trailing comma, at most
  28 words; anything else exits 2 with a message.
- cs_user_sgprs.asm declares eight user SGPRs (RSRC2 0x8090) and stores
  s4..s7 back. Both runners seed it with 0x4f000000,0xdeadbeef,7,
  0xffffffff (check.USER_SGPRS) and expect those words, then the
  0xEEEEEEEE fill.
- check.compute_test() takes an env for vk_compute_test.
- tests/test_shader_checks.py: the fake harness only answers
  cs_user_sgprs with the seeded words when VKC_SGPRS was passed, and no
  other test may get VKC_SGPRS.

AI-assisted (Claude Code). Tested on NVIDIA GeForce RTX 4060 Ti (Ada,
subgroup 32), driver 32.0.16.2124, Windows 11, on main d937b7f:
- run_gpu_tests.py cs_user_sgprs PASS natively and with
  LEONIDA_W64_EMULATE=1; check.py cs_user_sgprs ok
- vk_compute_test directly: unset -> s4..s7 read 0; "5" sets s4 only;
  28 words accepted; "1,2,", "-1", "0x1ffffffff", "abc" and 29 words
  exit 2
- test_shader_checks.py fails (7) if check.py stops passing VKC_SGPRS
- full suite: see the PR

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
troythedecoyx added a commit to troythedecoyx/Leonida that referenced this pull request Nov 22, 2026
…h VKC_SGPRS

cs_cvt_f32_sat (ItsNotRexed#300) takes its binary32 input in s4, so neither runner
ran it. Now both do: one dispatch per input of
tests/semantic/test_float_to_int.py (check.CVT_F32_SAT_CASES), each
judged on both words (v_cvt_u32_f32 in out[0], v_cvt_i32_f32 in out[1]).

- check.cvt_f32_sat_test() and the run_gpu_tests.py branch report
  "16/16 inputs exact", or how many were wrong and the first one.
- tests/test_shader_checks.py: the fake device answers from the word in
  VKC_SGPRS; every input must be sent, one wrong input must fail and be
  named, and the table must equal the interpreter test's CASES.

On NVIDIA this checks the fixed translation, but it cannot catch the old
bug: with the translator from before ItsNotRexed#300 (273e53c^1) the RTX 4060 Ti
also gives 16/16, since SPIR-V leaves an out-of-range conversion
undefined and this hardware saturates anyway. A driver that does not
(@EchoTD's RADV case on ItsNotRexed#300, constant inputs) is where it can fail;
tests/semantic/test_float_to_int.py pins the rule itself.

AI-assisted (Claude Code). Tested on NVIDIA GeForce RTX 4060 Ti (Ada,
subgroup 32), driver 32.0.16.2124, Windows 11, on main d937b7f:
run_gpu_tests.py cs_cvt_f32_sat "16/16 inputs exact" natively and with
LEONIDA_W64_EMULATE=1; check.py cs_cvt_f32_sat ok;
tests/test_shader_checks.py 18/18.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
ItsNotRexed pushed a commit that referenced this pull request Nov 22, 2026
Both raised Unsupported. RDNA ISA pseudocode: f32_to_i32(floor(S0)) and
f32_to_i32(floor(S0 + 0.5)), the + 0.5 rounded in binary32. The conversion
saturates as v_cvt_i32_f32 does (#300); only an in-range value reaches
OpConvertFToS.

tests/semantic/test_alu_cvt_flr_rpi.py checks both against a plain-Python
reference; docs/design.md gets a contract row.
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.

6 participants