Skip to content

shader: define v_cvt_rpi/flr_i32_f32 for NaN, infinity and overflow in pixel_aot - #1885

Merged
tedin7 merged 1 commit into
R0Y00:mainfrom
Asimmimx:shader/cvt-rpi-flr-exceptional
Nov 24, 2026
Merged

tedin7 merged 1 commit into
R0Y00:mainfrom
Asimmimx:shader/cvt-rpi-flr-exceptional

Conversation

@Asimmimx

Copy link
Copy Markdown
Contributor

Change

Fixes #1772.

tools/pixel_aot.py now defines v_cvt_flr_i32_f32 and v_cvt_rpi_i32_f32 for every binary32 input, so runtime sources are no longer refused.

  • cvt_flr_i32_f32 / cvt_rpi_i32_f32 (the lazy HLSL helpers): a NaN gives 0x7fffffff when its sign bit is clear and 0x80000000 when it is set (quiet or signalling). Both infinities and out-of-range values saturate to 0x7fffffff / 0x80000000. The helpers replace the NaN word before any float operation, clamp the rounded value to [-2^31, 2^31 - 128] (2^31 - 1 is not a binary32) and select 0x7fffffff for x >= 2^31. No HLSL cast reads a NaN or an out-of-range value any more. Before this change, int(floor(x)) and int(f + ...) were undefined casts for those inputs. Finite in-range results are bit-identical to the old helpers, and rpi still computes the floor of the exact x + 0.5.
  • require_proven_i32_f32_range is removed. Its docstring said it refused unproven runtime inputs and NaN, infinite or out-of-range constants "until a native RDNA reference establishes exceptional behavior". It guarded nothing else, since it already accepted subnormal constants. Its two call sites are removed, and the comments that cited it (cvt_f64_f32_source_bits, the v_cvt_f64_f32 branch, the e64 branch) are updated. The other refusals are unchanged: clamp, omod, decimal or float literals, bare -0x..., reversed modifiers, DPP and SDWA, pending scalar or sample reads, and WQM.
  • tools/scan_unsupported_shaders.py: the e32 and e64 patterns for v_cvt_i32_f32 now also cover flr_i32 and rpi_i32. The scanner previously left both opcodes unsupported for the same reason.
  • docs/experiments.md: the flr/rpi paragraph is rewritten, and two stale "flr/rpi remain rejected" mentions in the neighbouring cvt paragraphs are removed.

This follows R0Y00's condition on #540: "Before extending to NaN, infinities, or out-of-range values, we need an independent native reference that can observe those results as raw words, including values around ±0.5 and both integer endpoints." #1772 cites two such references: Marvisak/VIctory#605 (comment) (gfx1036, HIP inline asm) and xcl-dev/xcl#639 (comment) (gfx1036 and RDNA 4 gfx1201, OpenCL inline asm). Both include ±0.5, 0.49999997, ±2^31, both infinities and NaNs of both signs. Below I add a third native readback, on gfx1032.

Caveat: gfx1032, gfx1036 and gfx1201 are desktop RDNA 2 and RDNA 4 parts, not gfx1013 or Scarlett.

Verification

Commit 122e2db2 (rebased on main df40e4bc). Windows 11 Pro 10.0.26300.9974, MSVC (VS dev shell via tools/dev.ps1), Windows SDK 10.0.26100.0, DXC 1.8.2502.11, Python from out/pe-env.

cmake --preset dev -DINTAKE_PYTHON=C:/Users/asimm/Desktop/Xbox2Windows/out/pe-env/Scripts/python.exe
cmake --build --preset dev --target offscreen_check -j 4
ctest --preset dev --no-tests=error -R '^pixel_aot_cvt_flr_rpi_(warp|hardware)$'
1/2 Test  #903: pixel_aot_cvt_flr_rpi_hardware ...   Passed   12.23 sec
2/2 Test #1072: pixel_aot_cvt_flr_rpi_warp .......   Passed    5.33 sec
100% tests passed, 0 tests failed out of 2
  • WARP: Microsoft Basic Render Driver (1414:008c), driver 10.0.26100.9818, D3D12 debug layer enabled, debug_errors=0 debug_warnings=0. Passed.
  • Hardware: AMD Radeon RX 6600 (1002:73ff, RDNA 2, gfx1032), driver 32.0.21045.29003, D3D12 debug layer enabled, debug_errors=0 debug_warnings=0. Passed.
  • Skips: none.

