Repository navigation
shader: define v_cvt_rpi/flr_i32_f32 for NaN, infinity and overflow in pixel_aot - #1885
Conversation
…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
left a comment
There was a problem hiding this comment.
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 returns0x7fffffff + (bits >> 31):0x7fffffffwith the sign clear,0x80000000with it set, quiet or signalling. - The rounded value is clamped to [-2^31, 2147483520] (the largest binary32 below 2^31), and
x >= 2^31selects0x7fffffffbefore the cast, so no HLSL cast sees a NaN or an out-of-range value. +inf and -inf,+-2^31,+-FLT_MAXall land on the intended limits. - For
rpi, an infinity givesx - floor(x) = NaN, the comparison is false, and the clamp and the>= 2^31select still give the saturated values. For large integers (2^23 + 1)x - fis 0, so they are unchanged;0x3effffffstays 0. - Finite in-range results are unchanged from the previous helpers.
- Removal of
require_proven_i32_f32_rangeand its two call sites leaves no dangling use (mathandknown_ssa_bitsare still used elsewhere in the file). - The new tests,
rdna2_tools_checkentries 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.pyfails (rc=1) on Linux. - NaN not replaced before the float operations (
asfloat(bits)): fails (rc=1) on Linux. - the
x >= 2^31saturation select dropped: the Linux check does not catch it (rc=0). On Windows Server 2025 with the mutated tree,pixel_aot_cvt_flr_rpi_warpfails ("case=3..7 and 12 mismatch", pixel_mismatch at 0,0), so the draw test pins it. rpihalf-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>...HEADclean;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, targetoffscreen_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.pyandtests/docs_links_check.py: ok
- Debug (
- The merge against current
mainhas no content conflicts (git merge-tree).
AI-assisted; commands listed were executed locally.
tedin7
left a comment
There was a problem hiding this comment.
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=0python3 tests/docs_links_check.py: rc=0 (18 files, 376 links)python3 tests/bidi_controls_check.py: rc=0python3 tests/pixel_aot_check.py: rc=0python3 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.pyok
Mutations (each restored with git checkout -- .):
- NaN sign select dropped in
cvt_flr_i32_f32:pixel_aot_check.pyrc=1 (caught by a source-text assertion, so this one is wording-level evidence only) rpihalf-way test>=changed to>: Linux check rc=0 (not caught on Linux)x >= 2^31saturation select dropped incvt_flr_i32_f32: Linux check rc=0; on Windows Server 2025 with the mutated tree,pixel_aot_cvt_flr_rpi_warpfails 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.
…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.
Change
Fixes #1772.
tools/pixel_aot.pynow definesv_cvt_flr_i32_f32andv_cvt_rpi_i32_f32for every binary32 input, so runtime sources are no longer refused.cvt_flr_i32_f32/cvt_rpi_i32_f32(the lazy HLSL helpers): a NaN gives0x7fffffffwhen its sign bit is clear and0x80000000when it is set (quiet or signalling). Both infinities and out-of-range values saturate to0x7fffffff/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 select0x7fffffffforx >= 2^31. No HLSL cast reads a NaN or an out-of-range value any more. Before this change,int(floor(x))andint(f + ...)were undefined casts for those inputs. Finite in-range results are bit-identical to the old helpers, andrpistill computes the floor of the exactx + 0.5.require_proven_i32_f32_rangeis 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, thev_cvt_f64_f32branch, 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 forv_cvt_i32_f32now also coverflr_i32andrpi_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 onmaindf40e4bc). Windows 11 Pro 10.0.26300.9974, MSVC (VS dev shell viatools/dev.ps1), Windows SDK 10.0.26100.0, DXC 1.8.2502.11, Python fromout/pe-env.1414:008c), driver 10.0.26100.9818, D3D12 debug layer enabled,debug_errors=0 debug_warnings=0. Passed.1002:73ff, RDNA 2, gfx1032), driver 32.0.21045.29003, D3D12 debug layer enabled,debug_errors=0 debug_warnings=0. Passed.Python checks (
out/pe-env/Scripts/python.exe -B), all passed:tests/pixel_aot_check.py(no arguments: translator controls), exit 0tests/rdna2_tools_check.py:OK (skipped=18). The skips are the existing LLVM-dependent cases.tests/form_coverage_check.py:form coverage checks passedtests/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: passedNot 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:
CVT_FLR_RPI_EXCEPTIONAL:±qNaN,±sNaN,±NaNwith 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)and2^24 - 1. The Python references must reproduce the table before any draw.|-qNaN|,-(+qNaN),-|+sNaN|,-(-NaN),|-inf|,-(+inf)and-|±(2^31+256)|, for both opcodes.pixel_mismatch:0for NaN, which is whatv_cvt_i32_f32gives;absornegon a NaN source;0x80000000u - sign), and the2^31saturation select dropped.cvt_flr_i32_f32helper 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_f32andv_cvt_rpi_i32_f32as inline assembly in an OpenCL C kernel, loaded through ctypes. Devicegfx1032, boardAMD Radeon RX 6600, OpenCL driver3652.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 eachs_setregand gave0xc0, 0x2c0, 0x1c0, 0x3c0, 0xf0, 0x2f0, 0x1f0, 0x3f0. Every row ofCVT_FLR_RPI_EXCEPTIONALmatched in all eight modes, including the rows the table marks "by rule":±(2^31 + 256),2^31 - 128,±2^63and±FLT_MAX.flrdepend on MODE, which matches the gfx1036 report. The draws keep the harness's existing assumption,F32_DENORM_MODE = "preserve"; this PR does not change it. The console's FLOAT_MODE is not measured.0x00000000from both opcodes, and infinities and out-of-range values saturated. So the old undefined casts were not adapter-divergent on these two adapters. They gave 0 for NaN, which differs from the native0x7fffffff/0x80000000in 6 of 26 rows per opcode. With the new helpers, 0 of 26 rows differ on both adapters.tests/pixel_aot_check.py, with hand-worked expected words. No game data.git merge-treeagainst the open pixel_aot PRs shader: lower the 46 non-cmpx SDWA comparisons #1790, shader: lower the binary16 SDWA comparisons #1680, shader: lower the integer SDWA comparisons #1670, shader: convert signed and unsigned SDWA words to f32 #1644, shader: lower experimental gfx1030 2D sample clamps #1638, shader: lower the v_cvt_f32_ubyte SDWA unpacking forms in pixel_aot #1612, shader: lower four SDWA unsigned-byte conversions to f32 #1584, shader: lower eight SDWA bit and 24-bit multiply forms #1572, shader: lower gfx1030 comparison sample LOD and bias #1568, shader: lower the 16-bit three-operand float VOP3 ops in pixel_aot #1466, shader: lower paired 16-bit attr0 interpolation under round toward zero in pixel_aot (experiment) #713, shader: lower gfx1013 integer multiplies in pixel_aot #532, shader: lower gfx1013 byte and bit funnel VOP3 instructions in pixel_aot #531 and shader: lower gfx1013 v_cvt_pkrtz_f16_f32 as an ALU result in pixel_aot #528.mainin the same files, with the same number of conflict regions with or without this commit.v_cvt_pkrtz_f16_f32) has one of this PR's changed lines inside an existing conflict region: the cvt e32/e64 branches inlower()and scanner lines 170-171.Checklist
AI-assisted (Claude Code).