shader: order F32 min, max and MED3 by their bits #2096
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!2096
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-2096"
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-11-04 03:13 UTC · original
Nawid3333:shader/minmax-int-order→main· +80 −38 · 2 files · 6 commits · openSummary
Following the idea in #1534 (fewer instructions, same results). The exact
V_MIN/V_MAX/V_MIN3/V_MAX3_F32path (every host except AMD) andV_MED3_F32(every host) decided the order with float compares. Kyty's modules declare no denorm mode for 32-bit floats, and Vulkan says "by default any denormalized floating-point value input into a shader ... may be flushed to zero". On a host that flushes in compares, a zero and a denormal, or two denormals, compare equal, and the result was the host's choice:min(0x00000000, 0x00000001)could give0x00000001, andmed3(0x00000000, 0x00000002, 0x00000001)could give0x00000002(main gives that on a Radeon RX 9070 XT).Change
EmitMinMaxF32: a signed compare of the raw bits, inverted when both signs are set, gives the guest order for every non-NaN value (including-0below+0), so the separate signed-zero test goes. The NaN rules are unchanged. 11 instructions for min and max (max was 12).EmitFPMedTri32(non-relaxed path): each source gets an integer order key (sign-magnitude to two's complement, shifted so every NaN key lies above the key of+inf; the constants come from one bias). On these keys it computes #2528's MED3: the median from three signed key compares (-0below+0), and with a NaN source MIN3 in source order with V_MIN_F32's NaN rule. 33 instructions instead of 60, or 49 where the final max is NMax (AMD); MED3 no longer needs a float compare or a host min/max. The relaxed path is main's.All ordering is integer, so the results no longer depend on how a host treats denormals.
Same results
±1,±2,±3, the largest finite values,±inf, signaling and quiet NaNs of both signs with payloads). It gives the same bits for this PR's MED3 and main's MED3 formula from #2528 for all 32768 triples of these values and for 1,000,000 random triples (half of their sources taken from these values and 64 fixed random patterns).VectorMinMaxF32RuntimeEdgesgets six rows (a zero and a denormal, two denormals, two negative denormals, a negative denormal and-0,-sNaNnext to-inf, a signaling NaN with a denormal), andVectorMed3F32NanUsesMin3Pathseven on top of main's 23 (denormal order of both signs,-infwith a negative denormal and+0, three NaNs of both signs, a NaN of either sign with two numbers, a NaN with+0and-0).--float-med3-onlyruns the MED3 test on its own. The min/max test's expected values now come from a total order of the bits, so they no longer depend on the host CPU's float mode. On a Ryzen 5 7600X iGPU (gfx1036, RDNA2),v_med3_f32with IEEE mode off returns the expected value of each new MED3 row.--float-min-max-only,--float-med3-only,--relaxed-float-onlyandshader_cfg_testspass. Main gets 3 of the new MED3 rows and 12 results ofVectorMinMaxF32RuntimeEdgeswith zeros and denormals wrong there. Ten mutations each make these checks fail: no inversion for two negative sources, a NaN left source kept, NaN keys left unclamped in MIN3,+infcounted as a NaN, no magnitude flip in the key, no bias in the key, unsigned key compares, the median takingaforc, MIN3 comparing witha's key after takingb, and a NaN test withoutc.--float-min-max-only(#1761) fails on main with this driver: NMin/NMax of the signaling NaN0x7fa00001and2.0return the quieted NaN0x7fe00001instead of2.0(it passed with driver 26.8.1). This change doesn't touch the native path, but its new denormal rows fail there too:VectorMinMaxF32RuntimeEdgesruns withfloat_mode = 0xf0(FP32 denormals preserved), the module declares no 32-bitDenormPreserve, and the driver's NMin/NMax flush the denormals (max(0, 0x00000001)returns0,min3of0x80000001returns-0). So on AMD the native path loses denormals that the guest's float mode keeps.8dbee8635(relaxed float): the only conflict wasEmitFPMedTri32; its relaxed path comes first, unchanged, and the integer-key code is the non-relaxed path. Merged mainbd4a9e728(#2528): the non-relaxed path now computes #2528's MED3 on the keys (comment of 11-23).8bad39f23).Games
In the shaders our runs decoded (3016 distinct shaders of four games), V_MED3_F32 occurs 3904 times: Demon's Souls 683, ASTRO BOT 592, Ghost of Yōtei 752, Assassin's Creed Black Flag 1877. V_MAX_F32 occurs 28137 times and V_MIN_F32 11439 times; they take this path on hosts other than AMD.
Tested in Ghost of Yōtei (PPSA26344, Radeon RX 9070 XT, main
578f2d43fwith and without this change, two runs per build; the first one fills the driver's pipeline cache): in the second runs both builds reach the same screen ("DIGITAL DELUXE BONUS"), and neither logs a fatal error. A capture of the game's shaders (235 programs) recompiled offline with the captured device profile (AMD) emits 9,658 fewer instructions with this change, and no program grows.Written with Claude Code (AI): it wrote the change, the tests and the model check, and ran the tests and the game runs on my PC (Windows 11, RX 9070 XT, driver 26.10.1). I reviewed the diff and this description before opening the PR.
🤖 Generated with Claude Code
8dbee8635) into shader/minmax-int-order 21b1a57dc2bd4a9e728) into shader/minmax-int-order 8bad39f23b@coderabbitai[bot] on GitHub · 2026-11-04 03:20 UTC · original · edited 2026-11-23 16:21 UTC
No actionable comments were generated in the recent review.
Walkthrough
The SPIR-V emitter changes float min/max and median selection to use signed integer operations on float bit patterns. The median path uses derived order keys and detects NaNs from the maximum key. Compute tests add cases for NaNs, infinities, signed zeros, and denormal values. A new command-line option runs the median test with wave sizes 32 and 64.
Suggested reviewers:
nmzik,arda501Change: Bug fix
Merge Risk:
Minimal · up to
8bad3This change makes float min/max and MED3 results independent of how a host handles denormals, and it shrinks the emitted shader code. No concrete merge-blocking defect was established. The disclosed native AMD driver behavior on the new denormal rows applies only to an opt-in test mode.
Pre-merge checks:
4
Full details: Title check
Explanation
The title "shader: order F32 min, max and MED3 by their bits" matches the main change. The diff replaces float comparisons with integer bit-ordering in EmitMinMaxF32 and EmitFPMedTri32, and the tests were updated to match. The title is concise and specific, and a reviewer scanning history can identify the primary change.
Full details: Description check
Explanation
The description is directly related to the changeset. It explains the denormal-flush problem in V_MIN/V_MAX/V_MED3 F32 handling, describes the integer order-key change in EmitMinMaxF32 and EmitFPMedTri32, and lists the test additions (VectorMinMaxF32RuntimeEdges, VectorMed3F32NanUsesMin3Path, --float-med3-only). It also reports test and game-run results.
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
a5b2ab31-64da-4d42-b10c-edb70b0c4331Reviewing files that changed from the base of the PR and between
734e36a340and8bad39f23b.src/graphics/shader/recompiler/backend/spirv/spirvEmitterAluHelpers.cpptests/ShaderRecompilerComputeTests.cppIncluded review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.
@ -32806,0 +32803,4 @@{0x7fc01234u, 0xffc05678u, 0x7fa00001u}, {0xffc05678u, 0x7fc01234u, 0x3f800000u},{0x00000000u, 0x00000001u, 0x80000001u}, {0x00000001u, 0x00000002u, 0x80000000u},{0x80000001u, 0x80000002u, 0x00000001u}, {0x80000001u, 0x80000000u, 0x00000000u},{0xff800001u, 0xff800000u, 0x7f7fffffu}, {0x7fa00001u, 0x00000001u, 0x80000001u}};@coderabbitai[bot] on GitHub · 2026-11-04 03:20 UTC · original · edited 2026-11-04 04:04 UTC
Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 9101
Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 3675
Make the min/max oracle independent of host denormal handling.
min2andmax2convert the input bits to hostfloatvalues and compare them with<and>=. The added subnormal cases can therefore produce host-FP-state-dependent expected bits when FTZ/DAZ is enabled. Compute the expected results from integer order keys, or explicitly set and restore the required host FP state before building the oracle.✅ Addressed in commit
f82b5ae@coderabbitai[bot] on GitHub · 2026-11-04 03:20 UTC · original · reviewed
Actionable comments posted: 1
4e2459cb-a207-431f-9b3d-6d891b3afc85Reviewing files that changed from the base of the PR and between
eecd982193and90a98f9e8e.src/graphics/shader/recompiler/backend/spirv/spirvEmitterAluHelpers.cpptests/ShaderRecompilerComputeTests.cppIncluded review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.
@ -32806,0 +32803,4 @@{0x7fc01234u, 0xffc05678u, 0x7fa00001u}, {0xffc05678u, 0x7fc01234u, 0x3f800000u},{0x00000000u, 0x00000001u, 0x80000001u}, {0x00000001u, 0x00000002u, 0x80000000u},{0x80000001u, 0x80000002u, 0x00000001u}, {0x80000001u, 0x80000000u, 0x00000000u},{0xff800001u, 0xff800000u, 0x7f7fffffu}, {0x7fa00001u, 0x00000001u, 0x80000001u}};@Nawid3333 on GitHub · 2026-11-04 04:04 UTC · original
Thanks, fixed in
f82b5ae40:min2andmax2now order the raw bits with a total-order key (~vfor negative values,v | 0x80000000otherwise), so the expected bits no longer depend on the host's float mode, and thezeroandvaluehelpers are gone. On a host that keeps denormals, the new oracle gives the same result as the old one for every pair of 32 class-boundary values (both orders, min and max). The test comment that said denormal operands were left out is removed, since the test now covers them.@ -32806,0 +32803,4 @@{0x7fc01234u, 0xffc05678u, 0x7fa00001u}, {0xffc05678u, 0x7fc01234u, 0x3f800000u},{0x00000000u, 0x00000001u, 0x80000001u}, {0x00000001u, 0x00000002u, 0x80000000u},{0x80000001u, 0x80000002u, 0x00000001u}, {0x80000001u, 0x80000000u, 0x00000000u},{0xff800001u, 0xff800000u, 0x7f7fffffu}, {0x7fa00001u, 0x00000001u, 0x80000001u}};@coderabbitai[bot] on GitHub · 2026-11-04 04:05 UTC · original
@Nawid3333 Thanks for the fix. Using integer order keys in
min2andmax2addresses the host-denormal dependency. Removing the outdated comment also matches the added denormal coverage.If you found this review helpful, would you consider giving us a shout-out on X?
Thank you for using CodeRabbit!
Review thread resolved.
You are interacting with an AI system.
@coderabbitai[bot] on GitHub · 2026-11-08 14:35 UTC · original · reviewed
749957b0-1bae-44ba-b4ec-e83e19a4e48aReviewing files that changed from the base of the PR and between
f82b5ae408and21b1a57dc2.src/graphics/shader/recompiler/backend/spirv/spirvEmitterAluHelpers.cpptests/ShaderRecompilerComputeTests.cppIncluded review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.
@coderabbitai[bot] on GitHub · 2026-11-08 14:55 UTC · original · reviewed
00e1323c-6c82-4fb6-8437-4aea3c18a252Reviewing files that changed from the base of the PR and between
21b1a57dc2and734e36a340.src/graphics/shader/recompiler/backend/spirv/spirvEmitterAluHelpers.cppIncluded review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review.
@Nawid3333 on GitHub · 2026-11-12 08:34 UTC · original · edited 2026-11-13 13:29 UTC
Measured on game shaders without a game run: the 172 Ghost of Yōtei programs from a capture of 11-03 (113 CS, 43 PS, 16 VS), recompiled offline by main (
bc934ef03) and by main with this PR, with the same captured inputs (a replay tool on top of both feeds them to the recompiler; it changes nothing in the recompiler). Guest memory reads as zeros in the replay, so each program is compiled in a real variant, but not necessarily the one the game used. Counts are SPIR-V instructions without debug instructions.robustStorageBufferAccessSizeAlignment1 while AMD GPUs report 4): 44 of 172 programs change, none grows, 493,801 → 487,820 instructions (−1.21 %). The largest drop is a compute shader, 33,358 → 32,622.native_float_min_maxis set only for vendor 0x1002): 95 of 167 programs change, none grows, 517,464 → 507,295 (−1.97 %). The other 5 needmaintenance8for a dynamic texel offset on both builds.Every module that the two builds emit passes spirv-val. This measures size only; the same-results check is the model and the tests in the description.
Edit: the AMD line first came from a profile with robust buffer access on (489,006 → 483,025); the changed programs and the drop are the same in the AMD profile.
@Nawid3333 on GitHub · 2026-11-23 17:35 UTC · original
Merged main
bd4a9e728, which has #2528. The conflicts were inEmitFPMedTri32and in the test's MED3 rows and size cap.EmitFPMedTri32's non-relaxed path now computes #2528's MED3 (the ordered median with-0below+0; with a NaN source, MIN3 in source order) on this PR's integer order keys:bifa < bequalsb < c, elsecifa < bequalsa < c, elsea, with signed compares of the keys;+inf;+inf.That is 33 instructions; main's MED3 is 60, or 49 where its final max is NMax (AMD). The key constants now come from one bias (
OrderKeyBiasF32,InfinityKeyF32), as CodeRabbit suggested in its review of 11-08, and the zero keys of the old tie rule are gone. The relaxed path is main's.Checks of this version (Radeon RX 9070 XT, driver 26.10.1, with #2038 for #2036, unless named):
±inf, signaling and quiet NaNs of both signs with payloads) and for 1,000,000 random triples (half of their sources taken from these values and 64 fixed random patterns). On the boundary triples, main's formula gives the same bits on this PR's V_MIN/V_MAX as on the V_MIN/V_MAX semantics main documents.VectorMed3F32NanUsesMin3Pathkeeps main's 23 rows and gets 7 more: denormal order of both signs,-infwith a negative denormal and+0, three NaNs of both signs, a NaN of either sign with two numbers, and a NaN with+0and-0. On a Ryzen 5 7600X iGPU (gfx1036, RDNA2),v_med3_f32with IEEE mode off returns the expected value of each of these rows and of main's all-NaN row.--float-med3-onlypasses. Main's MED3 fails 3 of the new rows on this GPU:med3(0x00000000, 0x00000002, 0x00000001)gives0x00000002,med3(0x80000003, 0x80000001, 0x80000002)gives0x80000001, andmed3(-inf, 0x80000001, +0)gives+0.--float-min-max-onlypass (main's exact path gets 12 results ofVectorMinMaxF32RuntimeEdgeswith zeros and denormals wrong here); its native cases fail as on main (see the description).--relaxed-float-onlyandshader_cfg_testspass.+infcounted as a NaN, no magnitude flip in the key, no bias in the key, unsigned key compares, the median takingaforc, MIN3 comparing witha's key after takingb, and a NaN test withoutc.Ghost of Yōtei shaders, offline: a capture of 11-23 (235 programs) recompiled by main
bd4a9e728and by main with this PR (both with the lab's replay commits on top), with the captured inputs and device profile (AMD: native float min/max). 63 programs change (43 CS, 20 PS), exactly the 63 that contain V_MED3_F32 or V_MED3_F16 (613 MED3 instructions in all; both opcodes use this MED3); none grows; 1,192,276 → 1,182,618 instructions (−0.81 %), 16 per MED3 plus 2 to 4 constants per program. With the default profile (exact min/max, as main uses on every non-AMD GPU), the 59 of these programs that both builds emit (4 needmaintenance8) go from 625,241 to 607,928, V_MIN/V_MAX included. Every module both builds emit passes the SPIR-V validator. Counts are SPIR-V instructions without debug instructions.Ghost of Yōtei (PPSA26344) on the RX 9070 XT, main
578f2d43fwith and without this PR, two runs per build (the first one fills the driver's pipeline cache): in the second runs both builds reach the same screen ("DIGITAL DELUXE BONUS"), and neither logs a fatal error or loses the device.View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.