Python checks (out/pe-env/Scripts/python.exe -B), all passed:

  • tests/pixel_aot_check.py (no arguments: translator controls), exit 0
  • tests/rdna2_tools_check.py: OK (skipped=18). The skips are the existing LLVM-dependent cases.
  • tests/form_coverage_check.py: form coverage checks passed
  • tests/docs_links_check.py: Checked 18 Markdown files and 376 links.
  • tests/bidi_controls_check.py: Checked 1326 tracked source, script and build/CI files: no bidirectional controls.
  • tests/pso_readiness_check.py, tests/shader_census_check.py: passed

Not run: tests/pixel_aot_check.py binary. Only LLVM 19.1.1 (WinLibs) is installed here, and CI pins LLVM 15. This PR adds no binary round-trip fixtures; the existing ones are unchanged.

What the draw test now covers (check_cvt_flr_rpi_draw)

The existing finite rows, modifier rows and mutations are kept. They are added to:

  • Runtime exceptional rows. The inputs come from the constant buffer, so DXC cannot fold them. They run through e32 and e64, SGPR and VGPR. The 24 hand-worked rows are in CVT_FLR_RPI_EXCEPTIONAL: ±qNaN, ±sNaN, ±NaN with all payload bits, ±inf, ±2^31, ±(2^31 + 256), 2^31 - 128, ±2^63, ±FLT_MAX, 0.49999997, ±0.5, -0.50000006, ±(2^23 + 1) and 2^24 - 1. The Python references must reproduce the table before any draw.
  • Runtime e64 modifiers on NaN and infinity. |-qNaN|, -(+qNaN), -|+sNaN|, -(-NaN), |-inf|, -(+inf) and -|±(2^31+256)|, for both opcodes.
  • Literal constants. NaN, infinity and out-of-range values that DXC may fold at compile time.
  • Negative controls, each of which must report pixel_mismatch:
    • a case that expects 0 for NaN, which is what v_cvt_i32_f32 gives;
    • dropping abs or neg on a NaN source;
    • three mutants of the emitted helper: NaN sign select dropped, sign select reversed (0x80000000u - sign), and the 2^31 saturation select dropped.
  • Dev-time control, not committed: with the old cvt_flr_i32_f32 helper swapped in, the draw test fails on WARP (Expected ['pass', ...], got ['mismatch', ...]).

Evidence and follow-up

Native gfx1032 readback (RX 6600, dev-time probe, not committed). I ran v_cvt_flr_i32_f32 and v_cvt_rpi_i32_f32 as inline assembly in an OpenCL C kernel, loaded through ctypes. Device gfx1032, board AMD Radeon RX 6600, OpenCL driver 3652.0 (PAL,LC). Each input ran under all four MODE.IEEE / MODE.DX10_CLAMP settings, with f32 denormals flushed or kept, for eight modes. MODE was read back after each s_setreg and gave 0xc0, 0x2c0, 0x1c0, 0x3c0, 0xf0, 0x2f0, 0x1f0, 0x3f0. Every row of CVT_FLR_RPI_EXCEPTIONAL matched in all eight modes, including the rows the table marks "by rule": ±(2^31 + 256), 2^31 - 128, ±2^63 and ±FLT_MAX.

word        flr         rpi
0x7fc00000  0x7fffffff  0x7fffffff
0xffc00000  0x80000000  0x80000000
0x7f800001  0x7fffffff  0x7fffffff
0xff800001  0x80000000  0x80000000
0x7fffffff  0x7fffffff  0x7fffffff
0xffffffff  0x80000000  0x80000000
0x7f800000  0x7fffffff  0x7fffffff
0xff800000  0x80000000  0x80000000
0x4f000000  0x7fffffff  0x7fffffff
0xcf000000  0x80000000  0x80000000
0x4f000001  0x7fffffff  0x7fffffff
0xcf000001  0x80000000  0x80000000
0x4effffff  0x7fffff80  0x7fffff80
0x5f000000  0x7fffffff  0x7fffffff
0xdf000000  0x80000000  0x80000000
0x7f7fffff  0x7fffffff  0x7fffffff
0xff7fffff  0x80000000  0x80000000
0x3effffff  0x00000000  0x00000000
0x3f000000  0x00000000  0x00000001
0xbf000000  0xffffffff  0x00000000
0xbf000001  0xffffffff  0xffffffff
0x4b000001  0x00800001  0x00800001
0xcb000001  0xff7fffff  0xff7fffff
0x4b7fffff  0x00ffffff  0x00ffffff
0x80000001  mode-dependent: flr 0xffffffff with f32 denormals kept, 0x00000000 flushed; rpi 0
0x807fffff  mode-dependent: flr 0xffffffff with f32 denormals kept, 0x00000000 flushed; rpi 0

