shader: emit V_MED3_F32 on integer keys #1567
No reviewers
Labels
No labels
bug
documentation
duplicate
enhancement
GPU
GUI
help wanted
question
Shaders
Video
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/KytyPS5--KytyPS5!1567
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/1567/head"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@Nawid3333 on GitHub · 2026-10-16 19:00 UTC · original
Nawid3333:shader/med3-integer-keys→main· +116 −34 · 7 files · 3 commits · closed without mergingAnother one for nmzik's idea in #1534. V_MED3_F32 (both AMD drivers here compile
clamp(x, lo, hi)to onev_med3, with constant or variable bounds) is built from six calls of the f32 min/max helper, five NaN classifications and two float compares against MAX3 (defb2f11).Change (
EmitFPMedTri32): map each input to an integer key that sorts like the float,bits ^ ((bits >> 31) & 0x7fffffff)(-0 before +0, denormals by value; the map is its own inverse), take the median withSMin/SMaxand map it back. With a NaN input the result is min3 over the non-NaN inputs (NaN keys set toINT32_MAX), or the third input when all three are NaN, as on main. 37 instructions, all integer. In a compute shader whose FLOAT_MODE flushes f32 denormal inputs, the three inputs are first flushed to signed zero, as the med3 instructions of both GPUs here do in aDenormFlushToZeroshader (four more instructions per input).Ties between signed zeros: the keys put -0 below +0, so (-0, -0, +0), (-0, +0, -0), (-1, -0, +0), (-0, -1, +0) and (-0, +0, -1) give -0, where main gives +0.
v_med3_f32on an RDNA2 GPU (Ryzen 7600X iGPU) andv_med3_num_f32on the RX 9070 XT return -0 for all five, so the PR changes those five expected values inVectorMed3F32NanUsesMin3Pathto -0 (measurement in the comment below).Size (the module of
VectorMed3F32NanUsesMin3Path, 23 V_MED3_F32, harness dump on the RX 9070 XT)About 38 instructions fewer per emitted V_MED3_F32 (a wave-64 shader on a 32-wide host emits each one twice, once per half). The test's size limit moves from 17,600 / 36,620 to 13,171 / 27,752 words, the same margins over the measured sizes.
Results
mid3(), compiled tov_med3_f32on the iGPU): every 32-bit value in each of the three positions with the other two from 12 edge values (both zeros, the smallest denormals, the smallest normals, ±1, infinities, a quiet and a signaling NaN), 1.86 × 10^12 triples, in aDenormPreserveshader and in aDenormFlushToZeroshader (with the input flush): no difference on any triple without a NaN. With three NaN inputs the hardware returns the first input unchanged and this PR the third, as main does; that is left as it is.DenormPreserve, andVectorMed3F32DenormalInputs(float_mode 0xf0) fails on both GPUs. The integer keys don't depend on what the host does with denormals.VectorMed3F32DenormalInputs(float_mode 0xf0) andVectorMed3F32FlushesDenormalInputs(0xc0), expect whatv_med3_f32(iGPU) andv_med3_num_f32(RX 9070 XT) return for the same inputs. The flush test has the largest denormal and the smallest normal next to ±0.5, and both have +inf next to finite inputs. Each of three one-line bugs in the new code (an infinity counted as a NaN, the smallest normal flushed, the larger denormals not flushed) makes at least one of them fail; without those rows, none of the three makes these tests orVectorMed3F32NanUsesMin3Pathfail.cd4eab9don both GPUs, also with the harness forced to 32-wide subgroups (a local switch):VectorMed3F32NanUsesMin3Path(wave 32 and 64),VectorMed3F32DenormalInputsandVectorMed3F32FlushesDenormalInputsfail on main and pass with the PR, with no validation-layer messages. Where main'sVectorMed3F32NanUsesMin3Pathgets past the new size limit (wave 64 on a 64-wide host), it fails exactly the five rows above. The other 809 compute-suite entries and the 30 graphics cases give the same results with and without the PR.If you'd like this done differently, tell me and I'll change it.
Written with Claude Code (AI): it wrote the change and ran the tests and measurements on my PC (driver 26.8.1, Windows 11). I reviewed the diff and this description before opening the PR.
🤖 Generated with Claude Code
@coderabbitai[bot] on GitHub · 2026-10-16 19:05 UTC · original · edited 2026-10-19 13:20 UTC
No actionable comments were generated in the recent review. 🎉
ℹ️ Recent review info
⚙️ Run configuration
9f68ca2f-e5fe-4795-9161-195f8c3333c3📥 Commits
Reviewing files that changed from the base of the PR and between e23737ca10a4b443f3d9a0b7cd846d2ffa76f8b1 and
d8f3154d14.📒 Files selected for processing: 3
src/graphics/shader/recompiler/backend/spirv/spirvEmitterAlu.cppsrc/graphics/shader/recompiler/backend/spirv/spirvEmitterAluHelpers.cpptests/ShaderRecompilerComputeTests.cpp💤 Files with no reviewable changes: 1
📝 Walkthrough
V_MED3_F32now forwards the input-denormal flush flag through translation to SPIR-V emission. The emitter replaces subnormal inputs with signed zero when the flag is set, then selects a median or a NaN-dependent result. Compute tests cover denormal inputs in float modes0xf0and0xc0, and update expected signed-zero results and SPIR-V word limits.Suggested reviewers:
nmzik,shadowbeat070Priority: ➖ Normal
Change: Bug fix
Merge Risk: ⚪ Minimal · up to
d8f31V_MED3_F32 now produces a smaller shader and handles denormal and NaN inputs consistently with the reported GPU measurements. No actionable merge-blocking risk remains. The author could not verify PS5 hardware fidelity for the flush behavior.
Pre-merge checks:
4 ·
1
Full details: Docstring Coverage
Explanation
Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 10 functions across 5 files. (1 skipped: 1 too large.)
Resolution
Write docstrings for the functions missing them to satisfy the coverage threshold.
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
❤️ Share
Comment
@coderabbitai helpto get the list of available commands.@NeySlim on GitHub · 2026-10-17 23:09 UTC · original
RADV data point: Radeon 8060S (RDNA 3.5), Mesa 26.2.4, against
763d3be3.VectorMed3F32DenormalInputsfails on main the same way as on your GPUs, e.g.0x00019000for(+0, 0x00019000, +0), and passes with the PR; the rest of the suite is unchanged.@Nawid3333 on GitHub · 2026-10-18 15:49 UTC · original
Follow-up commit
e23737ca: in a compute shader that flushes f32 denormal inputs (FLOAT_MODE bit 4 clear, for example0xc0),V_MED3_F32now flushes its three inputs to signed zero before it takes the median.Measured in shaders that declare
DenormFlushToZero 32, the hardware instruction (v_med3_f32on the Ryzen 7600X iGPU, RDNA2, throughmid3(), andv_med3_num_f32on the RX 9070 XT) returns+0for (-1, d, 1) and-0for (-d, -1, 1), with d =0x00019000. WithDenormPreserve 32it returns d and -d. The integer keys of this PR gave the true median in every mode, which is right for FLOAT_MODE0xf0(the existing test) but not in shaders that flush.The decision uses the same
flush_f32_inputsas the compares, passed asFPCompareFlagsonFPMedTri32. I have no PS5, so the link from FLOAT_MODE to this flush rests on that existing decision.V_MED3_F16and the graphics stages are unchanged.Tests, with the PR applied on
763d3be3:VectorMed3F32FlushesDenormalInputs(FLOAT_MODE0xc0, the eight measured triples, one with a NaN): fails without the commit and passes with it on both GPUs, also with the harness forced to 32-wide subgroups and under the validation layer.VectorMed3F32DenormalInputsandVectorMed3F32NanUsesMin3Pathstill pass.shader_cfg_testspasses with and without it.v_med3_f32on the iGPU), a GLSL copy of the integer form gives the same result for every triple without a NaN, with this flush in aDenormFlushToZeroshader and without it in aDenormPreserveshader: every 32-bit value in each position, the other two from 12 edge values. With three NaN inputs the hardware returns the first one and Kyty the third, as before this PR (the ISA pseudocode gives the third); that is not changed here.Written with Claude Code (AI): it found this in a review of the PR, measured the hardware and wrote the commit; I read this before posting.
@Nawid3333 on GitHub · 2026-10-19 13:17 UTC · original
Rebased on
cd4eab9d.defb2f11movedEmitFPMedTri32intospirvEmitterAluHelpers.cpp; the integer-key form is back inspirvEmitterAlu.cppnext toEmitExt, and yourEmitMinMaxF32split stays forEmitMinMaxF32Value.Before resolving it I ran the 23 rows of your new
VectorMed3F32NanUsesMin3Paththrough the hardware instruction. Five rows that expect +0 come back as -0, the same on an RDNA2 GPU (Ryzen 7600X iGPU,v_med3_f32) and on the RX 9070 XT (v_med3_num_f32):defb2f11expectsThe other rows match, apart from the one with three NaNs (0x7fc01234, 0x7fa00001, 0xffc05678): both GPUs return the first input unchanged, 0x7fc01234, where the table, main and this PR return the third. I left that row and that behaviour as they are.
The ISA pseudo-code gives the hardware's result if its
==tells the zeros apart, as it has to in V_MIN_F32 and V_MAX_F32, which have separate branches forS0.f == +0.0 && S1.f == -0.0andS0.f == -0.0 && S1.f == +0.0. Read that way, for (-0, -0, +0) MAX3 is +0, which equals neither S0 nor S1, so the result is V_MAX_F32(-0, -0) = -0. With a numeric compare (+0 == -0) it is +0.This PR's keys sort -0 below +0, so it returns the hardware's value in all five rows, and the PR now expects -0 there.
How I measured: one compute shader that loads the 23 rows from a buffer and calls
mid3()(GL_AMD_shader_trinary_minmax) with DenormPreserve, as in float_mode 0xf0. The driver's ISA dump has onlyv_med3_f32(iGPU) orv_med3_num_f32(RX 9070 XT) for it, and the validation layer reports nothing. The shader and all 23 results are below.Size: the module of
VectorMed3F32NanUsesMin3Path(23 V_MED3_F32) is 17,593 / 36,607 words on main and 13,164 / 27,739 with this PR (wave 32 / wave 64 on a 32-wide host), about 38 instructions fewer per emitted V_MED3_F32. I moved the test's size limit down with it and kept your margins: 13,171 / 27,752.I also added four rows to this PR's two tests, with the medians both GPUs return: the largest denormal and the smallest normal next to ±0.5 in the flush test, and +inf next to finite inputs in both. Before, an infinity counted as a NaN, the smallest normal flushed, or the larger denormals left unflushed passed these tests and
VectorMed3F32NanUsesMin3Path; now each of the three makes at least one of the two tests fail.Tests on both GPUs, also with 32-wide subgroups:
VectorMed3F32NanUsesMin3Path(both waves),VectorMed3F32DenormalInputsandVectorMed3F32FlushesDenormalInputsfail on main and pass with the PR. Where main'sVectorMed3F32NanUsesMin3Pathgets past the new size limit (wave 64 on a 64-wide host), it fails exactly the five rows above. The other 809 compute-suite entries and the 30 graphics cases give the same results as on main. Fork CI (Windows, Linux, macOS): https://github.com/Nawid3333/KytyPS5/actions/runs/39254554407Probe shader and the 23 rows (hex bits)
defb2f11expects@Nawid3333 on GitHub · 2026-10-24 18:36 UTC · original
Closing:
mainhas its own V_MED3_F32 lowering since3c4124da("emit F32 min/max as exact compare-selects, MED3 with one max and saturate as NClamp"), so this version no longer applies.Pull request closed