Checklist

  • This pull request covers one reviewable behavior.
  • I included the exact command and result for every check I ran.
  • I considered a negative control or failure case for a new fixture.
  • I updated README/docs when a capability or build requirement changed.
  • I did not include game files, dumps, distribution links or secrets.

AI-assisted (Claude Code).

…n pixel_aot

The cvt_flr_i32_f32 and cvt_rpi_i32_f32 helpers now give 0x7fffffff for a NaN
with a clear sign bit and 0x80000000 for a set one, and saturate infinities and
out-of-range values, as native RDNA 2 and RDNA 4 raw words show (issue R0Y00#1772).
The NaN word is replaced before any float operation and the rounded value is
clamped before the cast, so no HLSL cast reads a NaN or an out-of-range value.
Finite in-range results are unchanged.

require_proven_i32_f32_range existed only to keep inputs in the defined
domain, so it is removed: runtime inputs and exceptional constants now lower,
and the scanner accepts the same e32 and e64 spellings as v_cvt_i32_f32.

@tedin7 tedin7 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Peer review of head 122e2db27708d4374a133ce12a98cffd8240dd48 (PRODUCTION_REVIEW_ONLY, technical comment only).

No P1 and no P2 found in the lowering or in its tests. One open question for the maintainers about the evidence the new behavior rests on (below).

What the change does: v_cvt_flr_i32_f32 and v_cvt_rpi_i32_f32 (e32 and e64 with abs/neg) stop requiring a statically proven input. The two helpers now define NaN, infinities and out-of-range inputs, and tools/scan_unsupported_shaders.py accepts the same spellings. I read all five changed files.

Helper review (cvt_flr_i32_f32, cvt_rpi_i32_f32 in tools/pixel_aot.py):

  • The NaN test is on the raw word ((bits & 0x7fffffff) > 0x7f800000) and the NaN is replaced by 0 before any float operation. A NaN returns 0x7fffffff + (bits >> 31): 0x7fffffff with the sign clear, 0x80000000 with it set, quiet or signalling.
  • The rounded value is clamped to [-2^31, 2147483520] (the largest binary32 below 2^31), and x >= 2^31 selects 0x7fffffff before the cast, so no HLSL cast sees a NaN or an out-of-range value. +inf and -inf, +-2^31, +-FLT_MAX all land on the intended limits.
  • For rpi, an infinity gives x - floor(x) = NaN, the comparison is false, and the clamp and the >= 2^31 select still give the saturated values. For large integers (2^23 + 1) x - f is 0, so they are unchanged; 0x3effffff stays 0.
  • Finite in-range results are unchanged from the previous helpers.
  • Removal of require_proven_i32_f32_range and its two call sites leaves no dangling use (math and known_ssa_bits are still used elsewhere in the file).
  • The new tests, rdna2_tools_check entries and scanner entries are consistent with each other.

Question for the maintainers (not a defect in the code): the exceptional-input table (NaN by sign, saturation) comes from measurements on other RDNA parts, cited in issue #1772 (an RDNA 2 desktop APU and an RDNA 4 card) and in the docs (an RX 6600). The issue itself says none of these is gfx1013 or the console, and I cannot reproduce or inspect those measurements from here. Before this PR the two opcodes refused runtime inputs; after it, a title shader that feeds a NaN to them gets a defined result chosen from those measurements. If the console part differs, the failure would be silent. That is a project decision, so I am leaving it to you rather than judging it.

Mutations of the helpers in tools/pixel_aot.py, one at a time (each restored with git checkout -- .):

  • NaN sign select dropped (return nan ? 0x7fffffffu : word): tests/pixel_aot_check.py fails (rc=1) on Linux.
  • NaN not replaced before the float operations (asfloat(bits)): fails (rc=1) on Linux.
  • the x >= 2^31 saturation select dropped: the Linux check does not catch it (rc=0). On Windows Server 2025 with the mutated tree, pixel_aot_cvt_flr_rpi_warp fails ("case=3..7 and 12 mismatch", pixel_mismatch at 0,0), so the draw test pins it.
  • rpi half-way comparison changed from >= to >: passes the Linux check (rc=0). Only the WARP draw test covers values at exactly 0.5.

Commands run:

  • Linux: git diff --check <merge-base>...HEAD clean; tests/docs_links_check.py, tests/bidi_controls_check.py, tests/pixel_aot_check.py, tests/rdna2_tools_check.py (148 tests, 18 skipped), tests/form_coverage_check.py: all pass; the mutations above.
  • Windows Server 2025 (build 26100, MSVC 14.44, WARP) at the same commit 122e2db2, target offscreen_check, regex ^(pixel_aot_cvt_flr_rpi_warp|pixel_aot_warp|rdna2_tools_check)$:
    • Debug (--preset dev): 3/3 passed
    • Release /O2 (--preset release): 3/3 passed
    • Python checks tests/pixel_aot_check.py and tests/docs_links_check.py: ok
  • The merge against current main has no content conflicts (git merge-tree).

AI-assisted; commands listed were executed locally.

@tedin7 tedin7 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Peer review of head 122e2db27708d4374a133ce12a98cffd8240dd48 (TIER_D_ISOLATED_SHADER_OPS, technical comment).

No P1 and no P2 found. I read the diff of tools/pixel_aot.py, tools/scan_unsupported_shaders.py and the three test/doc files, and I agree with the earlier review on the helper logic (NaN word replaced before any float op, clamp to [-2^31, 2147483520], x >= 2^31 selects 0x7fffffff, finite in-range results unchanged, no dangling use of the removed function).

Note: the exceptional-input table comes from RDNA 2 (gfx1036) and RDNA 4 measurements cited in #1772, not from gfx1013 or the console. That is a project decision and I did not verify the hardware source.

Commands run locally:

  • git diff --check origin/main...HEAD: rc=0
  • python3 tests/docs_links_check.py: rc=0 (18 files, 376 links)
  • python3 tests/bidi_controls_check.py: rc=0
  • python3 tests/pixel_aot_check.py: rc=0
  • python3 tests/rdna2_tools_check.py: rc=0
  • Windows Server 2025 (build 26100, MSVC 14.44, WARP), target offscreen_check, regex ^(pixel_aot_cvt_flr_rpi_warp|pixel_aot_warp|rdna2_tools_check)$: Debug 3/3 passed, Release /O2 3/3 passed, tests/form_coverage_check.py ok

Mutations (each restored with git checkout -- .):

  • NaN sign select dropped in cvt_flr_i32_f32: pixel_aot_check.py rc=1 (caught by a source-text assertion, so this one is wording-level evidence only)
  • rpi half-way test >= changed to >: Linux check rc=0 (not caught on Linux)
  • x >= 2^31 saturation select dropped in cvt_flr_i32_f32: Linux check rc=0; on Windows Server 2025 with the mutated tree, pixel_aot_cvt_flr_rpi_warp fails rc=1 (draw status mismatch, e.g. expected 0x00000000 got 0x80000000), so the WARP draw test pins the saturation logic

AI-assisted; commands listed were executed locally.

@tedin7
tedin7 merged commit 4eeaf3b into R0Y00:main Nov 24, 2026
5 checks passed
tedin7 pushed a commit that referenced this pull request Nov 24, 2026
…tz in pixel_aot

Extend the quad-local conversion DPP16/DPP8 slice (#421) with the three
remaining full-32-bit/export conversion opcodes (6 forms total across
DPP16 and DPP8 on gfx1013 and rdna2):

- v_cvt_flr_i32_f32_dpp and v_cvt_rpi_i32_f32_dpp added to DPP_CVT,
  reusing the shared cvt_flr_i32_f32 and cvt_rpi_i32_f32 helpers (handling
  fractional rounding, saturation, infinity, and signed NaNs as in #1885)
  with DPP16 abs/neg bit modifiers and DPP8 modifier refusal.
- v_cvt_pkrtz_f16_f32_dpp lowered as a compressed-export pair matching
  v_cvt_pkrtz_f16_f32_e32: src0 is quad-swizzled via emit_quad_swizzle_u32,
  src1 stays on the current lane, DPP16 accepts abs/neg on either source,
  and non-export reads of the packed destination remain refused.

Extend pixel_aot_dpp_cvt_{warp,hardware} with raw-word flr_rpi rows
(including fractional half-integers, overflow, infinities, signed NaNs,
and 5 mutations) and a compressed-export pkrtz draw (with RTZ-vs-RNE
mismatch control and 4 routing/modifier mutations), add both fixtures to
check_binary, and update form_coverage_check and docs/experiments.md.
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.

pixel_aot: a native RDNA 2 reference for v_cvt_rpi/flr_i32_f32 exceptional inputs (NaN, ±inf, overflow)

2 participants